Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add string/to-well-formed #1675

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions lib/node_modules/@stdlib/string/to-well-formed/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<!--

@license Apache-2.0

Copyright (c) 2024 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# str2wellformed

> Creates a new well-formed string.
Pratik772846 marked this conversation as resolved.
Show resolved Hide resolved

<section class="usage">

## Usage

```javascript
var str2wellformed = require( '@stdlib/string/to-well-formed' );
```

#### str2wellformed( str )

Creates a new well-formed string.
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

<!-- eslint-disable no-new-wrappers -->

```javascript
var result = str2wellformed( '' );
// returns ''

result = str2wellformed( '\uDBFF' );
// returns '�'

result = str2wellformed( '\uDBFFFF\uDBFF' );
// returns '�FF�'

result = str2wellformed( '-5' );
// returns '-5'
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint-disable no-new-wrappers -->

<!-- eslint no-undef: "error" -->

```javascript
var str2wellformed = require( '@stdlib/string/to-well-formed' );

var result = str2wellformed( '' );
// returns ''

result = str2wellformed( '\uDBFF' );
// returns '�'

result = str2wellformed( '\uDBFF\uDBFF' );
// returns '��'

result = str2wellformed( '5\uDBFF' );
// returns '5�'

result = str2wellformed( '-5' );
// returns '-5'
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">


</section>
Pratik772846 marked this conversation as resolved.
Show resolved Hide resolved

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

<!-- <related-links> -->


<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var str2wellformed = require( './../lib' );


// MAIN //

bench( pkg+'::str2wellformed', function benchmark( b ) {
Pratik772846 marked this conversation as resolved.
Show resolved Hide resolved
var result;
var strs;
var i;

strs = [
'',
'\uDBFF',
'Hello World',
'3.14',
'\uDBFF\uDBFF',
'foo\uD800bar',
'hello123'
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
result = str2wellformed( strs[ i % strs.length ] );
if ( typeof result !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( !isString( result ) ) {
b.fail( 'should return a string' );
}
b.pass( 'benchmark finished' );
b.end();
});
31 changes: 31 additions & 0 deletions lib/node_modules/@stdlib/string/to-well-formed/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

{{alias}}( str )
Returns a well formed string.

Parameters
----------
str: string
String to test.
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
result: string
Well formed string.

Examples
--------
> result = {{alias}}( '\uDBFF' )
'�'

> result = {{alias}}( '\uDBFFFF\uDBFF' )
'�FF�'

> result = {{alias}}( '-5' )
'-5'

> result = {{alias}}( '\uDBFFFF\uDBFF' )
'�FF�'

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/**
* Tests if a string is well-formed.
Pratik772846 marked this conversation as resolved.
Show resolved Hide resolved
*
* @param str - string to test
* @returns returns a well formed string
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* var result = str2wellformed( '\uDBFF' );
* // returns �
*
* @example
* var result = str2wellformed( '\uDBFFFF\uDBFF' );
* // returns �FF�
*
* @example
* var result = str2wellformed( '-5' );
* // returns -5
*/
declare function str2wellformed( str: string ): string;


// EXPORTS //

export = str2wellformed;
34 changes: 34 additions & 0 deletions lib/node_modules/@stdlib/string/to-well-formed/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import str2wellformed = require( './index' );

// TESTS //
Pratik772846 marked this conversation as resolved.
Show resolved Hide resolved

// The function returns a string...
{
str2wellformed( '' ); // $ExpectType string
str2wellformed( 'Hello' ); // $ExpectType string
str2wellformed( '\uDBFFFF\uDBFF' ); // $ExpectType string
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
str2wellformed(); // $ExpectError
str2wellformed( '', '\uDBFF' ); // $ExpectError
}
35 changes: 35 additions & 0 deletions lib/node_modules/@stdlib/string/to-well-formed/examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* eslint-disable no-new-wrappers */

'use strict';

var str2wellformed = require( './../lib' );

console.log( str2wellformed( '' ) );

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log( str2wellformed( '' ) );
console.log( str2wellformed( '' ) );
// => ''

I will prefer to do it for all console.log here

console.log( str2wellformed( new String( '' ) ) );

console.log( str2wellformed( '\uDFFFab' ) );

console.log( str2wellformed( '\uDBFFFF\uDBFF' ) );

console.log( str2wellformed( 'ab\uD800' ) );

console.log( str2wellformed( 'ab\uD83D\uDE04c' ) );
50 changes: 50 additions & 0 deletions lib/node_modules/@stdlib/string/to-well-formed/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* Return a well formed string.
Planeshifter marked this conversation as resolved.
Show resolved Hide resolved
*
* @module @stdlib/string/to-well-formed
*
* @example
* var str2wellformed = require( '@stdlib/string/to-well-formed' );
*
* var str = "ab\uD800";
* var newStr = str2wellformed( str );
* // returns ab�
*
* str = "ab\uD83D\uDE04c";
* newStr = str2wellformed( str );
* // returns ab😄c
*
* str = "abc";
* bool = str2wellformed( str );
* // returns abc
*
Pratik772846 marked this conversation as resolved.
Show resolved Hide resolved
*/

// MODULES //

var main = require( './main.js' );


// EXPORTS //

module.exports = main;
Loading
Loading