diff --git a/lib/node_modules/@stdlib/string/num-code-points/README.md b/lib/node_modules/@stdlib/string/num-code-points/README.md new file mode 100644 index 000000000000..93bfba833526 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/README.md @@ -0,0 +1,152 @@ + + +# numCodePoints + +> Return the number of Unicode code points in a string. + +
+ +## Usage + +```javascript +var numCodePoints = require( '@stdlib/string/num-code-points' ); +``` + +#### numCodePoints( str ) + +Returns the number of code points in a string. + +```javascript +var out = numCodePoints( 'last man standing' ); +// returns 17 + +out = numCodePoints( 'Hidden Treasures' ); +// returns 16 + +out = numCodePoints( '👋👋👋' ); +// returns 3 +``` + +
+ + + +
+ +## Examples + + + +```javascript +var numCodePoints = require( '@stdlib/string/num-code-points' ); + +console.log( numCodePoints( 'last man standing' ) ); +// => 17 + +console.log( numCodePoints( '六书/六書' ) ); +// => 5 + +console.log( numCodePoints( '🐶🐮🐷🐰🐸' ) ); +// => 5 + +console.log( numCodePoints( 'Hello 👋 World' ) ); +// => 13 +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: num-code-points [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + -l, --lines Analyze individual lines. +``` + +
+ + + +
+ +### Examples + +```bash +$ num-code-points beep +4 +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n 'beep\nboop書' | num-code-points +10 +``` + +```bash +$ echo -n 'beep\nboop書' | num-code-points -l +4 +5 +``` + +
+ + + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/num-code-points/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/num-code-points/benchmark/benchmark.js new file mode 100644 index 000000000000..577ba7c1d0cd --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/benchmark/benchmark.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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' ); +var pkg = require( './../package.json' ).name; +var numCodePoints = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var strings; + var out; + var i; + + strings = [ + 'last man standing', + 'presidential election', + 'अनुच्छेद', + '🌷', + '书/六書', + 'เ❄︎நி', + 'กิิก้้ก็็ก็็กิิก้้ก็็กิิก้้กิิก้้ก็็ก็็กิิก้้ก็็กิิก้้', + '書六/书六', + 'ܶƔλʃݖͱšɕ҆ʧѸؐҜҦɳΏ', + 'âݝΝ‚ҳӌݾҀƳ۵ۧ޳ǁǸΓ' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = numCodePoints( strings[ i%strings.length ] ); + if ( out < 0 ) { + b.fail( 'should never be a negative integer' ); + } + } + 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/num-code-points/bin/cli b/lib/node_modules/@stdlib/string/num-code-points/bin/cli new file mode 100644 index 000000000000..abf22268cc41 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/bin/cli @@ -0,0 +1,106 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 RE_EOL = require( '@stdlib/regexp/eol' ).REGEXP; +var numCodePoints = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var lines; + var args; + var cli; + var i; + + // 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; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + if ( flags.lines ) { + lines = args[ 0 ].split( RE_EOL ); + for ( i = 0; i < lines.length; i++ ) { + console.log( numCodePoints( lines[ i ] ) ); // eslint-disable-line no-console + } + } else { + console.log( numCodePoints( args[ 0 ] ) ); // eslint-disable-line no-console + } + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + data = data.toString(); + if ( flags.lines ) { + lines = data.split( RE_EOL ); + for ( i = 0; i < lines.length; i++ ) { + console.log( numCodePoints( lines[ i ] ) ); // eslint-disable-line no-console + } + } else { + console.log( numCodePoints( data ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/string/num-code-points/docs/repl.txt b/lib/node_modules/@stdlib/string/num-code-points/docs/repl.txt new file mode 100644 index 000000000000..53aaaadf4463 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/docs/repl.txt @@ -0,0 +1,23 @@ + +{{alias}}( str ) + Returns the number of code points in a string. + + Parameters + ---------- + str: string + Input string. + + Returns + ------- + out: string + Number of code points. + + Examples + -------- + > var out = {{alias}}( 'beep' ) + 4 + > out = {{alias}}( '六' ) + 1 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/string/num-code-points/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/num-code-points/docs/types/index.d.ts new file mode 100644 index 000000000000..ca70d00ef83d --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 + +/** +* Returns the number of code points in a string. +* +* @param str - input string +* @returns number of code points +* +* @example +* var out = numCodePoints( 'last man standing' ); +* // returns 17 +* +* @example +* var out = numCodePoints( 'presidential election' ); +* // returns 21 +* +* @example +* var out = numCodePoints( '六' ); +* // returns 1 +* +* @example +* var out = numCodePoints( 'अनुच्छेद' ); +* // returns 5 +*/ +declare function numCodePoints( str: string ): number; + + +// EXPORTS // + +export = numCodePoints; diff --git a/lib/node_modules/@stdlib/string/num-code-points/docs/types/test.ts b/lib/node_modules/@stdlib/string/num-code-points/docs/types/test.ts new file mode 100644 index 000000000000..0e277c7c1ade --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 numCodePoints = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + numCodePoints( 'abc' ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a value other than a string... +{ + numCodePoints( true ); // $ExpectError + numCodePoints( false ); // $ExpectError + numCodePoints( null ); // $ExpectError + numCodePoints( undefined ); // $ExpectError + numCodePoints( 5 ); // $ExpectError + numCodePoints( [] ); // $ExpectError + numCodePoints( {} ); // $ExpectError + numCodePoints( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + numCodePoints(); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/num-code-points/docs/usage.txt b/lib/node_modules/@stdlib/string/num-code-points/docs/usage.txt new file mode 100644 index 000000000000..3214f1149de3 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/docs/usage.txt @@ -0,0 +1,8 @@ + +Usage: num-code-points [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + -l, --lines Analyze individual lines. diff --git a/lib/node_modules/@stdlib/string/num-code-points/etc/cli_opts.json b/lib/node_modules/@stdlib/string/num-code-points/etc/cli_opts.json new file mode 100644 index 000000000000..d5d5629e9e27 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/etc/cli_opts.json @@ -0,0 +1,18 @@ +{ + "boolean": [ + "help", + "version", + "lines" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ], + "lines": [ + "l" + ] + } +} diff --git a/lib/node_modules/@stdlib/string/num-code-points/examples/index.js b/lib/node_modules/@stdlib/string/num-code-points/examples/index.js new file mode 100644 index 000000000000..845e339ae672 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 numCodePoints = require( './../lib' ); + +console.log( numCodePoints( 'last man standing' ) ); +// => 17 + +console.log( numCodePoints( '六书/六書' ) ); +// => 5 + +console.log( numCodePoints( '🐶🐮🐷🐰🐸' ) ); +// => 5 + +console.log( numCodePoints( 'Hello 👋 World' ) ); +// => 13 diff --git a/lib/node_modules/@stdlib/string/num-code-points/lib/index.js b/lib/node_modules/@stdlib/string/num-code-points/lib/index.js new file mode 100644 index 000000000000..bcc3f732fc3d --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 number of code points in a string. +* +* @module @stdlib/string/num-code-points +* +* @example +* var numCodePoints = require( '@stdlib/string/num-code-points' ); +* +* var out = numCodePoints( 'last man standing' ); +* // returns 17 +* +* out = numCodePoints( '六' ); +* // returns 1 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/num-code-points/lib/main.js b/lib/node_modules/@stdlib/string/num-code-points/lib/main.js new file mode 100644 index 000000000000..87762f0c6159 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/lib/main.js @@ -0,0 +1,89 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 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 number of code points in a string. +* +* @param {string} str - input string +* @throws {TypeError} must provide a string +* @returns {NonNegativeInteger} number of code points +* +* @example +* var out = numCodePoints( 'last man standing' ); +* // returns 17 +* +* @example +* var out = numCodePoints( 'presidential election' ); +* // returns 21 +* +* @example +* var out = numCodePoints( 'अनुच्छेद' ); +* // returns 8 +*/ +function numCodePoints( str ) { + var count; + var i; + + if ( !isString( str ) ) { + throw new TypeError( format( 'invalid argument. Must provide a string. Value: `%s`.', str ) ); + } + count = 0; + + // 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 < str.length; i++ ) { + // Check for a high UTF-16 surrogate... + if ( RE_UTF16_HIGH_SURROGATE.test( str[ i ] ) ) { + // Check for an unpaired surrogate at the end of the input string... + if ( i === str.length-1 ) { + // We found an unpaired surrogate... + count += 1; + break; + } + // Check whether the high surrogate is paired with a low surrogate... + if ( RE_UTF16_LOW_SURROGATE.test( str[ i+1 ] ) ) { + // We found a surrogate pair: + i += 1; // bump the index to process the next code unit + count += 1; + } + } else { + count += 1; + } + } + return count; +} + + +// EXPORTS // + +module.exports = numCodePoints; diff --git a/lib/node_modules/@stdlib/string/num-code-points/package.json b/lib/node_modules/@stdlib/string/num-code-points/package.json new file mode 100644 index 000000000000..dec608eca593 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/string/num-code-points", + "version": "0.0.0", + "description": "Return the number of code points in 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" + } + ], + "bin": { + "num-code-points": "./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", + "utilities", + "utility", + "utils", + "util", + "string", + "str", + "length", + "len", + "unicode", + "code", + "point", + "segmentation", + "surrogate", + "astral", + "emojis" + ] +} diff --git a/lib/node_modules/@stdlib/string/num-code-points/test/fixtures/stdin_error.js.txt b/lib/node_modules/@stdlib/string/num-code-points/test/fixtures/stdin_error.js.txt new file mode 100644 index 000000000000..7b3cc56df0fa --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/test/fixtures/stdin_error.js.txt @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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/num-code-points/test/test.cli.js b/lib/node_modules/@stdlib/string/num-code-points/test/test.cli.js new file mode 100644 index 000000000000..2b99d0b19f25 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/test/test.cli.js @@ -0,0 +1,261 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 prints the number of code points', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'beep\\nboop\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '9\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-l` flag, the command-line interface prints the number of code points for individual lines', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'beep\\nboop\'; process.argv[ 3 ] = \'-l\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '4\n4\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 "beep\nboop六"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '10\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream with `-l` flag, the command-line interface prints the number of code points for each line', opts, function test( t ) { + var cmd = [ + 'printf "beep\nboop六"', + '|', + EXEC_PATH, + fpath, + '-l' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '4\n5\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/num-code-points/test/test.js b/lib/node_modules/@stdlib/string/num-code-points/test/test.js new file mode 100644 index 000000000000..e4b2684c2978 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/test/test.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 numCodePoints = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof numCodePoints, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if not provided a string', 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() { + numCodePoints( value ); + }; + } +}); + +tape( 'the function returns 0 if provided an empty string', function test( t ) { + t.strictEqual( numCodePoints( '' ), 0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns the Unicode aware length of a provided string', function test( t ) { + var out; + + out = numCodePoints( 'hello world' ); + t.strictEqual( out, 11, 'returns expected value' ); + + out = numCodePoints( '!!!' ); + t.strictEqual( out, 3, 'returns expected value' ); + + out = numCodePoints( 'अनुच्छेद' ); + t.strictEqual( out, 8, 'returns expected value' ); + + out = numCodePoints( '六' ); + t.strictEqual( out, 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the number of code points (surrogates)', function test( t ) { + var out; + + out = numCodePoints( '𐒻𐓟' ); + t.strictEqual( out, 2, 'returns expected value' ); + + out = numCodePoints( '𐒻' ); + t.strictEqual( out, 1, 'returns expected value' ); + + out = numCodePoints( '\uD800' ); // trailing unpaired high surrogate + t.strictEqual( out, 1, 'returns expected value' ); + + t.end(); +});