diff --git a/lib/node_modules/@stdlib/string/base/reverse-code-points/README.md b/lib/node_modules/@stdlib/string/base/reverse-code-points/README.md new file mode 100644 index 00000000000..83553c12c1f --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-code-points/README.md @@ -0,0 +1,92 @@ + + +# reverseCodePoints + +> Reverse the Unicode code points of a string. + +
+ +## Usage + +```javascript +var reverseCodePoints = require( '@stdlib/string/base/reverse-code-points' ); +``` + +#### reverseCodePoints( str ) + +Reverses the Unicode code points of a string. + +```javascript +var out = reverseCodePoints( 'last man standing' ); +// returns 'gnidnats nam tsal' + +out = reverseCodePoints( 'Hidden Treasures' ); +// returns 'serusaerT neddiH' + +out = reverseCodePoints( 'foo bar' ); +// returns 'rab oof' +``` + +
+ + + +
+ +## Examples + + + +```javascript +var reverseCodePoints = require( '@stdlib/string/base/reverse-code-points' ); + +var str = reverseCodePoints( 'presidential election' ); +// returns 'noitcele laitnediserp' + +str = reverseCodePoints( 'JavaScript' ); +// returns 'tpircSavaJ' + +str = reverseCodePoints( 'The Last of the Mohicans' ); +// returns 'snacihoM eht fo tsaL ehT' + +str = reverseCodePoints( 'अनुच्छेद' ); +// returns 'देछ्चुनअ' +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/reverse-code-points/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/reverse-code-points/benchmark/benchmark.js new file mode 100644 index 00000000000..052ff1d5291 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-code-points/benchmark/benchmark.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 reverse = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var out; + var i; + + values = [ + 'beep boop', + 'foo bar', + 'xyz abc', + '🐶🐮🐷🐰🐸' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = reverse( values[ i%values.length ], 1 ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/string/base/reverse-code-points/docs/repl.txt b/lib/node_modules/@stdlib/string/base/reverse-code-points/docs/repl.txt new file mode 100644 index 00000000000..afb19a2d754 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-code-points/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( str ) + Reverses the Unicode code points of a string. + + Parameters + ---------- + str: string + Input string. + + Returns + ------- + out: string + Output string. + + Examples + -------- + > var out = {{alias}}( 'beep' ) + 'peeb' + > out = {{alias}}( 'Boop' ) + 'pooB' + > out = {{alias}}( 'foo bar' ) + 'rab oof' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/string/base/reverse-code-points/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/reverse-code-points/docs/types/index.d.ts new file mode 100644 index 00000000000..48765eeb882 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-code-points/docs/types/index.d.ts @@ -0,0 +1,52 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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: 2.0 + +/** +* Reverses the Unicode code points of a string. +* +* @param str - input string +* @returns output string +* +* @example +* var out = reverse( 'last man standing' ); +* // returns 'gnidnats nam tsal' +* +* @example +* var out = reverse( 'presidential election' ); +* // returns 'noitcele laitnediserp' +* +* @example +* var out = reverse( 'JavaScript' ); +* // returns 'tpircSavaJ' +* +* @example +* var out = reverse( 'Hidden Treasures' ); +* // returns 'serusaerT neddiH' +* +* @example +* var out = reverse( 'foo bar' ); +* // returns 'rab oof' +*/ +declare function reverse( str: string ): string; + + +// EXPORTS // + +export = reverse; diff --git a/lib/node_modules/@stdlib/string/base/reverse-code-points/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/reverse-code-points/docs/types/test.ts new file mode 100644 index 00000000000..116ee34cdec --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-code-points/docs/types/test.ts @@ -0,0 +1,46 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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 reverse = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + reverse( 'abc' ); // $ExpectType string +} + +// The compiler throws an error if the function is provided a value other than a string... +{ + reverse( true ); // $ExpectError + reverse( false ); // $ExpectError + reverse( null ); // $ExpectError + reverse( undefined ); // $ExpectError + reverse( 5 ); // $ExpectError + reverse( [] ); // $ExpectError + reverse( {} ); // $ExpectError + reverse( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + reverse(); // $ExpectError + reverse( 'abc', 1 ); // $ExpectError + reverse( 'abc', 1, 2 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/base/reverse-code-points/examples/index.js b/lib/node_modules/@stdlib/string/base/reverse-code-points/examples/index.js new file mode 100644 index 00000000000..b56a85ddc03 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-code-points/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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'; + +var reverse = require( './../lib' ); + +console.log( reverse( 'presidential election' ) ); +// => 'noitcele laitnediserp' + +console.log( reverse( 'JavaScript' ) ); +// => 'tpircSavaJ' + +console.log( reverse( 'The Last of the Mohicans' ) ); +// => 'snacihoM eht fo tsaL ehT' + +console.log( reverse( 'अनुच्छेद' ) ); +// => 'देछ्चुनअ' diff --git a/lib/node_modules/@stdlib/string/base/reverse-code-points/lib/index.js b/lib/node_modules/@stdlib/string/base/reverse-code-points/lib/index.js new file mode 100644 index 00000000000..7d6e5f35db8 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-code-points/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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'; + +/** +* Reverse the Unicode code points of a string. +* +* @module @stdlib/string/base/reverse-code-points +* +* @example +* var reverse = require( '@stdlib/string/base/reverse-code-points' ); +* +* var out = reverse( 'last man standing' ); +* // returns 'gnidnats nam tsal' +* +* out = reverse( 'Hidden Treasures' ); +* // returns 'serusaerT neddiH'; +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/base/reverse-code-points/lib/main.js b/lib/node_modules/@stdlib/string/base/reverse-code-points/lib/main.js new file mode 100644 index 00000000000..5f6360b0aab --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-code-points/lib/main.js @@ -0,0 +1,92 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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'; + +// VARIABLES // + +var RE_UTF16_LOW_SURROGATE = /[\uDC00-\uDFFF]/; // TODO: replace with stdlib pkg +var RE_UTF16_HIGH_SURROGATE = /[\uD800-\uDBFF]/; // TODO: replace with stdlib pkg + + +// MAIN // + +/** +* Reverses the Unicode code points of a string. +* +* @param {string} str - input string +* @returns {string} output string +* +* @example +* var out = reverse( 'last man standing' ); +* // returns 'gnidnats nam tsal' +* +* @example +* var out = reverse( 'presidential election' ); +* // returns 'noitcele laitnediserp' +* +* @example +* var out = reverse( 'JavaScript' ); +* // returns 'tpircSavaJ' +* +* @example +* var out = reverse( 'Hidden Treasures' ); +* // returns 'serusaerT neddiH' +*/ +function reverse( str ) { + var len; + var out; + var ch1; + var ch2; + var i; + + len = str.length; + out = ''; + + // Process the string one Unicode code unit at a time and count UTF-16 surrogate pairs as a single Unicode code point... + for ( i = 0; i < len; i++ ) { + ch1 = str[ i ]; + + // Check for a high UTF-16 surrogate... + if ( RE_UTF16_HIGH_SURROGATE.test( ch1 ) ) { + // Check for an unpaired surrogate at the end of the input string... + if ( i === len-1 ) { + // We found an unpaired surrogate... + out = ch1 + out; + break; + } + // Check whether the high surrogate is paired with a low surrogate... + ch2 = str[ i+1 ]; + if ( RE_UTF16_LOW_SURROGATE.test( ch2 ) ) { + // We found a surrogate pair: + out = ch1 + ch2 + out; + i += 1; // bump the index to process the next code unit + } else { + out = ch1 + out; + } + } else { + out = ch1 + out; + } + } + return out; +} + + +// EXPORTS // + +module.exports = reverse; diff --git a/lib/node_modules/@stdlib/string/base/reverse-code-points/package.json b/lib/node_modules/@stdlib/string/base/reverse-code-points/package.json new file mode 100644 index 00000000000..4a59877e1ef --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-code-points/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/string/base/reverse-code-points", + "version": "0.0.0", + "description": "Reverse the Unicode code points of a string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdstring", + "utilities", + "utility", + "utils", + "util", + "string", + "str", + "base", + "reverse", + "character", + "char", + "codepoint", + "unicode" + ] +} diff --git a/lib/node_modules/@stdlib/string/base/reverse-code-points/test/test.js b/lib/node_modules/@stdlib/string/base/reverse-code-points/test/test.js new file mode 100644 index 00000000000..7f7866d1eda --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-code-points/test/test.js @@ -0,0 +1,80 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 tape = require( 'tape' ); +var reverse = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof reverse, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an empty string if provided an empty string', function test( t ) { + t.strictEqual( reverse( '' ), '', 'returns expected value' ); + t.end(); +}); + +tape( 'the function reverses the Unicode code points of a provided string (ascii)', function test( t ) { + var out; + + out = reverse( 'hello world' ); + t.strictEqual( out, 'dlrow olleh', 'returns expected value' ); + + out = reverse( '!!!*' ); + t.strictEqual( out, '*!!!', 'returns expected value' ); + + out = reverse( 'Hello World' ); + t.strictEqual( out, 'dlroW olleH', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function reverses the Unicode code points of a provided string (Unicode)', function test( t ) { + var out; + + out = reverse( 'अनुच्छेद' ); + t.strictEqual( out, 'देछ्चुनअ', 'returns expected value' ); + + out = reverse( '六书/六書' ); + t.strictEqual( out, '書六/书六', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function reverses the Unicode code points of a provided string (surrogate pair)', function test( t ) { + var out; + + out = reverse( 'foo\uD834\uDF06bar' ); + t.strictEqual( out, 'rab\uD834\uDF06oof', 'returns expected value' ); + + out = reverse( 'foo\uD834bar\uDF06baz' ); + t.strictEqual( out, 'zab\uDF06rab\uD834oof', 'returns expected value' ); + + out = reverse( 'foo\uD834bar\uDF06\uD834' ); + t.strictEqual( out, '\uD834\uDF06rab\uD834oof', 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/README.md b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/README.md new file mode 100644 index 00000000000..3cf6926c1d7 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/README.md @@ -0,0 +1,92 @@ + + +# reverseGraphemeClusters + +> Reverse the grapheme clusters (i.e., user-perceived characters) of a string. + +
+ +## Usage + +```javascript +var reverseGraphemeClusters = require( '@stdlib/string/base/reverse-grapheme-clusters' ); +``` + +#### reverseGraphemeClusters( str ) + +Reverses the grapheme clusters (i.e., user-perceived characters) of a string. + +```javascript +var out = reverseGraphemeClusters( 'last man standing' ); +// returns 'gnidnats nam tsal' + +out = reverseGraphemeClusters( 'Hidden Treasures' ); +// returns 'serusaerT neddiH' + +out = reverseGraphemeClusters( 'foo bar' ); +// returns 'rab oof' +``` + +
+ + + +
+ +## Examples + + + +```javascript +var reverseGraphemeClusters = require( '@stdlib/string/base/reverse-grapheme-clusters' ); + +var str = reverseGraphemeClusters( 'presidential election' ); +// returns 'noitcele laitnediserp' + +str = reverseGraphemeClusters( 'JavaScript' ); +// returns 'tpircSavaJ' + +str = reverseGraphemeClusters( 'The Last of the Mohicans' ); +// returns 'snacihoM eht fo tsaL ehT' + +str = reverseGraphemeClusters( '🐶🐮🐷🐰🐸' ); +// returns '🐸🐰🐷🐮🐶' +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/benchmark/benchmark.js new file mode 100644 index 00000000000..8f45bcfe87b --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/benchmark/benchmark.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 reverse = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var out; + var i; + + values = [ + 'beep boop', + 'foo bar', + 'xyz abc', + '🐶🐮🐷🐰🐸' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = reverse( values[ i%values.length ] ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/docs/repl.txt b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/docs/repl.txt new file mode 100644 index 00000000000..960ed7170ec --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/docs/repl.txt @@ -0,0 +1,27 @@ + +{{alias}}( str ) + Reverses the grapheme clusters (i.e., user-perceived characters) of a + string. + + Parameters + ---------- + str: string + Input string. + + Returns + ------- + out: string + Output string. + + Examples + -------- + > var out = {{alias}}( 'beep' ) + 'peeb' + > out = {{alias}}( 'Boop' ) + 'pooB' + > out = {{alias}}( 'foo bar' ) + 'rab oof' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/docs/types/index.d.ts new file mode 100644 index 00000000000..1f40280204a --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/docs/types/index.d.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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: 2.0 + +/** +* Reverses the grapheme clusters (i.e., user-perceived characters) of a string. +* +* @param str - input string +* @returns output string +* +* @example +* var out = reverse( 'last man standing' ); +* // returns 'gnidnats nam tsal' +* +* @example +* var out = reverse( 'presidential election' ); +* // returns 'noitcele laitnediserp' +* +* @example +* var out = reverse( 'JavaScript' ); +* // returns 'tpircSavaJ' +* +* @example +* var out = reverse( 'Hidden Treasures' ); +* // returns 'serusaerT neddiH' +* +* @example +* var out = reverse( '🐶🐮🐷🐰🐸' ); +* // returns '🐸🐰🐷🐮🐶' +* +* @example +* var out = reverse( 'foo bar' ); +* // returns 'rab oof' +*/ +declare function reverse( str: string ): string; + + +// EXPORTS // + +export = reverse; diff --git a/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/docs/types/test.ts new file mode 100644 index 00000000000..f6d42b97e47 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/docs/types/test.ts @@ -0,0 +1,46 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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 reverse = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + reverse( 'abc' ); // $ExpectType string +} + +// The compiler throws an error if the function is provided a value other than a string... +{ + reverse( true ); // $ExpectError + reverse( false ); // $ExpectError + reverse( null ); // $ExpectError + reverse( undefined ); // $ExpectError + reverse( 5 ); // $ExpectError + reverse( [] ); // $ExpectError + reverse( {} ); // $ExpectError + reverse( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + reverse(); // $ExpectError + reverse( 'abc', 2 ); // $ExpectError + reverse( 'abc', 1, 2 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/examples/index.js b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/examples/index.js new file mode 100644 index 00000000000..2f92c9871ec --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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'; + +var reverseGraphemeClusters = require( './../lib' ); + +console.log( reverseGraphemeClusters( 'presidential election' ) ); +// => 'noitcele laitnediserp' + +console.log( reverseGraphemeClusters( 'JavaScript' ) ); +// => 'tpircSavaJ' + +console.log( reverseGraphemeClusters( 'The Last of the Mohicans' ) ); +// => 'snacihoM eht fo tsaL ehT' + +console.log( reverseGraphemeClusters( '🐶🐮🐷🐰🐸' ) ); +// => '🐸🐰🐷🐮🐶' diff --git a/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/lib/index.js b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/lib/index.js new file mode 100644 index 00000000000..2c4753e3d26 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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'; + +/** +* Reverse the grapheme clusters (i.e., user-perceived characters) of a string. +* +* @module @stdlib/string/base/reverse-grapheme-clusters +* +* @example +* var reverse = require( '@stdlib/string/base/reverse-grapheme-clusters' ); +* +* var out = reverse( 'last man standing' ); +* // returns 'gnidnats nam tsal' +* +* out = reverse( 'Hidden Treasures' ); +* // returns 'serusaerT neddiH'; +* +* out = reverse( '🐶🐮🐷🐰🐸' ); +* // returns '🐸🐰🐷🐮🐶' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/lib/main.js b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/lib/main.js new file mode 100644 index 00000000000..f2405c94280 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/lib/main.js @@ -0,0 +1,81 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 nextGraphemeClusterBreak = require( '@stdlib/string/next-grapheme-cluster-break' ); + + +// MAIN // + +/** +* Reverses the grapheme clusters (i.e., user-perceived characters) of a string. +* +* @param {string} str - input string +* @returns {string} output string +* +* @example +* var out = reverse( 'last man standing' ); +* // returns 'gnidnats nam tsal' +* +* @example +* var out = reverse( 'presidential election' ); +* // returns 'noitcele laitnediserp' +* +* @example +* var out = reverse( 'JavaScript' ); +* // returns 'tpircSavaJ' +* +* @example +* var out = reverse( 'Hidden Treasures' ); +* // returns 'serusaerT neddiH' +* +* @example +* var out = reverse( '🐶🐮🐷🐰🐸' ); +* // returns '🐸🐰🐷🐮🐶' +*/ +function reverse( str ) { + var cluster; + var out; + var idx; + var brk; + var i; + + out = ''; + idx = 0; + while ( idx < str.length ) { + brk = nextGraphemeClusterBreak( str, idx ); + if ( brk === -1 ) { + brk = str.length; + } + cluster = ''; + for ( i = idx; i < brk; i++ ) { + cluster += str.charAt( i ); + } + out = cluster + out; + idx = brk; + } + return out; +} + + +// EXPORTS // + +module.exports = reverse; diff --git a/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/package.json b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/package.json new file mode 100644 index 00000000000..dd9070a92e3 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/string/base/reverse-grapheme-clusters", + "version": "0.0.0", + "description": "Reverse the grapheme clusters (i.e., user-perceived characters) of a string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdstring", + "utilities", + "utility", + "utils", + "util", + "string", + "str", + "base", + "reverse", + "character", + "char", + "grapheme", + "cluster", + "unicode" + ] +} diff --git a/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/test/test.js b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/test/test.js new file mode 100644 index 00000000000..564d45a2e11 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse-grapheme-clusters/test/test.js @@ -0,0 +1,77 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 tape = require( 'tape' ); +var reverse = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof reverse, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an empty string if provided an empty string', function test( t ) { + t.strictEqual( reverse( '' ), '', 'returns expected value' ); + t.end(); +}); + +tape( 'the function reverses the grapheme clusters of a provided string (ascii)', function test( t ) { + var out; + + out = reverse( 'hello world' ); + t.strictEqual( out, 'dlrow olleh', 'returns expected value' ); + + out = reverse( '!!!*' ); + t.strictEqual( out, '*!!!', 'returns expected value' ); + + out = reverse( 'Hello World' ); + t.strictEqual( out, 'dlroW olleH', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function reverses the grapheme clusters of a provided string (Unicode)', function test( t ) { + var out; + + out = reverse( 'अनुच्छेद', 1 ); + t.strictEqual( out, 'दछेच्नुअ', 'returns expected value' ); + + out = reverse( '六书/六書', 1 ); + t.strictEqual( out, '書六/书六', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function reverses the grapheme clusters of a provided string (emoji)', function test( t ) { + var out; + + out = reverse( '🌷', 1 ); + t.strictEqual( out, '🌷', 'returns expected value' ); + + out = reverse( '🏝️🌷', 1 ); + t.strictEqual( out, '🌷🏝️', 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/string/base/reverse/README.md b/lib/node_modules/@stdlib/string/base/reverse/README.md new file mode 100644 index 00000000000..466cf4bc5f8 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse/README.md @@ -0,0 +1,89 @@ + + +# reverse + +> reverse the UTF-16 code units of a string. + +
+ +## Usage + +```javascript +var reverse = require( '@stdlib/string/base/reverse' ); +``` + +#### reverse( str ) + +Reverses the UTF-16 code units of a string. + +```javascript +var out = reverse( 'last man standing' ); +// returns 'gnidnats nam tsal' + +out = reverse( 'Hidden Treasures' ); +// returns 'serusaerT neddiH' + +out = reverse( 'foo bar' ); +// returns 'rab oof' +``` + +
+ + + +
+ +## Examples + + + +```javascript +var reverse = require( '@stdlib/string/base/reverse' ); + +var str = reverse( 'presidential election' ); +// returns 'noitcele laitnediserp' + +str = reverse( 'JavaScript' ); +// returns 'tpircSavaJ' + +str = reverse( 'The Last of the Mohicans' ); +// returns 'snacihoM eht fo tsaL ehT' +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/reverse/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/reverse/benchmark/benchmark.js new file mode 100644 index 00000000000..8f45bcfe87b --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse/benchmark/benchmark.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 reverse = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var out; + var i; + + values = [ + 'beep boop', + 'foo bar', + 'xyz abc', + '🐶🐮🐷🐰🐸' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = reverse( values[ i%values.length ] ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/string/base/reverse/docs/repl.txt b/lib/node_modules/@stdlib/string/base/reverse/docs/repl.txt new file mode 100644 index 00000000000..0bac948a953 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse/docs/repl.txt @@ -0,0 +1,26 @@ + +{{alias}}( str ) + Reverses the UTF-16 code units of a string. + + Parameters + ---------- + str: string + Input string. + + Returns + ------- + out: string + Output string. + + Examples + -------- + > var out = {{alias}}( 'beep' ) + 'peeb' + > out = {{alias}}( 'Boop' ) + 'pooB' + > out = {{alias}}( 'foo bar' ) + 'rab oof' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/string/base/reverse/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/reverse/docs/types/index.d.ts new file mode 100644 index 00000000000..43d3f4e7612 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse/docs/types/index.d.ts @@ -0,0 +1,52 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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: 2.0 + +/** +* Reverses the UTF-16 code units of a string. +* +* @param str - input string +* @returns output string +* +* @example +* var out = reverse( 'last man standing' ); +* // returns 'gnidnats nam tsal' +* +* @example +* var out = reverse( 'presidential election' ); +* // returns 'noitcele laitnediserp' +* +* @example +* var out = reverse( 'JavaScript' ); +* // returns 'tpircSavaJ' +* +* @example +* var out = reverse( 'Hidden Treasures' ); +* // returns 'serusaerT neddiH' +* +* @example +* var out = reverse( 'foo bar' ); +* // returns 'rab oof' +*/ +declare function reverse( str: string ): string; + + +// EXPORTS // + +export = reverse; diff --git a/lib/node_modules/@stdlib/string/base/reverse/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/reverse/docs/types/test.ts new file mode 100644 index 00000000000..116ee34cdec --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse/docs/types/test.ts @@ -0,0 +1,46 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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 reverse = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + reverse( 'abc' ); // $ExpectType string +} + +// The compiler throws an error if the function is provided a value other than a string... +{ + reverse( true ); // $ExpectError + reverse( false ); // $ExpectError + reverse( null ); // $ExpectError + reverse( undefined ); // $ExpectError + reverse( 5 ); // $ExpectError + reverse( [] ); // $ExpectError + reverse( {} ); // $ExpectError + reverse( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + reverse(); // $ExpectError + reverse( 'abc', 1 ); // $ExpectError + reverse( 'abc', 1, 2 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/base/reverse/examples/index.js b/lib/node_modules/@stdlib/string/base/reverse/examples/index.js new file mode 100644 index 00000000000..1c473d63bb1 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse/examples/index.js @@ -0,0 +1,30 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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'; + +var reverse = require( './../lib' ); + +console.log( reverse( 'presidential election' ) ); +// => 'noitcele laitnediserp' + +console.log( reverse( 'JavaScript' ) ); +// => 'tpircSavaJ' + +console.log( reverse( 'The Last of the Mohicans' ) ); +// => 'snacihoM eht fo tsaL ehT' diff --git a/lib/node_modules/@stdlib/string/base/reverse/lib/index.js b/lib/node_modules/@stdlib/string/base/reverse/lib/index.js new file mode 100644 index 00000000000..f5e7c1fe8f8 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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'; + +/** +* Reverse the UTF-16 code units of a string. +* +* @module @stdlib/string/base/reverse +* +* @example +* var reverse = require( '@stdlib/string/base/reverse' ); +* +* var out = reverse( 'last man standing' ); +* // returns 'gnidnats nam tsal' +* +* out = reverse( 'Hidden Treasures' ); +* // returns 'serusaerT neddiH'; +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/base/reverse/lib/main.js b/lib/node_modules/@stdlib/string/base/reverse/lib/main.js new file mode 100644 index 00000000000..f8e3dcd7e08 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse/lib/main.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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'; + +// MAIN // + +/** +* Reverses the UTF-16 code units of a string. +* +* @param {string} str - input string +* @returns {string} output string +* +* @example +* var out = reverse( 'last man standing' ); +* // returns 'gnidnats nam tsal' +* +* @example +* var out = reverse( 'presidential election' ); +* // returns 'noitcele laitnediserp' +* +* @example +* var out = reverse( 'JavaScript' ); +* // returns 'tpircSavaJ' +* +* @example +* var out = reverse( 'Hidden Treasures' ); +* // returns 'serusaerT neddiH' +*/ +function reverse( str ) { + var out; + var i; + + out = ''; + for ( i = str.length - 1; i >= 0; i-- ) { + out += str[ i ]; + } + return out; +} + + +// EXPORTS // + +module.exports = reverse; diff --git a/lib/node_modules/@stdlib/string/base/reverse/package.json b/lib/node_modules/@stdlib/string/base/reverse/package.json new file mode 100644 index 00000000000..41b2e85fb05 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/string/base/reverse", + "version": "0.0.0", + "description": "Reverse the UTF-16 code units of a string.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdstring", + "utilities", + "utility", + "utils", + "util", + "string", + "str", + "base", + "reverse", + "character", + "char", + "codeunit", + "unicode" + ] +} diff --git a/lib/node_modules/@stdlib/string/base/reverse/test/test.js b/lib/node_modules/@stdlib/string/base/reverse/test/test.js new file mode 100644 index 00000000000..51934d51a75 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/reverse/test/test.js @@ -0,0 +1,53 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 tape = require( 'tape' ); +var reverse = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof reverse, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an empty string if provided an empty string', function test( t ) { + t.strictEqual( reverse( '' ), '', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns reversed UTF-16 code units of the string', function test( t ) { + var out; + + out = reverse( 'hello world' ); + t.strictEqual( out, 'dlrow olleh', 'returns expected value' ); + + out = reverse( '!!!*' ); + t.strictEqual( out, '*!!!', 'returns expected value' ); + + out = reverse( 'Hello World' ); + t.strictEqual( out, 'dlroW olleH', 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/string/reverse/README.md b/lib/node_modules/@stdlib/string/reverse/README.md index ad7255a1412..a95a9fae4db 100644 --- a/lib/node_modules/@stdlib/string/reverse/README.md +++ b/lib/node_modules/@stdlib/string/reverse/README.md @@ -30,7 +30,7 @@ limitations under the License. var reverseString = require( '@stdlib/string/reverse' ); ``` -#### reverseString( str ) +#### reverseString( str\[, options] ) Reverses a `string`. @@ -45,12 +45,28 @@ out = reverseString( 'Lorem ipsum 𝌆 dolor sit ameͨ͆t.' ); // returns '.teͨ͆ma tis rolod 𝌆 muspi meroL' ``` +The function supports the following options: + +- **mode**: type of characters to reverse. Must be one of the following: + + - `'grapheme'`: grapheme clusters. Appropriate for strings containing visual characters which can span multiple Unicode code points (e.g., emoji). + - `'code_point'`: Unicode code points. Appropriate for strings containing visual characters which are comprised of more than one Unicode code unit (e.g., ideographic symbols and punctuation and mathematical alphanumerics). + - `'code_unit'`: UTF-16 code units. Appropriate for strings containing visual characters drawn from the basic multilingual plane (BMP) (e.g., common characters, such as those from the Latin, Greek, and Cyrillic alphabets). + + Default: `'grapheme'`. + + +
+## Notes + +- By default, the function assumes the general case in which an input string may contain an arbitrary number of grapheme clusters. This assumption comes with a performance cost. Accordingly, if an input string is known to only contain visual characters of a particular type (e.g., only alphanumeric), one can achieve better performance by specifying the appropriate `mode` option. +
@@ -99,6 +115,7 @@ Options: -h, --help Print this message. -V, --version Print the package version. --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. + --mode mode Type of character to return. Default: 'grapheme'. ``` diff --git a/lib/node_modules/@stdlib/string/reverse/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/reverse/benchmark/benchmark.js index 9ecfea55aed..6de65042d3d 100644 --- a/lib/node_modules/@stdlib/string/reverse/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/string/reverse/benchmark/benchmark.js @@ -49,3 +49,93 @@ bench( pkg, function benchmark( b ) { b.pass( 'benchmark finished' ); b.end(); }); + +bench( pkg+':mode=grapheme', function benchmark( b ) { + var values; + var opts; + var out; + var i; + + values = [ + 'beep boop', + 'foo bar', + 'xyz abc' + ]; + opts = { + 'mode': 'grapheme' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = reverseString( values[ i%values.length ], opts ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':mode=code_point', function benchmark( b ) { + var values; + var opts; + var out; + var i; + + values = [ + 'beep boop', + 'foo bar', + 'xyz abc' + ]; + opts = { + 'mode': 'code_point' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = reverseString( values[ i%values.length ], opts ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':mode=code_unit', function benchmark( b ) { + var values; + var opts; + var out; + var i; + + values = [ + 'beep boop', + 'foo bar', + 'xyz abc' + ]; + opts = { + 'mode': 'code_unit' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = reverseString( values[ i%values.length ], opts ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/string/reverse/bin/cli b/lib/node_modules/@stdlib/string/reverse/bin/cli index f7ba6a5d593..c96be860bfb 100755 --- a/lib/node_modules/@stdlib/string/reverse/bin/cli +++ b/lib/node_modules/@stdlib/string/reverse/bin/cli @@ -45,6 +45,7 @@ function main() { var split; var flags; var args; + var opts; var cli; // Create a command-line interface: @@ -61,6 +62,10 @@ function main() { if ( flags.help || flags.version ) { return; } + opts = {}; + if ( flags.mode ) { + opts.mode = flags.mode; + } // Get any provided command-line arguments: args = cli.args(); @@ -77,7 +82,11 @@ function main() { } return stdin( onRead ); } - console.log( reverse( args[ 0 ] ) ); // eslint-disable-line no-console + try { + console.log( reverse( args[ 0 ], opts ) ); // eslint-disable-line no-console + } catch ( error ) { + return cli.error( error ); + } /** * Callback invoked upon reading from `stdin`. @@ -99,8 +108,15 @@ function main() { if ( lines[ lines.length-1 ] === '' ) { lines.pop(); } - for ( i = 0; i < lines.length; i++ ) { - console.log( reverse( lines[ i ] ) ); // eslint-disable-line no-console + if ( lines.length ) { + try { + console.log( reverse( lines[ 0 ], opts ) ); // eslint-disable-line no-console + } catch ( error ) { + return cli.error( error ); + } + for ( i = 1; i < lines.length; i++ ) { + console.log( reverse( lines[ i ], opts ) ); // eslint-disable-line no-console + } } } } diff --git a/lib/node_modules/@stdlib/string/reverse/docs/repl.txt b/lib/node_modules/@stdlib/string/reverse/docs/repl.txt index 8de69b78d81..06ec709581f 100644 --- a/lib/node_modules/@stdlib/string/reverse/docs/repl.txt +++ b/lib/node_modules/@stdlib/string/reverse/docs/repl.txt @@ -1,12 +1,31 @@ -{{alias}}( str ) - Reverses a `string`. +{{alias}}( str[, options] ) + Reverses a string. Parameters ---------- str: string Input string. + options: Object (optional) + Options. + + options.mode: string (optional) + Type of characters to reverse. The following modes are supported: + + - grapheme: grapheme clusters. Appropriate for strings containing visual + characters which can span multiple Unicode code points (e.g., emoji). + - code_point: Unicode code points. Appropriate for strings containing + visual characters which are comprised of more than one Unicode code + unit (e.g., ideographic symbols and punctuation and mathematical + alphanumerics). + - code_unit': UTF-16 code units. Appropriate for strings containing + visual characters drawn from the basic multilingual plane (BMP) (e.g., + common characters, such as those from the Latin, Greek, and Cyrillic + alphabets). + + Default: 'grapheme'. + Returns ------- out: string diff --git a/lib/node_modules/@stdlib/string/reverse/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/reverse/docs/types/index.d.ts index 008230d3011..add71c37489 100644 --- a/lib/node_modules/@stdlib/string/reverse/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/string/reverse/docs/types/index.d.ts @@ -18,29 +18,44 @@ // TypeScript Version: 2.0 +/** +* Interface describing function options. +*/ +interface Options { + /** + * Specifies the type of characters to reverse (default: 'grapheme'). + * + * ## Notes + * + * - The following modes are supported: + * + * - `'grapheme'`: grapheme clusters. Appropriate for strings containing visual characters which can span multiple Unicode code points (e.g., emoji). + * - `'code_point'`: Unicode code points. Appropriate for strings containing visual characters which are comprised of more than one Unicode code unit (e.g., ideographic symbols and punctuation and mathematical alphanumerics). + * - `'code_unit'`: UTF-16 code units. Appropriate for strings containing visual characters drawn from the basic multilingual plane (BMP) (e.g., common characters, such as those from the Latin, Greek, and Cyrillic alphabets). + */ + mode?: 'grapheme' | 'code_point' | 'code_unit'; +} + /** * Reverses a string. * * @param str - input string +* @param options - options * @returns reversed string * * @example -* var out = reverseString( 'last man standing' ); +* var out = reverseString( 'last man standing', { +* 'mode': 'code_unit' +* }); * // returns 'gnidnats nam tsal' * * @example -* var out = reverseString( 'presidential election' ); -* // returns 'noitcele laitnediserp' -* -* @example -* var out = reverseString( 'javaScript' ); -* // returns 'tpircSavaj' -* -* @example -* var out = reverseString( 'Hidden Treasures' ); -* // returns 'serusaerT neddiH' +* var out = reverseString( '🐶🐮🐷🐰🐸', { +* 'mode': 'grapheme' +* }); +* // returns '🐸🐰🐷🐮🐶' */ -declare function reverseString( str: string ): string; +declare function reverseString( str: string, options?: Options ): string; // EXPORTS // diff --git a/lib/node_modules/@stdlib/string/reverse/docs/types/test.ts b/lib/node_modules/@stdlib/string/reverse/docs/types/test.ts index b00c8a9c20c..888c2d1341f 100644 --- a/lib/node_modules/@stdlib/string/reverse/docs/types/test.ts +++ b/lib/node_modules/@stdlib/string/reverse/docs/types/test.ts @@ -36,6 +36,37 @@ import reverseString = require( './index' ); reverseString( [] ); // $ExpectError reverseString( {} ); // $ExpectError reverseString( ( x: number ): number => x ); // $ExpectError + + reverseString( true, {} ); // $ExpectError + reverseString( false, {} ); // $ExpectError + reverseString( null, {} ); // $ExpectError + reverseString( undefined, {} ); // $ExpectError + reverseString( 5, {} ); // $ExpectError + reverseString( [], {} ); // $ExpectError + reverseString( {}, {} ); // $ExpectError + reverseString( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a value other than an object as second argument... +{ + reverseString( 'abc', '1' ); // $ExpectError + reverseString( 'abc', 1 ); // $ExpectError + reverseString( 'abc', true ); // $ExpectError + reverseString( 'abc', false ); // $ExpectError + reverseString( 'abc', null ); // $ExpectError + reverseString( 'abc', '' ); // $ExpectError + reverseString( 'abc', [] ); // $ExpectError + reverseString( 'abc', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `mode` option... +{ + reverseString( 'abc', { 'mode': true } ); // $ExpectError + reverseString( 'abc', { 'mode': false } ); // $ExpectError + reverseString( 'abc', { 'mode': null } ); // $ExpectError + reverseString( 'abc', { 'mode': '' } ); // $ExpectError + reverseString( 'abc', { 'mode': [] } ); // $ExpectError + reverseString( 'abc', { 'mode': ( x: number ): number => x } ); // $ExpectError } // The compiler throws an error if the function is provided insufficient arguments... diff --git a/lib/node_modules/@stdlib/string/reverse/docs/usage.txt b/lib/node_modules/@stdlib/string/reverse/docs/usage.txt index c6a6e7fa694..b0a676239ca 100644 --- a/lib/node_modules/@stdlib/string/reverse/docs/usage.txt +++ b/lib/node_modules/@stdlib/string/reverse/docs/usage.txt @@ -6,3 +6,4 @@ Options: -h, --help Print this message. -V, --version Print the package version. --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. + --mode mode Type of character to reverse. Default: 'grapheme'. diff --git a/lib/node_modules/@stdlib/string/reverse/etc/cli_opts.json b/lib/node_modules/@stdlib/string/reverse/etc/cli_opts.json index 7c40f9a10cc..b090761664d 100644 --- a/lib/node_modules/@stdlib/string/reverse/etc/cli_opts.json +++ b/lib/node_modules/@stdlib/string/reverse/etc/cli_opts.json @@ -1,6 +1,7 @@ { "string": [ - "split" + "split", + "mode" ], "boolean": [ "help", diff --git a/lib/node_modules/@stdlib/string/reverse/lib/main.js b/lib/node_modules/@stdlib/string/reverse/lib/main.js index a257ecb8e57..a61f598ac3f 100644 --- a/lib/node_modules/@stdlib/string/reverse/lib/main.js +++ b/lib/node_modules/@stdlib/string/reverse/lib/main.js @@ -20,18 +20,38 @@ // MODULES // -var prevGraphemeClusterBreak = require( '@stdlib/string/prev-grapheme-cluster-break' ); var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var contains = require( '@stdlib/array/base/assert/contains' ).factory; +var reverseCodeUnit = require( '@stdlib/string/base/reverse' ); +var reverseCodePoints = require( '@stdlib/string/base/reverse-code-points' ); +var reverseGraphemeClusters = require( '@stdlib/string/base/reverse-grapheme-clusters' ); var format = require( '@stdlib/string/format' ); +// VARIABLES // + +var MODES = [ 'grapheme', 'code_point', 'code_unit' ]; +var FCNS = { + 'grapheme': reverseGraphemeClusters, + 'code_point': reverseCodePoints, + 'code_unit': reverseCodeUnit +}; +var isMode = contains( MODES ); + + // MAIN // /** * Reverses a string. * * @param {string} str - input string +* @param {Options} [options] - options +* @param {string} [options.mode="grapheme"] - type of "character" to return (must be either `grapheme`, `code_point`, or `code_unit`) * @throws {TypeError} must provide a string primitive +* @throws {TypeError} options argument must be an object +* @throws {TypeError} must provide valid options * @returns {string} reversed string * * @example @@ -51,27 +71,32 @@ var format = require( '@stdlib/string/format' ); * // returns 'serusaerT neddiH' */ function reverse( str ) { - var out; - var brk; - var idx; - var i; + var options; + var nargs; + var opts; + if ( !isString( str ) ) { throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); } - if ( str === '' ) { - return ''; + opts = { + 'mode': 'grapheme' + }; + nargs = arguments.length; + if ( nargs > 1 ) { + options = arguments[ 1 ]; + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } } - - out = []; - idx = str.length - 1; - while ( idx >= 0 ) { - brk = prevGraphemeClusterBreak( str, idx ); - for ( i = brk + 1; i <= idx; i++ ) { - out.push( str.charAt( i ) ); + if ( options ) { + if ( hasOwnProp( options, 'mode' ) ) { + opts.mode = options.mode; + if ( !isMode( opts.mode ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Value: `%s`.', 'mode', MODES.join( '", "' ), opts.mode ) ); + } } - idx = brk; } - return out.join( '' ); + return FCNS[ opts.mode ]( str ); } diff --git a/lib/node_modules/@stdlib/string/reverse/test/test.cli.js b/lib/node_modules/@stdlib/string/reverse/test/test.cli.js index 8c854eb4fc8..394ca6f99cc 100644 --- a/lib/node_modules/@stdlib/string/reverse/test/test.cli.js +++ b/lib/node_modules/@stdlib/string/reverse/test/test.cli.js @@ -162,6 +162,46 @@ tape( 'the command-line interface reverses a string argument', opts, function te } }); +tape( 'the command-line interface supports specifying the type of characters to reverse', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'beep\'; process.argv[ 3 ] = \'--mode=code_point\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'peeb\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'if provided an invalid option, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'beep\'; process.argv[ 3 ] = \'--mode=foo\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString().length > 0, true, 'expected value' ); + t.end(); + } +}); + tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { var cmd = [ 'printf "foobar\nlast man standing"', diff --git a/lib/node_modules/@stdlib/string/reverse/test/test.js b/lib/node_modules/@stdlib/string/reverse/test/test.js index 950bd47087b..9d1debfc52d 100644 --- a/lib/node_modules/@stdlib/string/reverse/test/test.js +++ b/lib/node_modules/@stdlib/string/reverse/test/test.js @@ -59,8 +59,92 @@ tape( 'the function throws an error if not provided a string', function test( t } }); +tape( 'the function throws an error if not provided a string (options)', function test( t ) { + var values; + var i; + + values = [ + 5, + null, + true, + void 0, + NaN, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + reverseString( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var i; + + values = [ + 'abc', + 3, + null, + true, + void 0, + NaN, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + reverseString( 'beep', value ); + }; + } +}); + +tape( 'the function throws an error if provided a `mode` option which is not a supported mode', function test( t ) { + var values; + var i; + + values = [ + 'abc', + 3, + null, + true, + void 0, + NaN, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + reverseString( 'beep', { + 'mode': value + }); + }; + } +}); + tape( 'the function returns an empty string if provided an empty string', function test( t ) { t.strictEqual( reverseString( '' ), '', 'returns expected value' ); + t.strictEqual( reverseString( '', {} ), '', 'returns expected value' ); t.end(); }); @@ -116,3 +200,82 @@ tape( 'the function reverses a string', function test( t ) { } t.end(); }); + +tape( 'the function reverses the first string (mode=grapheme)', function test( t ) { + var opts; + var out; + + opts = { + 'mode': 'grapheme' + }; + + out = reverseString( 'hello world', opts ); + t.strictEqual( out, 'dlrow olleh', 'returns expected value' ); + + out = reverseString( '*!!!', opts ); + t.strictEqual( out, '!!!*', 'returns expected value' ); + + out = reverseString( 'अनुच्छेद', opts ); + t.strictEqual( out, 'दछेच्नुअ', 'returns expected value' ); + + out = reverseString( '六书/六書', opts ); + t.strictEqual( out, '書六/书六', 'returns expected value' ); + + out = reverseString( '🌷', opts ); + t.strictEqual( out, '🌷', 'returns expected value' ); + + out = reverseString( '👉🏿🌷', opts ); + t.strictEqual( out, '🌷👉🏿', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function reverses the first string (mode=code_point)', function test( t ) { + var opts; + var out; + + opts = { + 'mode': 'code_point' + }; + + out = reverseString( 'hello world', opts ); + t.strictEqual( out, 'dlrow olleh', 'returns expected value' ); + + out = reverseString( '*!!!', opts ); + t.strictEqual( out, '!!!*', 'returns expected value' ); + + out = reverseString( 'अनुच्छेद', opts ); + t.strictEqual( out, 'देछ्चुनअ', 'returns expected value' ); + + out = reverseString( '六书/六書', opts ); + t.strictEqual( out, '書六/书六', 'returns expected value' ); + + out = reverseString( '🌷', opts ); + t.strictEqual( out, '🌷', 'returns expected value' ); + t.end(); +}); + +tape( 'the function reverses the first string (mode=code_unit)', function test( t ) { + var opts; + var out; + + opts = { + 'mode': 'code_unit' + }; + + out = reverseString( 'hello world', opts ); + t.strictEqual( out, 'dlrow olleh', 'returns expected value' ); + + out = reverseString( '*!!!', opts ); + t.strictEqual( out, '!!!*', 'returns expected value' ); + + out = reverseString( 'अनुच्छेद', opts ); + t.strictEqual( out, 'देछ्चुनअ', 'returns expected value' ); + + out = reverseString( '六书/六書', opts ); + t.strictEqual( out, '書六/书六', 'returns expected value' ); + + out = reverseString( '🌷', opts ); + t.strictEqual( out, '\udf37\ud83c', 'returns expected value' ); + t.end(); +});