From 6139d7409a4012065015bfb424007dfca002a18a Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Wed, 26 Feb 2025 03:26:43 +0000 Subject: [PATCH 1/4] feat: add @stdlib/string/numCodePoints --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: passed - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na --- --- .../@stdlib/string/num-code-points/README.md | 152 ++++++++++ .../num-code-points/benchmark/benchmark.js | 62 +++++ .../@stdlib/string/num-code-points/bin/cli | 108 ++++++++ .../string/num-code-points/docs/repl.txt | 23 ++ .../num-code-points/docs/types/index.d.ts | 48 ++++ .../string/num-code-points/docs/types/test.ts | 44 +++ .../string/num-code-points/docs/usage.txt | 8 + .../string/num-code-points/etc/cli_opts.json | 18 ++ .../string/num-code-points/examples/index.js | 33 +++ .../string/num-code-points/lib/index.js | 43 +++ .../string/num-code-points/lib/main.js | 89 ++++++ .../string/num-code-points/package.json | 73 +++++ .../test/fixtures/stdin_error.js.txt | 33 +++ .../string/num-code-points/test/test.cli.js | 261 ++++++++++++++++++ .../string/num-code-points/test/test.js | 98 +++++++ 15 files changed, 1093 insertions(+) create mode 100644 lib/node_modules/@stdlib/string/num-code-points/README.md create mode 100644 lib/node_modules/@stdlib/string/num-code-points/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/string/num-code-points/bin/cli create mode 100644 lib/node_modules/@stdlib/string/num-code-points/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/string/num-code-points/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/string/num-code-points/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/string/num-code-points/docs/usage.txt create mode 100644 lib/node_modules/@stdlib/string/num-code-points/etc/cli_opts.json create mode 100644 lib/node_modules/@stdlib/string/num-code-points/examples/index.js create mode 100644 lib/node_modules/@stdlib/string/num-code-points/lib/index.js create mode 100644 lib/node_modules/@stdlib/string/num-code-points/lib/main.js create mode 100644 lib/node_modules/@stdlib/string/num-code-points/package.json create mode 100644 lib/node_modules/@stdlib/string/num-code-points/test/fixtures/stdin_error.js.txt create mode 100644 lib/node_modules/@stdlib/string/num-code-points/test/test.cli.js create mode 100644 lib/node_modules/@stdlib/string/num-code-points/test/test.js 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..e6095f29ae67 --- /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' ); + +var out = numCodePoints( 'last man standing' ); +// returns 17 + +out = numCodePoints( '六书/六書' ); +// returns 5 + +out = numCodePoints( '🐶🐮🐷🐰🐸' ); +// returns 5 + +out = numCodePoints( 'Hello 👋 World' ); +// returns 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..69ddc4b78a0d --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/bin/cli @@ -0,0 +1,108 @@ +#!/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 isRegExpString = require( '@stdlib/assert/is-regexp-string' ); +var reFromString = require( '@stdlib/utils/regexp-from-string' ); +var numCodePoints = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var split; + var args; + var cli; + + // 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 ) { + if ( flags.split ) { + if ( !isRegExpString( flags.split ) ) { + flags.split = '/'+flags.split+'/'; + } + split = reFromString( flags.split ); + } else { + split = RE_EOL; + } + return stdin( onRead ); + } + 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 ); + } + lines = data.toString().split( split ); + + // Remove any trailing separators (e.g., trailing newline)... + if ( lines[ lines.length-1 ] === '' ) { + lines.pop(); + } + for ( i = 0; i < lines.length; i++ ) { + console.log( numCodePoints( lines[ i ] ) ); // 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..4054c64bc200 --- /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( 'अनुच्छेद' ) ); +// => 8 + +console.log( numCodePoints( '𐒻𐓟𐒻𐓟' ) ); +// => 4 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..3a0536eec9ba --- /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(), '9\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(), '4\n5\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(); +}); From f6ce71bbed31bdc767c0518d37e1fae7d9893559 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Wed, 26 Feb 2025 11:46:10 +0500 Subject: [PATCH 2/4] refactor: match examples Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- .../@stdlib/string/num-code-points/examples/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 index 4054c64bc200..845e339ae672 100644 --- a/lib/node_modules/@stdlib/string/num-code-points/examples/index.js +++ b/lib/node_modules/@stdlib/string/num-code-points/examples/index.js @@ -26,8 +26,8 @@ console.log( numCodePoints( 'last man standing' ) ); console.log( numCodePoints( '六书/六書' ) ); // => 5 -console.log( numCodePoints( 'अनुच्छेद' ) ); -// => 8 +console.log( numCodePoints( '🐶🐮🐷🐰🐸' ) ); +// => 5 -console.log( numCodePoints( '𐒻𐓟𐒻𐓟' ) ); -// => 4 +console.log( numCodePoints( 'Hello 👋 World' ) ); +// => 13 From 11f531ce0aa4a0550637d51f9b69b469353e83ec Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Wed, 26 Feb 2025 11:48:20 +0500 Subject: [PATCH 3/4] docs: match examples Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- .../@stdlib/string/num-code-points/README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/string/num-code-points/README.md b/lib/node_modules/@stdlib/string/num-code-points/README.md index e6095f29ae67..93bfba833526 100644 --- a/lib/node_modules/@stdlib/string/num-code-points/README.md +++ b/lib/node_modules/@stdlib/string/num-code-points/README.md @@ -58,17 +58,17 @@ out = numCodePoints( '👋👋👋' ); ```javascript var numCodePoints = require( '@stdlib/string/num-code-points' ); -var out = numCodePoints( 'last man standing' ); -// returns 17 +console.log( numCodePoints( 'last man standing' ) ); +// => 17 -out = numCodePoints( '六书/六書' ); -// returns 5 +console.log( numCodePoints( '六书/六書' ) ); +// => 5 -out = numCodePoints( '🐶🐮🐷🐰🐸' ); -// returns 5 +console.log( numCodePoints( '🐶🐮🐷🐰🐸' ) ); +// => 5 -out = numCodePoints( 'Hello 👋 World' ); -// returns 13 +console.log( numCodePoints( 'Hello 👋 World' ) ); +// => 13 ``` From 838029165daaad1eb9bd478ccf0c44794bbda6b1 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sat, 15 Mar 2025 10:20:01 +0000 Subject: [PATCH 4/4] fix: apply suggestions from code review --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: passed - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/string/num-code-points/bin/cli | 38 +++++++++---------- .../string/num-code-points/test/test.cli.js | 4 +- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/lib/node_modules/@stdlib/string/num-code-points/bin/cli b/lib/node_modules/@stdlib/string/num-code-points/bin/cli index 69ddc4b78a0d..abf22268cc41 100644 --- a/lib/node_modules/@stdlib/string/num-code-points/bin/cli +++ b/lib/node_modules/@stdlib/string/num-code-points/bin/cli @@ -28,8 +28,6 @@ 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 isRegExpString = require( '@stdlib/assert/is-regexp-string' ); -var reFromString = require( '@stdlib/utils/regexp-from-string' ); var numCodePoints = require( './../lib' ); @@ -43,9 +41,10 @@ var numCodePoints = require( './../lib' ); */ function main() { var flags; - var split; + var lines; var args; var cli; + var i; // Create a command-line interface: cli = new CLI({ @@ -67,17 +66,16 @@ function main() { // Check if we are receiving data from `stdin`... if ( !stdinStream.isTTY ) { - if ( flags.split ) { - if ( !isRegExpString( flags.split ) ) { - flags.split = '/'+flags.split+'/'; - } - split = reFromString( flags.split ); - } else { - split = RE_EOL; - } return stdin( onRead ); } - console.log( numCodePoints( args[ 0 ] ) ); // eslint-disable-line no-console + 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`. @@ -93,14 +91,14 @@ function main() { if ( error ) { return cli.error( error ); } - lines = data.toString().split( split ); - - // Remove any trailing separators (e.g., trailing newline)... - if ( lines[ lines.length-1 ] === '' ) { - lines.pop(); - } - for ( i = 0; i < lines.length; i++ ) { - console.log( numCodePoints( lines[ i ] ) ); // eslint-disable-line no-console + 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 } } } 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 index 3a0536eec9ba..2b99d0b19f25 100644 --- 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 @@ -175,7 +175,7 @@ tape( 'when invoked with a `-l` flag, the command-line interface prints the numb if ( error ) { t.fail( error.message ); } else { - t.strictEqual( stdout.toString(), '9\n', 'expected value' ); + t.strictEqual( stdout.toString(), '4\n4\n', 'expected value' ); t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); } t.end(); @@ -196,7 +196,7 @@ tape( 'the command-line interface supports use as a standard stream', opts, func if ( error ) { t.fail( error.message ); } else { - t.strictEqual( stdout.toString(), '4\n5\n', 'expected value' ); + t.strictEqual( stdout.toString(), '10\n', 'expected value' ); t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); } t.end();