diff --git a/lib/node_modules/@stdlib/string/base/truncate-code-point/README.md b/lib/node_modules/@stdlib/string/base/truncate-code-point/README.md new file mode 100644 index 000000000000..e29e045a5f5a --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/README.md @@ -0,0 +1,104 @@ + + +# truncate + +> Truncate a string to a specified length in code points. + +
+ +## Usage + +```javascript +var truncate = require( '@stdlib/string/base/truncate-code-point' ); +``` + +#### truncate( str, len, ending ) + +Truncates a string to a specified length in code points. + +```javascript +var out = truncate( 'beep boop', 7, '...' ); +// returns 'beep...' + +out = truncate( 'beep boop', 5, '>>>' ); +// returns 'be>>>' +``` + +
+ + + +
+ +## Notes + +- This function assumes that the input arguments are valid. For input validation, use the top-level [`@stdlib/string/truncate`][@stdlib/string/truncate]. + +
+ + + +
+ +## Examples + + + +```javascript +var truncate = require( '@stdlib/string/base/truncate-code-point' ); + +console.log( truncate( 'beep boop', 7, '...' ) ); +// => 'beep...' + +console.log( truncate( 'beep boop', 5, '>>>' ) ); +// => 'be>>>' + +console.log( truncate( '🐺 Wolf Brothers 🐺', 7, '...' ) ); +// => '🐺 Wo...' + +console.log( truncate( '🐶🐮🐷🐰🐸', 2, '...' ) ); +// => '..' + +console.log( truncate( '🐶🐮🐷🐰🐸', 10, '...' ) ); +// => '🐶🐮🐷🐰🐸' +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/truncate-code-point/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/truncate-code-point/benchmark/benchmark.js new file mode 100644 index 000000000000..1ff3214ecc3a --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/benchmark/benchmark.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 randu = require( '@stdlib/random/base/randu' ); +var pkg = require( './../package.json' ).name; +var truncate = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var strings; + var out; + var i; + + strings = [ + 'beep boop', + 'foo bar', + 'hello world', + '🐺 Wolf Brothers 🐺', + '🐶🐮🐷🐰🐸' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = truncate( strings[ i % strings.length ], (randu() * 10)|0, '...' ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/string/base/truncate-code-point/docs/repl.txt b/lib/node_modules/@stdlib/string/base/truncate-code-point/docs/repl.txt new file mode 100644 index 000000000000..b404e52e4e6d --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/docs/repl.txt @@ -0,0 +1,32 @@ +{{alias}}( str, len, ending ) + Truncates a string to a specified length in code points (i.e., + user-perceived characters). + + Parameters + ---------- + str: string + Input string. + + len: NonNegativeInteger + Output string length (including ending). + + ending: string + Custom ending. + + Returns + ------- + out: string + Output string. + + Examples + -------- + > var out = {{alias}}( 'beep boop', 7, '...' ) + 'beep...' + > out = {{alias}}( 'beep boop', 5, '>>>' ) + 'be>>>' + > out = {{alias}}( 'foo bar', 10, '...' ) + 'foo bar' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/string/base/truncate-code-point/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/truncate-code-point/docs/types/index.d.ts new file mode 100644 index 000000000000..6c057e524e51 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 + +/** +* Truncates a string to a specified length in code points. +* +* @param str - input string +* @param len - output string length +* @param ending - custom ending +* @returns truncated string +* +* @example +* var out = truncate( 'beep boop', 7, '...' ); +* // returns 'beep...' +* +* @example +* var out = truncate( 'beep boop', 5, '>>>' ); +* // returns 'be>>>' +*/ +declare function truncate( str: string, len: number, ending: string ): string; + + +// EXPORTS // + +export = truncate; diff --git a/lib/node_modules/@stdlib/string/base/truncate-code-point/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/truncate-code-point/docs/types/test.ts new file mode 100644 index 000000000000..02a9ffaf41ba --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/docs/types/test.ts @@ -0,0 +1,71 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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 truncate = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + truncate( 'beep boop', 5, '...' ); // $ExpectType string +} + +// The compiler throws an error if the function is provided a first argument which is not a string... +{ + truncate( 5, 5, '...' ); // $ExpectError + truncate( false, 5, '...' ); // $ExpectError + truncate( true, 5, '...' ); // $ExpectError + truncate( null, 5, '...' ); // $ExpectError + truncate( undefined, 5, '...' ); // $ExpectError + truncate( [], 5, '...' ); // $ExpectError + truncate( {}, 5, '...' ); // $ExpectError + truncate( ( x: number ): number => x, 5, '...' ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + truncate( 'beep boop', '5', '...' ); // $ExpectError + truncate( 'beep boop', false, '...' ); // $ExpectError + truncate( 'beep boop', true, '...' ); // $ExpectError + truncate( 'beep boop', null, '...' ); // $ExpectError + truncate( 'beep boop', undefined, '...' ); // $ExpectError + truncate( 'beep boop', [], '...' ); // $ExpectError + truncate( 'beep boop', {}, '...' ); // $ExpectError + truncate( 'beep boop', ( x: number ): number => x, '...' ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a string... +{ + truncate( 'beep boop', 5, 5 ); // $ExpectError + truncate( 'beep boop', 5, false ); // $ExpectError + truncate( 'beep boop', 5, true ); // $ExpectError + truncate( 'beep boop', 5, null ); // $ExpectError + truncate( 'beep boop', 5, undefined ); // $ExpectError + truncate( 'beep boop', 5, [] ); // $ExpectError + truncate( 'beep boop', 5, {} ); // $ExpectError + truncate( 'beep boop', 5, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + truncate(); // $ExpectError + truncate( 'beep boop' ); // $ExpectError + truncate( 'beep boop', 5 ); // $ExpectError + truncate( 'beep boop', 5, '...', 10 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/base/truncate-code-point/examples/index.js b/lib/node_modules/@stdlib/string/base/truncate-code-point/examples/index.js new file mode 100644 index 000000000000..85b737360fdd --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/examples/index.js @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 truncate = require( './../lib' ); + +console.log( truncate( 'beep boop', 7, '...' ) ); +// => 'beep...' + +console.log( truncate( 'beep boop', 5, '>>>' ) ); +// => 'be>>>' + +console.log( truncate( '🐺 Wolf Brothers 🐺', 7, '...' ) ); +// => '🐺 Wo...' + +console.log( truncate( '🐶🐮🐷🐰🐸', 2, '...' ) ); +// => '..' + +console.log( truncate( '🐶🐮🐷🐰🐸', 10, '...' ) ); +// => '🐶🐮🐷🐰🐸' diff --git a/lib/node_modules/@stdlib/string/base/truncate-code-point/lib/index.js b/lib/node_modules/@stdlib/string/base/truncate-code-point/lib/index.js new file mode 100644 index 000000000000..f6e4618c5dc3 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; + +/** +* Truncate a string to a specified length in code points. +* +* @module @stdlib/string/base/truncate-code-point +* +* @example +* var truncate = require( '@stdlib/string/base/truncate-code-point' ); +* +* var str = truncate( 'beep boop', 7, '...' ); +* // returns 'beep...' +* +* str = truncate( 'beep boop', 5, '>>>' ); +* // returns 'be>>>' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/base/truncate-code-point/lib/main.js b/lib/node_modules/@stdlib/string/base/truncate-code-point/lib/main.js new file mode 100644 index 000000000000..17d55d782bf8 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/lib/main.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 numCodePoints = require( '@stdlib/string/num-code-points' ); +var nextCodePointIndex = require( '@stdlib/string/next-code-point-index' ); + + +// MAIN // + +/** +* Truncates a string to a specified length in code points. +* +* @param {string} str - input string +* @param {NonNegativeInteger} len - output string length (including ending) +* @param {string} ending - custom ending +* @returns {string} truncated string +* +* @example +* var out = truncate( 'beep boop', 7, '...' ); +* // returns 'beep...' +* +* @example +* var out = truncate( 'beep boop', 5, '>>>' ); +* // returns 'be>>>' +* +* @example +* var out = truncate( 'beep boop', 10, '...' ); +* // returns 'beep boop' +* +* @example +* var out = truncate( 'beep boop', 0, '...' ); +* // returns '' +* +* @example +* var out = truncate( 'beep boop', 2, '...' ); +* // returns '..' +* +* @example +* var out = truncate( '🐺 Wolf Brothers 🐺', 7, '...' ); +* // returns '🐺 Wo...' +*/ +function truncate( str, len, ending ) { + var endingLength; + var nVisual; + var idx; + + endingLength = numCodePoints( ending ); + if ( len >= numCodePoints( str ) ) { + return str; + } + if ( len - endingLength <= 0 ) { + idx = 0; + nVisual = 0; + while ( nVisual < len ) { + idx = nextCodePointIndex( ending, idx ); + if ( idx === -1 ) { + idx = ending.length; + break; + } + nVisual += 1; + } + return ending.substring( 0, idx ); + } + idx = 0; + nVisual = 0; + while ( nVisual < len - endingLength ) { + idx = nextCodePointIndex( str, idx ); + if ( idx === -1 ) { + idx = str.length; + break; + } + nVisual += 1; + } + return str.substring( 0, idx ) + ending; +} + + +// EXPORTS // + +module.exports = truncate; diff --git a/lib/node_modules/@stdlib/string/base/truncate-code-point/package.json b/lib/node_modules/@stdlib/string/base/truncate-code-point/package.json new file mode 100644 index 000000000000..b55f7dc75f81 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/string/base/truncate-code-point", + "version": "0.0.0", + "description": "Truncate a string to a specified length in 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", + "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": { + "@stdlib/string/next-code-point-index": "^0.2.1", + "@stdlib/string/num-code-points": "^0.2.1" + }, + "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", + "truncate", + "trunc", + "shorten", + "short", + "code", + "point" + ] +} diff --git a/lib/node_modules/@stdlib/string/base/truncate-code-point/test/test.js b/lib/node_modules/@stdlib/string/base/truncate-code-point/test/test.js new file mode 100644 index 000000000000..dc9eb5c82473 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/test/test.js @@ -0,0 +1,143 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 truncate = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof truncate, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function truncates a string to the specified ' + + 'length', function test( t ) { + var expected; + var actual; + var str; + var len; + + str = 'beep boop'; + len = 5; + expected = 'be...'; + actual = truncate( str, len, '...' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 10; + expected = 'beep boop'; + actual = truncate( str, len, '...' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 0; + expected = ''; + actual = truncate( str, len, '...' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 1; + expected = '.'; + actual = truncate( str, len, '...' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 3; + expected = '...'; + actual = truncate( str, len, '...' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 9; + expected = 'beep boop'; + actual = truncate( str, len, '...' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🐺 Wolf Brothers 🐺'; + len = 6; + expected = '🐺 W...'; + actual = truncate( str, len, '...' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🐶🐮🐷🐰🐸'; + len = 2; + expected = '..'; + actual = truncate( str, len, '...' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🐶🐮🐷🐰🐸'; + len = 10; + expected = '🐶🐮🐷🐰🐸'; + actual = truncate( str, len, '...' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function truncates a string to the specified length (custom ' + + 'ending)', function test( t ) { + var expected; + var actual; + var str; + var len; + + str = 'beep boop'; + len = 5; + expected = 'beep|'; + actual = truncate( str, len, '|' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 10; + expected = 'beep boop'; + actual = truncate( str, len, '!' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 3; + expected = 'be!'; + actual = truncate( str, len, '!' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 5; + expected = 'beep😃'; + actual = truncate( str, len, '😃' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop foo bar'; + len = 10; + expected = 'beep 🙃 🙃 🙃'; + actual = truncate( str, len, ' 🙃 🙃 🙃' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🐺 Wolf Brothers 🐺'; + len = 8; + expected = '🐺 Wolf 🐺'; + actual = truncate( str, len, '🐺' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +});