diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/README.md b/lib/node_modules/@stdlib/string/next-code-point-index/README.md new file mode 100644 index 000000000000..970341192f4f --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/README.md @@ -0,0 +1,193 @@ + + +# nextCodePointIndex + +> Return the position of the next Unicode code point in a string after a specified position. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var nextCodePointIndex = require( '@stdlib/string/next-code-point-index' ); +``` + +#### nextCodePointIndex( string\[, fromIndex] ) + +Returns the position of the next Unicode code point in a string after a specified position. + +```javascript +var out = nextCodePointIndex( 'last man standing' ); +// returns 1 +``` + +By default, the function searches for a Unicode code point starting from the first index. To specify an alternative starting search index, provide a `fromIndex` argument. + +```javascript +var out = nextCodePointIndex( 'last man standing', 4 ); +// returns 5 +``` + +
+ + + + + +
+ +## Notes + +- If `string` is an empty string, the function returns `-1` irrespective of `fromIndex`. +- If a code point does not exist after `fromIndex`, the function returns `-1`. +- Note that `fromIndex` does **not** refer to a visual character position, but to an index in the ordered sequence of [UTF-16][utf-16] code units. + +
+ + + + + +
+ +## Examples + + + +```javascript +var nextCodePointIndex = require( '@stdlib/string/next-code-point-index' ); + +var out = nextCodePointIndex( 'last man standing', 4 ); +// returns 5 + +out = nextCodePointIndex( 'presidential election', 8 ); +// returns 9 + +out = nextCodePointIndex( '𐒻𐓟𐒻𐓟', 0 ); +// returns 2 + +out = nextCodePointIndex( '🌷', 0 ); +// returns -1 +``` + +
+ + + + + +* * * + +
+ +## CLI + + + +
+ +### Usage + +```text +Usage: next-code-point-index [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --from index Starting search position in string. Default: 0. +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```bash +$ next-code-point-index --from=0 𐒻𐓟𐒻𐓟 +2 +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n '𐒻𐓟𐒻𐓟' | next-code-point-index --from=0 +2 +``` + +
+ + + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/next-code-point-index/benchmark/benchmark.js new file mode 100644 index 000000000000..1eedf93a09db --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/benchmark/benchmark.js @@ -0,0 +1,64 @@ +/** +* @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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var nextCodePointIndex = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var strings; + var len; + var out; + var i; + + strings = [ + 'last man standing', + 'presidential election', + 'अनुच्छेद', + '🌷', + '书/六書', + 'เ❄︎நி', + 'กิิก้้ก็็ก็็กิิก้้ก็็กิิก้้กิิก้้ก็็ก็็กิิก้้ก็็กิิก้้', + '書六/书六', + 'ܶƔλʃݖͱšɕ҆ʧѸؐҜҦɳΏ', + 'âݝΝ‚ҳӌݾҀƳ۵ۧ޳ǁǸΓ' + ]; + len = strings.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = nextCodePointIndex( strings[ i%len ], 1 ); + if ( out !== out ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isInteger( out ) ) { + b.fail( 'should return an integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/bin/cli b/lib/node_modules/@stdlib/string/next-code-point-index/bin/cli new file mode 100755 index 000000000000..dcc36981b8d1 --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/bin/cli @@ -0,0 +1,91 @@ +#!/usr/bin/env node + +/** +* @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 resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/cli/ctor' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var nextCodePointIndex = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var args; + var cli; + var pos; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + if ( flags.from ) { + pos = parseInt( flags.from, 10 ); + } else { + pos = 0; + } + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( stdinStream.isTTY ) { + return console.log( nextCodePointIndex( args[ 0 ], pos ) ); // eslint-disable-line no-console + } + return stdin( onRead ); + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + if ( error ) { + return cli.error( error ); + } + console.log( nextCodePointIndex( data.toString(), pos ) ); // eslint-disable-line no-console + } +} + +main(); diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/docs/repl.txt b/lib/node_modules/@stdlib/string/next-code-point-index/docs/repl.txt new file mode 100644 index 000000000000..15203eb6068a --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( str[, fromIndex] ) + Returns the position of the next Unicode code point in a string after a + specified position. + + Parameters + ---------- + str: string + Input string. + + fromIndex: integer (optional) + Position. Default: 0. + + Returns + ------- + out: integer + Next code point position. + + Examples + -------- + > var out = {{alias}}( 'last man standing', 4 ) + 5 + > out = {{alias}}( 'presidential election', 8 ) + 9 + > out = {{alias}}( '𐒻𐓟𐒻𐓟', 0 ) + 2 + > out = {{alias}}( '🌷' ) + -1 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/next-code-point-index/docs/types/index.d.ts new file mode 100644 index 000000000000..986bcf2539d0 --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @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: 4.1 + +/** +* Returns the position of the next Unicode code point in a string after a specified position. +* +* @param str - input string +* @param fromIndex - position (default: 0) +* @throws first argument must be a string +* @throws second argument must be an integer +* @returns next code point position +* +* @example +* var out = nextCodePointIndex( '𐒻𐓟𐒻𐓟', 0 ); +* // returns 2 +* +* out = nextCodePointIndex( '🌷' ); +* // returns -1 +*/ +declare function nextCodePointIndex( str: string, fromIndex?: number ): number; + + +// EXPORTS // + +export = nextCodePointIndex; diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/docs/types/test.ts b/lib/node_modules/@stdlib/string/next-code-point-index/docs/types/test.ts new file mode 100644 index 000000000000..cf9a41076e9d --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/docs/types/test.ts @@ -0,0 +1,47 @@ +/* +* @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 nextCodePointIndex = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + nextCodePointIndex( 'last man standing', 4 ); // $ExpectType number + nextCodePointIndex( 'presidential election', 8 ); // $ExpectType number + nextCodePointIndex( 'अनुच्छेद', 1 ); // $ExpectType number + nextCodePointIndex( '🌷', 0 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided invalid arguments... +{ + nextCodePointIndex( false, 3 ); // $ExpectError + nextCodePointIndex( {}, 3 ); // $ExpectError + nextCodePointIndex( ( x: number ): number => x, 3 ); // $ExpectError + + nextCodePointIndex( 'string', true ); // $ExpectError + nextCodePointIndex( 'string', false ); // $ExpectError + nextCodePointIndex( 'string', {} ); // $ExpectError + nextCodePointIndex( 'string', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + nextCodePointIndex(); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/docs/usage.txt b/lib/node_modules/@stdlib/string/next-code-point-index/docs/usage.txt new file mode 100644 index 000000000000..63c838c6e2a3 --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: next-code-point-index [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --from index Starting search position in string. Default: 0. diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/etc/cli_opts.json b/lib/node_modules/@stdlib/string/next-code-point-index/etc/cli_opts.json new file mode 100644 index 000000000000..8a766dc0ecdb --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/etc/cli_opts.json @@ -0,0 +1,17 @@ +{ + "string": [ + "from" + ], + "boolean": [ + "help", + "version" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/examples/index.js b/lib/node_modules/@stdlib/string/next-code-point-index/examples/index.js new file mode 100644 index 000000000000..a034f6f7b746 --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/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 nextCodePointIndex = require( './../lib' ); + +console.log( nextCodePointIndex( 'last man standing', 4 ) ); +// => 5 + +console.log( nextCodePointIndex( 'presidential election', 8 ) ); +// => 9 + +console.log( nextCodePointIndex( '𐒻𐓟𐒻𐓟', 0 ) ); +// => 2 + +console.log( nextCodePointIndex( '🌷', 0 ) ); +// => -1 diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/lib/index.js b/lib/node_modules/@stdlib/string/next-code-point-index/lib/index.js new file mode 100644 index 000000000000..716736f8ac9c --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/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'; + +/** +* Return the position of the next Unicode code point in a string after a specified position. +* +* @module @stdlib/string/next-code-point-index +* +* @example +* var nextCodePointIndex = require( '@stdlib/string/next-code-point-index' ); +* +* var out = nextCodePointIndex( '𐒻𐓟𐒻𐓟', 0 ); +* // returns 2 +* +* out = nextCodePointIndex( '🌷', 0 ); +* // returns -1 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/lib/main.js b/lib/node_modules/@stdlib/string/next-code-point-index/lib/main.js new file mode 100644 index 000000000000..d0ffffc10860 --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/lib/main.js @@ -0,0 +1,108 @@ +/** +* @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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var format = require( '@stdlib/string/format' ); + + +// 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 // + +/** +* Returns the position of the next Unicode code point in a string after a specified position. +* +* @param {string} str - input string +* @param {integer} [fromIndex=0] - position +* @throws {TypeError} first argument must be a string +* @throws {TypeError} second argument must be an integer +* @returns {integer} position of the next Unicode code point +* +* @example +* var out = nextCodePointIndex( 'last man standing', 4 ); +* // returns 5 +* +* @example +* var out = nextCodePointIndex( 'presidential election', 8 ); +* // returns 9 +* +* @example +* var out = nextCodePointIndex( '𐒻𐓟𐒻𐓟', 0 ); +* // returns 2 +* +* @example +* var out = nextCodePointIndex( '🌷' ); +* // returns -1 +*/ +function nextCodePointIndex( str, fromIndex ) { + var lastIndex; + var len; + var idx; + var i; + var j; + + if ( !isString( str ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); + } + if ( arguments.length > 1 ) { + if ( !isInteger( fromIndex ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) ); + } + idx = fromIndex; + } else { + idx = 0; + } + len = str.length; + if ( idx < 0 ) { + idx += len; + if ( idx < 0 ) { + idx = 0; + } + } + lastIndex = len - 1; + if ( idx >= lastIndex ) { + return -1; + } + + // Check for a high UTF-16 surrogate... + i = idx + 1; + j = i + 1; + if ( RE_UTF16_HIGH_SURROGATE.test( str[ idx ] ) ) { + // Check whether the high surrogate is paired with a low surrogate... + if ( RE_UTF16_LOW_SURROGATE.test( str[ i ] ) ) { + // We found a surrogate pair: + return ( j >= lastIndex ) ? -1 : j; + } + return i; + } + return i; +} + + +// EXPORTS // + +module.exports = nextCodePointIndex; diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/package.json b/lib/node_modules/@stdlib/string/next-code-point-index/package.json new file mode 100644 index 000000000000..baa059e922b3 --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/string/next-code-point-index", + "version": "0.0.0", + "description": "Return the position of the next Unicode code point in a string after a specified position.", + "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" + } + ], + "bin": { + "next-code-point-index": "./bin/cli" + }, + "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", + "string", + "str", + "utilities", + "utility", + "utils", + "util", + "code", + "point", + "pt", + "unicode", + "utf-16", + "utf16", + "surrogate", + "astral", + "segmentation", + "tc39" + ] +} diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/test/fixtures/stdin_error.js.txt b/lib/node_modules/@stdlib/string/next-code-point-index/test/fixtures/stdin_error.js.txt new file mode 100644 index 000000000000..b42c70b3b554 --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/test/fixtures/stdin_error.js.txt @@ -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. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/test/test.cli.js b/lib/node_modules/@stdlib/string/next-code-point-index/test/test.cli.js new file mode 100644 index 000000000000..964a7a258e7a --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/test/test.cli.js @@ -0,0 +1,260 @@ +/** +* @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 resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var EXEC_PATH = require( '@stdlib/process/exec-path' ); + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface returns the next code point break position in a string', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'--from=1\'; process.argv[ 3 ] = \'अनुच्छेद\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '2\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports negative integers when specifying a starting search index', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'--from=-7\'; process.argv[ 3 ] = \'अनुच्छेद\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '2\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface uses a default starting search index of `0`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'नुच्छेद\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '1\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf \'अनुच्छेद\'', + '|', + EXEC_PATH, + fpath, + '--from=1' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '2\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, 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(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/lib/node_modules/@stdlib/string/next-code-point-index/test/test.js b/lib/node_modules/@stdlib/string/next-code-point-index/test/test.js new file mode 100644 index 000000000000..7a95ca27c23a --- /dev/null +++ b/lib/node_modules/@stdlib/string/next-code-point-index/test/test.js @@ -0,0 +1,242 @@ +/** +* @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 nextCodePointIndex = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof nextCodePointIndex, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if the first argument is not a string (one argument)', function test( t ) { + var values; + var i; + + values = [ + -5, + 3.14, + -1.0, + NaN, + true, + false, + null, + void 0, + [ 'beep', 'boop' ], + [ 1, 2, 3 ], + {}, + 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() { + nextCodePointIndex( value ); + }; + } +}); + +tape( 'the function throws an error if the first argument is not a string (two arguments)', function test( t ) { + var values; + var i; + + values = [ + -5, + 3.14, + -1.0, + NaN, + true, + false, + null, + void 0, + [ 'beep', 'boop' ], + [ 1, 2, 3 ], + {}, + 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() { + nextCodePointIndex( value, 0 ); + }; + } +}); + +tape( 'the function throws an error if the second argument is not an integer', function test( t ) { + var values; + var i; + + values = [ + 'bar', + 3.14, + NaN, + null, + void 0, + [ 'beep', 'boop' ], + [ 1, 2, 3 ], + {}, + 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() { + nextCodePointIndex( 'foo', value ); + }; + } +}); + +tape( 'the function returns the next code point position in a provided string', function test( t ) { + var out; + + out = nextCodePointIndex( 'last man standing' ); + t.strictEqual( out, 1, 'returns expected value' ); + + out = nextCodePointIndex( 'presidential election' ); + t.strictEqual( out, 1, 'returns expected value' ); + + out = nextCodePointIndex( 'नुच्छेद' ); + t.strictEqual( out, 1, 'returns expected value' ); + + out = nextCodePointIndex( '\uD800' ); // unpaired ending high surrogate + t.strictEqual( out, -1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing an index from which to begin searching', function test( t ) { + var out; + + out = nextCodePointIndex( 'last man standing', 4 ); + t.strictEqual( out, 5, 'returns expected value' ); + + out = nextCodePointIndex( 'presidential election', 8 ); + t.strictEqual( out, 9, 'returns expected value' ); + + out = nextCodePointIndex( 'अनुच्छेद', 1 ); + t.strictEqual( out, 2, 'returns expected value' ); + + out = nextCodePointIndex( '𐒻𐓟𐒻𐓟', 0 ); + t.strictEqual( out, 2, 'returns expected value' ); + + out = nextCodePointIndex( 'a\uDBFFaaaaaaa', 0 ); // unpaired high surrogate + t.strictEqual( out, 1, 'returns expected value' ); + + out = nextCodePointIndex( 'a\uDBFF', 0 ); // unpaired ending high surrogate + t.strictEqual( out, 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a negative integer for the starting search index', function test( t ) { + var out; + + out = nextCodePointIndex( 'last man standing', -13 ); + t.strictEqual( out, 5, 'returns expected value' ); + + out = nextCodePointIndex( 'presidential election', -13 ); + t.strictEqual( out, 9, 'returns expected value' ); + + out = nextCodePointIndex( 'अनुच्छेद', -7 ); + t.strictEqual( out, 2, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `-1` if the starting search index is greater than or equal to the string length', function test( t ) { + var out; + + out = nextCodePointIndex( 'last man standing', 17 ); + t.strictEqual( out, -1, 'returns expected value' ); + + out = nextCodePointIndex( 'presidential election', 22 ); + t.strictEqual( out, -1, 'returns expected value' ); + + out = nextCodePointIndex( 'अनुच्छेद', 10 ); + t.strictEqual( out, -1, 'returns expected value' ); + + out = nextCodePointIndex( '🌷', 2 ); + t.strictEqual( out, -1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `-1` if the starting search index resides within the last code point of the string', function test( t ) { + var out; + + out = nextCodePointIndex( 'last man standing', 16 ); + t.strictEqual( out, -1, 'returns expected value' ); + + out = nextCodePointIndex( 'presidential election', 20 ); + t.strictEqual( out, -1, 'returns expected value' ); + + out = nextCodePointIndex( 'अनुच्छेद', 7 ); + t.strictEqual( out, -1, 'returns expected value' ); + + out = nextCodePointIndex( '🌷', 1 ); + t.strictEqual( out, -1, 'returns expected value' ); + + out = nextCodePointIndex( '🌷' ); + t.strictEqual( out, -1, 'returns expected value' ); + + out = nextCodePointIndex( '六', 0 ); + t.strictEqual( out, -1, 'returns expected value' ); + + out = nextCodePointIndex( '六' ); + t.strictEqual( out, -1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns `-1` if provided an empty string', function test( t ) { + var out; + + out = nextCodePointIndex( '', -2 ); + t.strictEqual( out, -1, 'returns expected value' ); + + out = nextCodePointIndex( '', 2 ); + t.strictEqual( out, -1, 'returns expected value' ); + + out = nextCodePointIndex( '', 0 ); + t.strictEqual( out, -1, 'returns expected value' ); + + out = nextCodePointIndex( '' ); + t.strictEqual( out, -1, 'returns expected value' ); + + t.end(); +});