-
-
Notifications
You must be signed in to change notification settings - Fork 836
feat: add string/to-well-formed
#1675
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
Merged
Planeshifter
merged 6 commits into
stdlib-js:develop
from
Pratik772846:package-stdlib/string/iswellformed
Mar 10, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
02743a7
feat: added @stdlib/string/to-well-formed
Pratik772846 f0d6ebf
feat: add string/to-well-formed
Pratik772846 330f5b0
feat: add string/to-well-formed
Pratik772846 8173ed2
feat: add string/to-well-formed
Pratik772846 1f2a28d
chore: apply code review
Pranavchiku 738e84f
Apply suggestions from code review
Planeshifter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
lib/node_modules/@stdlib/string/to-well-formed/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<!-- | ||
|
||
@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 | ||
|
||
> Create a new well-formed string. | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var str2wellformed = require( '@stdlib/string/to-well-formed' ); | ||
``` | ||
|
||
#### str2wellformed( str ) | ||
|
||
Creates a new well-formed string by replacing all lone surrogates with the replacement character `\uFFFD`. | ||
|
||
<!-- 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> | ||
|
||
<!-- /.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 --> |
59 changes: 59 additions & 0 deletions
59
lib/node_modules/@stdlib/string/to-well-formed/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, function benchmark( b ) { | ||
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
31
lib/node_modules/@stdlib/string/to-well-formed/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
{{alias}}( str ) | ||
Returns a well formed string. | ||
|
||
Parameters | ||
---------- | ||
str: string | ||
Input string. | ||
|
||
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 | ||
-------- | ||
|
44 changes: 44 additions & 0 deletions
44
lib/node_modules/@stdlib/string/to-well-formed/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
/** | ||
* Replaces all lone surrogates in order to create a new string which is well formed. | ||
* | ||
* @param str - string to test | ||
* @returns new string which does not contain any lone surrogates | ||
* | ||
* @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; |
35 changes: 35 additions & 0 deletions
35
lib/node_modules/@stdlib/string/to-well-formed/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
|
||
import str2wellformed = require( './index' ); | ||
|
||
|
||
// TESTS // | ||
|
||
// 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 | ||
} |
41 changes: 41 additions & 0 deletions
41
lib/node_modules/@stdlib/string/to-well-formed/examples/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @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( '' ) ); | ||
// => '' | ||
|
||
console.log( str2wellformed( new String( '' ) ) ); | ||
// => '' | ||
|
||
console.log( str2wellformed( '\uDFFFab' ) ); | ||
// => '�ab' | ||
|
||
console.log( str2wellformed( '\uDBFFFF\uDBFF' ) ); | ||
// => '�FF�' | ||
|
||
console.log( str2wellformed( 'ab\uD800' ) ); | ||
// => 'ab�' | ||
|
||
console.log( str2wellformed( 'ab\uD83D\uDE04c' ) ); | ||
// => 'ab😄c' |
49 changes: 49 additions & 0 deletions
49
lib/node_modules/@stdlib/string/to-well-formed/lib/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @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'; | ||
|
||
/** | ||
* Convert a string to a well formed string. | ||
* | ||
* @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 | ||
*/ | ||
|
||
// MODULES // | ||
|
||
var main = require( './main.js' ); | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = main; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.