From 61f2dcaaaace50d7cf5033ac1357a92fd8c38860 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Mon, 24 Feb 2025 13:43:32 +0000 Subject: [PATCH 1/9] feat: add string/base/slice-code-points --- 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: na - 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 --- --- .../string/base/slice-code-points/README.md | 86 ++++++++++ .../slice-code-points/benchmark/benchmark.js | 57 +++++++ .../base/slice-code-points/docs/repl.txt | 28 +++ .../slice-code-points/docs/types/index.d.ts | 42 +++++ .../base/slice-code-points/docs/types/test.ts | 68 ++++++++ .../base/slice-code-points/examples/index.js | 30 ++++ .../base/slice-code-points/lib/index.js | 43 +++++ .../string/base/slice-code-points/lib/main.js | 124 ++++++++++++++ .../base/slice-code-points/package.json | 64 +++++++ .../base/slice-code-points/test/test.js | 159 ++++++++++++++++++ 10 files changed, 701 insertions(+) create mode 100644 lib/node_modules/@stdlib/string/base/slice-code-points/README.md create mode 100644 lib/node_modules/@stdlib/string/base/slice-code-points/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/string/base/slice-code-points/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/string/base/slice-code-points/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/string/base/slice-code-points/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/string/base/slice-code-points/examples/index.js create mode 100644 lib/node_modules/@stdlib/string/base/slice-code-points/lib/index.js create mode 100644 lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js create mode 100644 lib/node_modules/@stdlib/string/base/slice-code-points/package.json create mode 100644 lib/node_modules/@stdlib/string/base/slice-code-points/test/test.js diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/README.md b/lib/node_modules/@stdlib/string/base/slice-code-points/README.md new file mode 100644 index 000000000000..141978898e1f --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/README.md @@ -0,0 +1,86 @@ + + +# sliceCodePoints + +> Slice a string based on Unicode code point indices. + +
+ +## Usage + +```javascript +var sliceCodePoints = require( '@stdlib/string/base/slice-code-points' ); +``` + +#### sliceCodePoints( str, start, end ) + +Slices a string based on Unicode code point indices. + +```javascript +var str = 'Hello ๐Ÿ‘‹ World'; +var out = sliceCodePoints( str, 0, 7 ); +// returns 'Hello ๐Ÿ‘‹' + +str = '๐Ÿ‘‹๐Ÿ‘‹๐Ÿ‘‹'; +out = sliceCodePoints( str, 1, 2 ); +// returns '๐Ÿ‘‹' +``` + +
+ + + +
+ +## Examples + +```javascript +var sliceCodePoints = require( '@stdlib/string/base/slice-code-points' ); + +console.log( sliceCodePoints( 'Hello ๐Ÿ‘‹ World', 0, 7 ) ); +// => 'Hello ๐Ÿ‘‹' + +console.log( sliceCodePoints( 'last man standing', 1, 17 ) ); +// => 'ast man standing' + +console.log( sliceCodePoints( 'ๅ…ญไนฆ/ๅ…ญๆ›ธ', 1, 14 ) ); +// => 'ไนฆ/ๅ…ญๆ›ธ' +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/slice-code-points/benchmark/benchmark.js new file mode 100644 index 000000000000..525025ed48f1 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/benchmark/benchmark.js @@ -0,0 +1,57 @@ +/** +* @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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var sliceCodePoints = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var out; + var i; + + values = [ + 'Iรฑtรซrnรขtiรดnร lizรฆtiรธn', + 'presidential election', + '๐Ÿถ๐Ÿฎ๐Ÿท๐Ÿฐ๐Ÿธ', + 'Hello ๐Ÿ‘‹ World', + 'เค…เคจเฅเคšเฅเค›เฅ‡เคฆ' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = sliceCodePoints( values[ i%values.length ], 1, 3 ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/docs/repl.txt b/lib/node_modules/@stdlib/string/base/slice-code-points/docs/repl.txt new file mode 100644 index 000000000000..fbfaca1173f8 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/docs/repl.txt @@ -0,0 +1,28 @@ +{{alias}}( str, start, end ) + Slices a string based on Unicode code point indices. + + Parameters + ---------- + str: string + Input string. + start: integer + The `ith` Unicode code point to start a slice (inclusive). + end: integer + The `jth` Unicode code point to end a slice (exclusive). + + Returns + ------- + out: string + Output string. + + Examples + -------- + > var out = {{alias}}( 'Hello ๐Ÿ‘‹ World', 1, 3 ) + 'el' + > out = {{alias}}( '๐Ÿ‘‹๐Ÿ‘‹๐Ÿ‘‹', 1, 2 ) + '๐Ÿ‘‹' + > out = {{alias}}( 'ๅ…ญไนฆ/ๅ…ญๆ›ธ', 1, 14 ) + 'ไนฆ/ๅ…ญๆ›ธ' + + See Also + -------- diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/slice-code-points/docs/types/index.d.ts new file mode 100644 index 000000000000..6779de9d8722 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @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: 4.1 + +/** +* Slices a string based on Unicode code point indices. +* +* @param str - input string +* @param start - the `ith` Unicode code point to start a slice (inclusive) +* @param end - the `jth` Unicode code point to end a slice (exclusive) +* @returns output string +* +* @example +* var out = sliceCodePoints( 'Hello ๐Ÿ‘‹ World', 1, 3 ); +* // returns 'el' +* +* @example +* var out = sliceCodePoints( '๐Ÿ‘‹๐Ÿ‘‹๐Ÿ‘‹', 1, 2 ); +* // returns '๐Ÿ‘‹' +*/ +declare function sliceCodePoints( str: string, start: number, end: number ): string; + + +// EXPORTS // + +export = sliceCodePoints; diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/slice-code-points/docs/types/test.ts new file mode 100644 index 000000000000..5e7baf157e5e --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/docs/types/test.ts @@ -0,0 +1,68 @@ +/* +* @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 sliceCodePoints = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + sliceCodePoints( 'abc', 1, 2 ); // $ExpectType string +} + +// The compiler throws an error if the function is provided a value other than a string... +{ + sliceCodePoints( true, 1, 2 ); // $ExpectError + sliceCodePoints( false, 1, 2 ); // $ExpectError + sliceCodePoints( null, 1, 2 ); // $ExpectError + sliceCodePoints( undefined, 1, 2 ); // $ExpectError + sliceCodePoints( 5, 1, 2 ); // $ExpectError + sliceCodePoints( [], 1, 2 ); // $ExpectError + sliceCodePoints( {}, 1, 2 ); // $ExpectError + sliceCodePoints( ( x: number ): number => x, 1, 2 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument that is not a number... +{ + sliceCodePoints( 'abc', true, 2 ); // $ExpectError + sliceCodePoints( 'abc', false, 2 ); // $ExpectError + sliceCodePoints( 'abc', null, 2 ); // $ExpectError + sliceCodePoints( 'abc', 'abc', 2 ); // $ExpectError + sliceCodePoints( 'abc', [], 2 ); // $ExpectError + sliceCodePoints( 'abc', {}, 2 ); // $ExpectError + sliceCodePoints( 'abc', ( x: number ): number => x, 2 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument that is not a number... +{ + sliceCodePoints( 'abc', 1, true ); // $ExpectError + sliceCodePoints( 'abc', 1, false ); // $ExpectError + sliceCodePoints( 'abc', 1, null ); // $ExpectError + sliceCodePoints( 'abc', 1, 'abc' ); // $ExpectError + sliceCodePoints( 'abc', 1, [] ); // $ExpectError + sliceCodePoints( 'abc', 1, {} ); // $ExpectError + sliceCodePoints( 'abc', 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + sliceCodePoints(); // $ExpectError + sliceCodePoints( 'abc' ); // $ExpectError + sliceCodePoints( 'abc', 1, 2, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/examples/index.js b/lib/node_modules/@stdlib/string/base/slice-code-points/examples/index.js new file mode 100644 index 000000000000..25556534a021 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/examples/index.js @@ -0,0 +1,30 @@ +/** +* @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 sliceCodePoints = require( './../lib' ); + +console.log( sliceCodePoints( 'Hello ๐Ÿ‘‹ World', 0, 7 ) ); +// => 'Hello ๐Ÿ‘‹' + +console.log( sliceCodePoints( 'last man standing', 1, 17 ) ); +// => 'ast man standing' + +console.log( sliceCodePoints( 'ๅ…ญไนฆ/ๅ…ญๆ›ธ', 1, 14 ) ); +// => 'ไนฆ/ๅ…ญๆ›ธ' diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/lib/index.js b/lib/node_modules/@stdlib/string/base/slice-code-points/lib/index.js new file mode 100644 index 000000000000..2020cfff8b75 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-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'; + +/** +* Slice a string based on Unicode code point indices. +* +* @module @stdlib/string/base/slice-code-points +* +* @example +* var sliceCodePoints = require( '@stdlib/string/base/slice-code-points' ); +* +* var out = sliceCodePoints( 'Hello ๐Ÿ‘‹ World', 1, 3 ); +* // returns 'el' +* +* out = sliceCodePoints( '๐Ÿ‘‹๐Ÿ‘‹๐Ÿ‘‹', 1, 2 ); +* // returns '๐Ÿ‘‹' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js b/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js new file mode 100644 index 000000000000..3a0d01669764 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js @@ -0,0 +1,124 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var max = require( '@stdlib/math/base/special/max' ); + + +// 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 // + +/** +* Slices a string based on Unicode code points. +* +* @param {string} str - input string +* @param {integer} start - the `ith` Unicode code point to start a slice (inclusive) +* @param {integer} end - the `jth` Unicode code point to end a slice (exclusive) +* @throws {TypeError} must provide exactly 3 arguments +* @returns {string} output string +* +* @example +* var out = sliceCodePoints( 'Hello ๐Ÿ‘‹ World', 1, 3 ); +* // returns 'el' +* +* out = sliceCodePoints( '๐Ÿ‘‹๐Ÿ‘‹๐Ÿ‘‹', 1, 2 ); +* // returns '๐Ÿ‘‹' +*/ +function sliceCodePoints( str, start, end ) { + var totalCodePoints; + var codePoints; + var len; + var ch1; + var ch2; + var idx; + var ch; + var i; + + if ( arguments.length !== 3 ) { + throw new TypeError( 'invalid invocation. Must provide a valid string, starting code point index, and ending code point index.' ); + } + + len = str.length; + if ( len === 0 ) { + return ''; + } + + totalCodePoints = 0; + for ( i = 0; i < len; i++ ) { + ch1 = str[ i ]; + totalCodePoints += 1; + + if ( RE_UTF16_HIGH_SURROGATE.test( ch1 ) && i < len - 1 ) { + ch2 = str[ i + 1 ]; + if ( RE_UTF16_LOW_SURROGATE.test( ch2 ) ) { + i += 1; + } + } + } + + if ( start < 0 ) { + start = max( totalCodePoints + start, 0 ); + } + if ( end < 0 ) { + end = max( totalCodePoints + end, 0 ); + } + + if ( start >= totalCodePoints || end <= start ) { + return ''; + } + + codePoints = ''; + idx = 0; + + // Process the string one Unicode code unit at a time and handle UTF-16 surrogate pairs as a single Unicode code point... + for ( i = 0; i < len; i++ ) { + ch1 = str[ i ]; + ch = ch1; + + // Check for a UTF-16 surrogate pair... + if ( i < len-1 && RE_UTF16_HIGH_SURROGATE.test( ch1 ) ) { + // Check whether the high surrogate is paired with a low surrogate... + ch2 = str[ i+1 ]; + if ( RE_UTF16_LOW_SURROGATE.test( ch2 ) ) { + // We found a surrogate pair: + ch += ch2; + i += 1; // bump the index to process the next code unit + } + } + // Note: `ch` may be a lone surrogate (e.g., a low surrogate without a preceding high surrogate or a high surrogate at the end of the input string). + + if ( idx >= start && idx < end ) { + codePoints += ch; + } + idx += 1; + } + return codePoints; +} + + +// EXPORTS // + +module.exports = sliceCodePoints; diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/package.json b/lib/node_modules/@stdlib/string/base/slice-code-points/package.json new file mode 100644 index 000000000000..ae14af7fb833 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/string/base/slice-code-points", + "version": "0.0.0", + "description": "Slice a string based on Unicode code points.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib/main.js", + "directories": { + "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", + "string", + "str", + "base", + "slice", + "substring", + "unicode", + "code", + "point", + "codepoint", + "utilities", + "utils" + ] +} diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/test/test.js b/lib/node_modules/@stdlib/string/base/slice-code-points/test/test.js new file mode 100644 index 000000000000..9d6ac418c7aa --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/test/test.js @@ -0,0 +1,159 @@ +/** +* @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 sliceCodePoints = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof sliceCodePoints, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 3', function test( t ) { + t.strictEqual( sliceCodePoints.length, 3, 'the function has an arity of 3' ); + t.end(); +}); + +tape( 'the function returns an empty string if provided an empty string', function test( t ) { + t.strictEqual( sliceCodePoints( '', 1, 2 ), '', 'returns expected value' ); + t.strictEqual( sliceCodePoints( '', 2, 3 ), '', 'returns expected value' ); + t.strictEqual( sliceCodePoints( '', 3, 4 ), '', 'returns expected value' ); + t.end(); +}); + +tape( 'the function throws an error if provided an unsupported number of arguments', function test( t ) { + var values; + var i; + + values = [ + [ 'foo', 1 ], + [ 'bar' ], + [ 'beep', 1, 3, 5 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue.apply( null, values[i] ), TypeError, 'throws an error when provided unsupported number of arguments' ); + } + t.end(); + + function badValue() { + return function badValue() { + sliceCodePoints.apply( null, arguments ); + }; + } +}); + +tape( 'the function slices a string based on code point indices', function test( t ) { + var str; + var out; + + str = 'Hello World'; + out = sliceCodePoints( str, 1, 5 ); + t.strictEqual( out, 'ello', 'returns expected value' ); + + str = 'Hello ๐Ÿ‘‹ World'; + out = sliceCodePoints( str, 5, 7 ); + t.strictEqual( out, ' ๐Ÿ‘‹', 'returns expected value' ); + + str = 'เค…เคจเฅเคšเฅเค›เฅ‡เคฆ'; + out = sliceCodePoints( str, 1, str.length ); + t.strictEqual( out, 'เคจเฅเคšเฅเค›เฅ‡เคฆ', 'returns expected value' ); + + str = 'ๅ…ญไนฆ/ๅ…ญๆ›ธ'; + out = sliceCodePoints( str, 1, str.length ); + t.strictEqual( out, 'ไนฆ/ๅ…ญๆ›ธ', 'returns expected value' ); + + str = '๐’ป๐“Ÿ'; + out = sliceCodePoints( str, 1, str.length ); + t.strictEqual( out, '๐“Ÿ', 'returns expected value' ); + + str = '\uD800'; + out = sliceCodePoints( str, 1, 2 ); + t.strictEqual( out, '', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing negative indices', function test( t ) { + var str; + var out; + + str = 'Hello World'; + out = sliceCodePoints( str, -5, -1 ); + t.strictEqual( out, 'Worl', 'returns expected value' ); + + str = 'Hello ๐Ÿ‘‹ World'; + out = sliceCodePoints( str, 0, -6 ); + t.strictEqual( out, 'Hello ๐Ÿ‘‹', 'returns expected value' ); + + str = 'เค…เคจเฅเคšเฅเค›เฅ‡เคฆ'; + out = sliceCodePoints( str, -7, str.length ); + t.strictEqual( out, 'เคจเฅเคšเฅเค›เฅ‡เคฆ', 'returns expected value' ); + + str = 'ๅ…ญไนฆ/ๅ…ญๆ›ธ'; + out = sliceCodePoints( str, -4, str.length ); + t.strictEqual( out, 'ไนฆ/ๅ…ญๆ›ธ', 'returns expected value' ); + + str = '๐’ป๐“Ÿ'; + out = sliceCodePoints( str, -1, str.length ); + t.strictEqual( out, '๐“Ÿ', 'returns expected value' ); + + str = '\uD800'; + out = sliceCodePoints( str, -1, str.length ); + t.strictEqual( out, '\uD800', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles surrogate pairs correctly', function test( t ) { + var out; + + out = sliceCodePoints( '๐Ÿ‘‹๐Ÿ‘‹๐Ÿ‘‹', 0, 1 ); + t.strictEqual( out, '๐Ÿ‘‹', 'returns expected value' ); + + out = sliceCodePoints( '๐Ÿ‘‹๐Ÿ‘‹๐Ÿ‘‹', 1, 2 ); + t.strictEqual( out, '๐Ÿ‘‹', 'returns expected value' ); + + out = sliceCodePoints( '๐Ÿ‘‹a๐Ÿ‘‹b๐Ÿ‘‹', 1, 4 ); + t.strictEqual( out, 'a๐Ÿ‘‹b', 'returns expected value' ); + + out = sliceCodePoints( '๐Ÿ‘‹a๐Ÿ‘‹b๐Ÿ‘‹', 1, 4 ); + t.strictEqual( out, 'a๐Ÿ‘‹b', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function handles lone surrogates', function test( t ) { + var out; + + out = sliceCodePoints( '\uD800abc', 0, 2 ); + t.strictEqual( out, '\uD800a', 'returns expected value for lone high surrogate' ); + + out = sliceCodePoints( '\uDC00abc', 0, 2 ); + t.strictEqual( out, '\uDC00a', 'returns expected value for lone low surrogate' ); + + t.end(); +}); From 1aa4cbda8ddec880d720cfba83c655a13d627da5 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Mon, 24 Feb 2025 19:44:09 +0500 Subject: [PATCH 2/9] docs: apply suggestions from code review Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- .../@stdlib/string/base/slice-code-points/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js b/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js index 3a0d01669764..8a934885d6b1 100644 --- a/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2024 The Stdlib Authors. +* 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. From e0cfaca5815f98ee8fb1af0a14cf610aec602e51 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Mon, 24 Feb 2025 19:55:53 +0500 Subject: [PATCH 3/9] fix: add missing key Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- .../@stdlib/string/base/slice-code-points/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/package.json b/lib/node_modules/@stdlib/string/base/slice-code-points/package.json index ae14af7fb833..ffd42a4e8738 100644 --- a/lib/node_modules/@stdlib/string/base/slice-code-points/package.json +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/package.json @@ -15,6 +15,7 @@ ], "main": "./lib/main.js", "directories": { + "benchmark": "./benchmark", "doc": "./docs", "example": "./examples", "lib": "./lib", From f5d926f4a7ffe27ba4df63a641357adee646245b Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Wed, 26 Feb 2025 06:56:48 +0000 Subject: [PATCH 4/9] fix: add missing edge case & relevant test case --- 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: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - 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 --- --- 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: passed - 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: passed --- --- .../string/base/slice-code-points/README.md | 6 ++---- .../string/base/slice-code-points/lib/main.js | 4 ++++ .../string/base/slice-code-points/test/test.js | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/README.md b/lib/node_modules/@stdlib/string/base/slice-code-points/README.md index 141978898e1f..deeb9b43a70e 100644 --- a/lib/node_modules/@stdlib/string/base/slice-code-points/README.md +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/README.md @@ -35,12 +35,10 @@ var sliceCodePoints = require( '@stdlib/string/base/slice-code-points' ); Slices a string based on Unicode code point indices. ```javascript -var str = 'Hello ๐Ÿ‘‹ World'; -var out = sliceCodePoints( str, 0, 7 ); +var out = sliceCodePoints( 'Hello ๐Ÿ‘‹ World', 0, 7 ); // returns 'Hello ๐Ÿ‘‹' -str = '๐Ÿ‘‹๐Ÿ‘‹๐Ÿ‘‹'; -out = sliceCodePoints( str, 1, 2 ); +out = sliceCodePoints( '๐Ÿ‘‹๐Ÿ‘‹๐Ÿ‘‹', 1, 2 ); // returns '๐Ÿ‘‹' ``` diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js b/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js index 8a934885d6b1..cdce4cfa4f6b 100644 --- a/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js @@ -90,6 +90,10 @@ function sliceCodePoints( str, start, end ) { return ''; } + if ( end > totalCodePoints ) { + end = totalCodePoints; + } + codePoints = ''; idx = 0; diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/test/test.js b/lib/node_modules/@stdlib/string/base/slice-code-points/test/test.js index 9d6ac418c7aa..d6b71aec2436 100644 --- a/lib/node_modules/@stdlib/string/base/slice-code-points/test/test.js +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/test/test.js @@ -157,3 +157,18 @@ tape( 'the function handles lone surrogates', function test( t ) { t.end(); }); + +tape( 'the function truncates the end index to the string length', function test( t ) { + var out; + + out = sliceCodePoints( 'hello', 0, 10 ); + t.strictEqual( out, 'hello', 'returns expected value' ); + + out = sliceCodePoints( 'ๅ…ญไนฆ/ๅ…ญๆ›ธ', 1, 10 ); + t.strictEqual( out, 'ไนฆ/ๅ…ญๆ›ธ', 'returns expected value' ); + + out = sliceCodePoints( '๐Ÿ๏ธ๐ŸŒท', 0, 5 ); + t.strictEqual( out, '๐Ÿ๏ธ๐ŸŒท', 'returns expected value' ); + + t.end(); +}); From bbf37ff659533274805ce6466bc15ee48b9ea043 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Fri, 28 Feb 2025 06:49:26 +0000 Subject: [PATCH 5/9] docs: add function args description --- 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: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - 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 --- --- 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: passed - 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/base/slice-code-points/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/README.md b/lib/node_modules/@stdlib/string/base/slice-code-points/README.md index deeb9b43a70e..bd375ca53143 100644 --- a/lib/node_modules/@stdlib/string/base/slice-code-points/README.md +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/README.md @@ -42,6 +42,12 @@ out = sliceCodePoints( '๐Ÿ‘‹๐Ÿ‘‹๐Ÿ‘‹', 1, 2 ); // returns '๐Ÿ‘‹' ``` +The function accepts the following arguments: + +- **str**: input string. +- **start**: the `ith` Unicode code point to start a slice (inclusive). +- **end**: the `jth` Unicode code point to end a slice (exclusive). + From c9fd8cbaa96fe8ada63c1dafd8687c957411904c Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Fri, 28 Feb 2025 06:51:50 +0000 Subject: [PATCH 6/9] docs: add missing line in repl --- 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: passed - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - 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 --- --- 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/base/slice-code-points/docs/repl.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/docs/repl.txt b/lib/node_modules/@stdlib/string/base/slice-code-points/docs/repl.txt index fbfaca1173f8..6647a36a9044 100644 --- a/lib/node_modules/@stdlib/string/base/slice-code-points/docs/repl.txt +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/docs/repl.txt @@ -1,3 +1,4 @@ + {{alias}}( str, start, end ) Slices a string based on Unicode code point indices. From b7d92f7de690f193769dcf4604a229316ba8b4e0 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sat, 15 Mar 2025 11:08:45 +0000 Subject: [PATCH 7/9] refactor: use related dep --- 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: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - 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 --- --- .../string/base/slice-code-points/lib/main.js | 25 +++---------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js b/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js index cdce4cfa4f6b..8370898e30d0 100644 --- a/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js @@ -20,7 +20,8 @@ // MODULES // -var max = require( '@stdlib/math/base/special/max' ); +var numCodePoints = require( '@stdlib/string/num-code-points' ); +var max = require( '@stdlib/math/base/special/fast/max' ); // VARIABLES // @@ -57,43 +58,23 @@ function sliceCodePoints( str, start, end ) { var ch; var i; - if ( arguments.length !== 3 ) { - throw new TypeError( 'invalid invocation. Must provide a valid string, starting code point index, and ending code point index.' ); - } - len = str.length; if ( len === 0 ) { return ''; } - - totalCodePoints = 0; - for ( i = 0; i < len; i++ ) { - ch1 = str[ i ]; - totalCodePoints += 1; - - if ( RE_UTF16_HIGH_SURROGATE.test( ch1 ) && i < len - 1 ) { - ch2 = str[ i + 1 ]; - if ( RE_UTF16_LOW_SURROGATE.test( ch2 ) ) { - i += 1; - } - } - } - + totalCodePoints = numCodePoints( str ); if ( start < 0 ) { start = max( totalCodePoints + start, 0 ); } if ( end < 0 ) { end = max( totalCodePoints + end, 0 ); } - if ( start >= totalCodePoints || end <= start ) { return ''; } - if ( end > totalCodePoints ) { end = totalCodePoints; } - codePoints = ''; idx = 0; From 94a32ff1aab0eeab871b91530a58fe474e4c4e98 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Tue, 1 Apr 2025 04:50:48 +0000 Subject: [PATCH 8/9] refactor: apply changes 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: passed - task: lint_javascript_cli status: na - 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 --- --- .../string/base/slice-code-points/lib/main.js | 6 ++--- .../base/slice-code-points/test/test.js | 22 ------------------- 2 files changed, 2 insertions(+), 26 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js b/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js index 8370898e30d0..10eb3b22dad1 100644 --- a/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js @@ -38,7 +38,6 @@ var RE_UTF16_HIGH_SURROGATE = /[\uD800-\uDBFF]/; // TODO: replace with stdlib pk * @param {string} str - input string * @param {integer} start - the `ith` Unicode code point to start a slice (inclusive) * @param {integer} end - the `jth` Unicode code point to end a slice (exclusive) -* @throws {TypeError} must provide exactly 3 arguments * @returns {string} output string * * @example @@ -68,13 +67,12 @@ function sliceCodePoints( str, start, end ) { } if ( end < 0 ) { end = max( totalCodePoints + end, 0 ); + } else if ( end > totalCodePoints ) { + end = totalCodePoints; } if ( start >= totalCodePoints || end <= start ) { return ''; } - if ( end > totalCodePoints ) { - end = totalCodePoints; - } codePoints = ''; idx = 0; diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/test/test.js b/lib/node_modules/@stdlib/string/base/slice-code-points/test/test.js index d6b71aec2436..1ba51bf50729 100644 --- a/lib/node_modules/@stdlib/string/base/slice-code-points/test/test.js +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/test/test.js @@ -44,28 +44,6 @@ tape( 'the function returns an empty string if provided an empty string', functi t.end(); }); -tape( 'the function throws an error if provided an unsupported number of arguments', function test( t ) { - var values; - var i; - - values = [ - [ 'foo', 1 ], - [ 'bar' ], - [ 'beep', 1, 3, 5 ] - ]; - - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue.apply( null, values[i] ), TypeError, 'throws an error when provided unsupported number of arguments' ); - } - t.end(); - - function badValue() { - return function badValue() { - sliceCodePoints.apply( null, arguments ); - }; - } -}); - tape( 'the function slices a string based on code point indices', function test( t ) { var str; var out; From 730c1e013c0d9d30d17db6a8a1263fdcbf110d9a Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sun, 6 Apr 2025 07:52:02 +0000 Subject: [PATCH 9/9] refactor: break out of loop early --- 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: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - 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/base/slice-code-points/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js b/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js index 10eb3b22dad1..e1d4c6496949 100644 --- a/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/slice-code-points/lib/main.js @@ -77,7 +77,7 @@ function sliceCodePoints( str, start, end ) { idx = 0; // Process the string one Unicode code unit at a time and handle UTF-16 surrogate pairs as a single Unicode code point... - for ( i = 0; i < len; i++ ) { + for ( i = 0; i < len && idx < end; i++ ) { ch1 = str[ i ]; ch = ch1;