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 00000000000..64568733835 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/README.md @@ -0,0 +1,99 @@ + + +# truncateCodePoint + +> Truncate code points of a string to a specified length. + +
+ +## Usage + +```javascript +var truncateCodePoint = require( '@stdlib/string/base/truncate-code-point' ); +``` + +#### truncateCodePoint( str, len\[, ending] ) + +Truncates code points of a string to a specified length. + +```javascript +var out = truncateCodePoint( 'beep boop', 7 ); +// returns 'beep...' +``` + +By default, the truncated string is appended with `'...'`. To customize the truncated string, provide an `ending` argument: + +```javascript +var out = truncateCodePoint( 'beep boop', 7, '!' ); +// returns 'beep b!' + +out = truncateCodePoint( 'beep boop', 7, '!!!' ); +// returns 'beep!!!' +``` + +
+ + + +
+ +## Examples + + + +```javascript +var truncate = require( '@stdlib/string/truncate' ); + +var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; +var out = truncateCodePoint( str, 14 ); +// returns 'Lorem ipsum...' + +str = 'To be or not to be, that is the question'; +out = truncateCodePoint( str, 19, '!' ); +// returns 'To be or not to be!' + +str = 'The quick fox jumps over the lazy dog.'; +out = truncateCodePoint( str, 16, '...' ); +// returns 'The quick fox...' + +out = truncateCodePoint( 'अनुच्छेद', 7 ); +// returns 'अनुच...' +``` + +
+ + + + + + + + + + + + + + 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 00000000000..d44f1e4f845 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/benchmark/benchmark.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var truncate = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var out; + var i; + + values = [ + 'beep boop', + 'foo bar', + 'xyz abc', + '🐶🐮🐷🐰🐸' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = truncate( values[ i%values.length ], 1 ); + 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/truncate-code-point/docs/repl.txt b/lib/node_modules/@stdlib/string/base/truncate-code-point/docs/repl.txt new file mode 100644 index 00000000000..3901fd76e3e --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( str, len[, ending] ) + Truncate code points of a string to a specified length. + + Parameters + ---------- + str: string + Input string. + + len: integer + Output string length. + + ending: string (optional) + Custom ending. Default: '...'. + + Returns + ------- + out: string + Output string. + + Examples + -------- + > var str = 'beep boop'; + > var out = {{alias}}( str, 5 ) + 'be...' + + > out = {{alias}}( str, 5, '|' ) + 'beep|' + + 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 00000000000..c685e6e856b --- /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) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Truncate code points of a string to a specified length. +* +* @param str - input string +* @param len - output string length (including ending) +* @param ending - custom ending (default: `...`) +* @returns truncated string +* +* @example +* var out = truncate( 'beep boop', 7 ); +* // returns 'beep...' +* +* @example +* var out = truncate( 'beep boop', 7, '|' ); +* // returns 'beep b|' +*/ +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 00000000000..ae2ff2f94f8 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/docs/types/test.ts @@ -0,0 +1,69 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import truncate = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + truncate( 'abc', 1 ); // $ExpectType string + truncate( 'abcdefghi', 10, '|' ); // $ExpectType string +} + +// The compiler throws an error if the function is provided a value other than a string as its first argument... +{ + truncate( true, 1 ); // $ExpectError + truncate( false, 1 ); // $ExpectError + truncate( null, 1 ); // $ExpectError + truncate( undefined, 1 ); // $ExpectError + truncate( 5, 1 ); // $ExpectError + truncate( [], 1 ); // $ExpectError + truncate( {}, 1 ); // $ExpectError + truncate( ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument that is not a number... +{ + truncate( 'abc', true ); // $ExpectError + truncate( 'abc', false ); // $ExpectError + truncate( 'abc', null ); // $ExpectError + truncate( 'abc', 'abc' ); // $ExpectError + truncate( 'abc', [] ); // $ExpectError + truncate( 'abc', {} ); // $ExpectError + truncate( 'abc', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument that is not a string... +{ + truncate( 'beep boop', 4, true ); // $ExpectError + truncate( 'beep boop', 4, false ); // $ExpectError + truncate( 'beep boop', 4, null ); // $ExpectError + truncate( 'beep boop', 4, 123 ); // $ExpectError + truncate( 'beep boop', 4, [], 0 ); // $ExpectError + truncate( 'beep boop', 4, {}, 0 ); // $ExpectError + truncate( 'beep boop', 4, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + truncate(); // $ExpectError + truncate( 'abc' ); // $ExpectError + truncate( 'abc', 1, '.', true ); // $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 00000000000..9486ed3a109 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/examples/index.js @@ -0,0 +1,41 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var truncate = require( './../lib' ); + +var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; +var out = truncate( str, 14 ); +console.log( out ); +// => 'Lorem ipsum...' + +str = 'To be or not to be, that is the question'; +out = truncate( str, 19, '!' ); +console.log( out ); +// => 'To be or not to be!' + +str = 'The quick fox jumps over the lazy dog.'; +out = truncate( str, 16, '...' ); +console.log( out ); +// => 'The quick fox...' + +str = 'अनुच्छेद'; +out = truncate( str, 7, '...' ); +console.log( out ); +// => 'अनुच...' 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 00000000000..1e899a3d7ad --- /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) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Truncate code points of a string to a specified length. +* +* @module @stdlib/string/base/truncate-code-point +* +* @example +* var truncate = require( '@stdlib/string/base/truncate-code-point' ); +* +* var out = truncate( 'beep boop', 7 ); +* // returns 'beep...' +* +* out = truncate( 'beep boop', 7, '|' ); +* // returns 'beep b|' +*/ + +// 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 00000000000..211e7b8c36c --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/lib/main.js @@ -0,0 +1,122 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var numCodePoints = require( '@stdlib/string/num-code-points' ); + + +// 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 // + +/** +* Truncates code point of a string to a specified length. +* +* @param {string} str - input string +* @param {NonNegativeInteger} len - output string length (including ending) +* @param {string} [ending='...'] - custom ending +* @returns {string} truncated string +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 7 ); +* // returns 'beep...' +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 5, '>>>' ); +* // returns 'be>>>' +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 10 ); +* // returns 'beep boop' +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 0 ); +* // returns '' +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 2 ); +* // returns '..' +*/ +function truncate( str, len, ending ) { + var endingLength; + var strLength; + var ch1; + var ch2; + var out; + var i; + + if ( typeof ending === 'undefined' ) { + ending = '...'; + } + if ( len <= 0 ) { + return ''; + } + strLength = numCodePoints( str ); + if ( len >= strLength ) { + return str; + } + endingLength = numCodePoints( ending ); + if ( endingLength >= len ) { + str = ending; + ending = ''; + endingLength = 0; + } + out = ''; + + // 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 < len - endingLength; i++ ) { + ch1 = str[ i ]; + + // Check for a high UTF-16 surrogate... + if ( RE_UTF16_HIGH_SURROGATE.test( ch1 ) ) { + // Check for an unpaired surrogate at the end of the input string... + if ( i === len - endingLength - 1 ) { + // We found an unpaired surrogate... + break; + } + // 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: + i += 1; // bump the index to process the next code unit + out += ch1 + ch2; + } + } + else { + out += ch1; + } + } + return out + 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 00000000000..2ab3fd4ec3c --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/string/base/truncate-code-point", + "version": "0.0.0", + "description": "Truncate code points of a string to a specified length.", + "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": {}, + "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", + "pad", + "string", + "str", + "truncate", + "trunc", + "shorten", + "short" + ] +} 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 00000000000..d053b2a4fb1 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-code-point/test/test.js @@ -0,0 +1,174 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var 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 = 'अनुच्छेद'; + len = 7; + expected = 'अनुच...'; + actual = truncate( str, len ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '六书/六書'; + len = 4; + expected = '六...'; + actual = truncate( str, len ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '𐒻𐓟𐒻𐓟𐒻𐓟𐒻𐓟𐒻𐓟'; + len = 5; + expected = '𐒻...'; + actual = truncate( str, len ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '\uD800𐓟𐒻𐓟𐒻𐓟𐒻𐓟𐒻𐓟'; + len = 4; + 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 = 'अनुच्छेद'; + len = 7; + expected = 'अनुच् &'; + actual = truncate( str, len, ' &' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '六书/六書'; + len = 4; + expected = '六书/&'; + actual = truncate( str, len, '&' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '六书/六'; + len = 4; + expected = '六书/六'; + actual = truncate( str, len, '書' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '六书/六'; + len = 4; + expected = '六书/六'; + actual = truncate( str, len, '' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function truncates a string to the specified length (truncate ending)', function test( t ) { + var expected; + var actual; + var str; + var len; + + str = 'beep boop'; + len = 5; + 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 = 'aaa '; + len = 2; + expected = 'अन'; + actual = truncate( str, len, 'अनुच्छेद' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '六书/六'; + len = 3; + expected = '六书書'; + actual = truncate( str, len, '書' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/README.md b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/README.md new file mode 100644 index 00000000000..4be52003238 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/README.md @@ -0,0 +1,102 @@ + + +# truncateGraphemeCluster + +> Truncate grapheme clusters of a string to a specified length. + +
+ +## Usage + +```javascript +var truncateGraphemeCluster = require( '@stdlib/string/base/truncate-grapheme-cluster' ); +``` + +#### truncateGraphemeCluster( str, len\[, ending] ) + +Truncates grapheme clusters of a string to a specified length. + +```javascript +var out = truncateGraphemeCluster( 'beep boop', 7 ); +// returns 'beep...' +``` + +By default, the truncated string is appended with `'...'`. To customize the truncated string, provide an `ending` argument: + +```javascript +var out = truncateGraphemeCluster( 'beep boop', 7, '!' ); +// returns 'beep b!' + +out = truncateGraphemeCluster( 'beep boop', 7, '!!!' ); +// returns 'beep!!!' +``` + +
+ + + +
+ +## Examples + + + +```javascript +var truncate = require( '@stdlib/string/truncate' ); + +var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; +var out = truncateGraphemeCluster( str, 14 ); +// returns 'Lorem ipsum...' + +str = 'To be or not to be, that is the question'; +out = truncateGraphemeCluster( str, 19, '!' ); +// returns 'To be or not to be!' + +str = 'The quick fox jumps over the lazy dog.'; +out = truncateGraphemeCluster( str, 16, '...' ); +// returns 'The quick fox...' + +out = truncateGraphemeCluster( 'अनुच्छेद', 4 ); +// returns 'अ...' + +out = truncateGraphemeCluster( '🌷🌷🌷🌷🌷', 4 ); +// returns '🌷...' +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/benchmark/benchmark.js new file mode 100644 index 00000000000..d44f1e4f845 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/benchmark/benchmark.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var truncate = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var out; + var i; + + values = [ + 'beep boop', + 'foo bar', + 'xyz abc', + '🐶🐮🐷🐰🐸' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = truncate( values[ i%values.length ], 1 ); + 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/truncate-grapheme-cluster/docs/repl.txt b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/docs/repl.txt new file mode 100644 index 00000000000..82ee003417a --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( str, len[, ending] ) + Truncate grapheme clusters of a string to a specified length. + + Parameters + ---------- + str: string + Input string. + + len: integer + Output string length. + + ending: string (optional) + Custom ending. Default: '...'. + + Returns + ------- + out: string + Output string. + + Examples + -------- + > var str = 'beep boop'; + > var out = {{alias}}( str, 5 ) + 'be...' + + > out = {{alias}}( str, 5, '|' ) + 'beep|' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/docs/types/index.d.ts new file mode 100644 index 00000000000..1cbd0e75d77 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Truncate grapheme clusters of a string to a specified length. +* +* @param str - input string +* @param len - output string length (including ending) +* @param ending - custom ending (default: `...`) +* @returns truncated string +* +* @example +* var out = truncate( 'beep boop', 7 ); +* // returns 'beep...' +* +* @example +* var out = truncate( 'beep boop', 7, '|' ); +* // returns 'beep b|' +*/ +declare function truncate( str: string, len: number, ending?: string ): string; + + +// EXPORTS // + +export = truncate; diff --git a/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/docs/types/test.ts new file mode 100644 index 00000000000..ae2ff2f94f8 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/docs/types/test.ts @@ -0,0 +1,69 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import truncate = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + truncate( 'abc', 1 ); // $ExpectType string + truncate( 'abcdefghi', 10, '|' ); // $ExpectType string +} + +// The compiler throws an error if the function is provided a value other than a string as its first argument... +{ + truncate( true, 1 ); // $ExpectError + truncate( false, 1 ); // $ExpectError + truncate( null, 1 ); // $ExpectError + truncate( undefined, 1 ); // $ExpectError + truncate( 5, 1 ); // $ExpectError + truncate( [], 1 ); // $ExpectError + truncate( {}, 1 ); // $ExpectError + truncate( ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument that is not a number... +{ + truncate( 'abc', true ); // $ExpectError + truncate( 'abc', false ); // $ExpectError + truncate( 'abc', null ); // $ExpectError + truncate( 'abc', 'abc' ); // $ExpectError + truncate( 'abc', [] ); // $ExpectError + truncate( 'abc', {} ); // $ExpectError + truncate( 'abc', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument that is not a string... +{ + truncate( 'beep boop', 4, true ); // $ExpectError + truncate( 'beep boop', 4, false ); // $ExpectError + truncate( 'beep boop', 4, null ); // $ExpectError + truncate( 'beep boop', 4, 123 ); // $ExpectError + truncate( 'beep boop', 4, [], 0 ); // $ExpectError + truncate( 'beep boop', 4, {}, 0 ); // $ExpectError + truncate( 'beep boop', 4, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + truncate(); // $ExpectError + truncate( 'abc' ); // $ExpectError + truncate( 'abc', 1, '.', true ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/examples/index.js b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/examples/index.js new file mode 100644 index 00000000000..ab746954b4d --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/examples/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var truncate = require( './../lib' ); + +var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; +var out = truncate( str, 14 ); +console.log( out ); +// => 'Lorem ipsum...' + +str = 'To be or not to be, that is the question'; +out = truncate( str, 19, '!' ); +console.log( out ); +// => 'To be or not to be!' + +str = 'The quick fox jumps over the lazy dog.'; +out = truncate( str, 16, '...' ); +console.log( out ); +// => 'The quick fox...' + +str = 'अनुच्छेद'; +out = truncate( str, 4, '...' ); +console.log( out ); +// => 'अ...' + +str = '🌷🌷🌷🌷🌷'; +out = truncate( str, 4, '...' ); +console.log( out ); +// => '🌷...' diff --git a/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/lib/index.js b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/lib/index.js new file mode 100644 index 00000000000..e249fef30e2 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Truncate grapheme clusters of a string to a specified length. +* +* @module @stdlib/string/base/truncate-grapheme-cluster +* +* @example +* var truncate = require( '@stdlib/string/base/truncate-grapheme-cluster' ); +* +* var out = truncate( 'beep boop', 7 ); +* // returns 'beep...' +* +* out = truncate( 'beep boop', 7, '|' ); +* // returns 'beep b|' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/lib/main.js b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/lib/main.js new file mode 100644 index 00000000000..f136a864348 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/lib/main.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var numGraphemeClusters = require( '@stdlib/string/num-grapheme-clusters' ); +var nextGraphemeClusterBreak = require( '@stdlib/string/next-grapheme-cluster-break' ); + + +// MAIN // + +/** +* Truncates grapheme clusters of a string to a specified length. +* +* @param {string} str - input string +* @param {NonNegativeInteger} len - output string length (including ending) +* @param {string} [ending='...'] - custom ending +* @returns {string} truncated string +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 7 ); +* // returns 'beep...' +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 5, '>>>' ); +* // returns 'be>>>' +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 10 ); +* // returns 'beep boop' +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 0 ); +* // returns '' +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 2 ); +* // returns '..' +*/ +function truncate( str, len, ending ) { + var endingLength; + var strLength; + var n; + var i; + + if ( typeof ending === 'undefined' ) { + ending = '...'; + } + if ( len <= 0 ) { + return ''; + } + strLength = numGraphemeClusters( str ); + if ( len >= strLength ) { + return str; + } + endingLength = numGraphemeClusters( ending ); + if ( endingLength >= len ) { + str = ending; + ending = ''; + endingLength = 0; + } + i = 0; + n = len - endingLength; + while ( n > 0 ) { + i = nextGraphemeClusterBreak( str, i ); + n -= 1; + } + if ( i === -1 ) { + return str; + } + return str.substring( 0, i ) + ending; +} + + +// EXPORTS // + +module.exports = truncate; diff --git a/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/package.json b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/package.json new file mode 100644 index 00000000000..a4db1dc3db9 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/string/base/truncate-grapheme-cluster", + "version": "0.0.0", + "description": "Truncate grapheme clusters of a string to a specified length.", + "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": {}, + "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", + "pad", + "string", + "str", + "truncate", + "trunc", + "shorten", + "short" + ] +} diff --git a/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/test/test.js b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/test/test.js new file mode 100644 index 00000000000..47348a781c3 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-grapheme-cluster/test/test.js @@ -0,0 +1,186 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var 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 = 'अनुच्छेद'; + len = 4; + expected = 'अ...'; + actual = truncate( str, len ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '六书/六書'; + len = 4; + expected = '六...'; + actual = truncate( str, len ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🐶🐮🐷🐰🐸🐸'; + len = 5; + 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 = 'अनुच्छेद'; + len = 4; + expected = 'अनु &'; + actual = truncate( str, len, ' &' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '六书/六書'; + len = 4; + expected = '六书/&'; + actual = truncate( str, len, '&' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '六书/六'; + len = 4; + expected = '六书/六'; + actual = truncate( str, len, '書' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '六书/六'; + len = 4; + expected = '六书/六'; + actual = truncate( str, len, '' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🐶🐮🐷🐰🐸'; + len = 3; + expected = '🐶🐮🐷'; + 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' ); + + t.end(); +}); + +tape( 'the function truncates a string to the specified length (truncate ending)', function test( t ) { + var expected; + var actual; + var str; + var len; + + str = 'beep boop'; + len = 5; + 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 = 'aaa '; + len = 2; + expected = 'अनु'; + actual = truncate( str, len, 'अनुच्छेद' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '六书/六'; + len = 3; + expected = '六书書'; + actual = truncate( str, len, '書' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🐶a🐮🐷🐰🐸'; + len = 4; + expected = '🐶a🌷🌷'; + actual = truncate( str, len, '🌷🌷' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/string/base/truncate/README.md b/lib/node_modules/@stdlib/string/base/truncate/README.md new file mode 100644 index 00000000000..f8b9078f040 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate/README.md @@ -0,0 +1,94 @@ + + +# truncate + +> Truncate UTF-16 code units of a string to a specified length. + +
+ +## Usage + +```javascript +var truncate = require( '@stdlib/string/base/truncate' ); +``` + +#### truncate( str, len\[, ending] ) + +Truncates UTF-16 code units of a string to a specified length. + +```javascript +var out = truncate( 'beep boop', 7 ); +// returns 'beep...' +``` + +By default, the truncated string is appended with `'...'`. To customize the truncated string, provide an `ending` argument: + +```javascript +var out = truncate( 'beep boop', 7, '!' ); +// returns 'beep b!' + +out = truncate( 'beep boop', 7, '!!!' ); +// returns 'beep!!!' +``` + +
+ + + +
+ +## Examples + + + +```javascript +var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; +var out = truncate( str, 14 ); +// returns 'Lorem ipsum...' + +str = 'To be or not to be, that is the question'; +out = truncate( str, 19, '!' ); +// returns 'To be or not to be!' + +str = 'The quick fox jumps over the lazy dog.'; +out = truncate( str, 16, '...' ); +// returns 'The quick fox...' +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/truncate/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/truncate/benchmark/benchmark.js new file mode 100644 index 00000000000..d44f1e4f845 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate/benchmark/benchmark.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var truncate = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var out; + var i; + + values = [ + 'beep boop', + 'foo bar', + 'xyz abc', + '🐶🐮🐷🐰🐸' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = truncate( values[ i%values.length ], 1 ); + 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/truncate/docs/repl.txt b/lib/node_modules/@stdlib/string/base/truncate/docs/repl.txt new file mode 100644 index 00000000000..ee842a5fcb0 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( str, len[, ending] ) + Truncate UTF-16 code units of a string to a specified length. + + Parameters + ---------- + str: string + Input string. + + len: integer + Output string length. + + ending: string (optional) + Custom ending. Default: '...'. + + Returns + ------- + out: string + Output string. + + Examples + -------- + > var str = 'beep boop'; + > var out = {{alias}}( str, 5 ) + 'be...' + + > out = {{alias}}( str, 5, '|' ) + 'beep|' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/string/base/truncate/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/truncate/docs/types/index.d.ts new file mode 100644 index 00000000000..a0ce02e1e8e --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate/docs/types/index.d.ts @@ -0,0 +1,42 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 2.0 + +/** +* Truncate UTF-16 code units of a string to a specified length. +* +* @param str - input string +* @param len - output string length (including ending) +* @param ending - custom ending (default: `...`) +* @returns truncated string +* +* @example +* var out = truncate( 'beep boop', 7 ); +* // returns 'beep...' +* +* @example +* var out = truncate( 'beep boop', 7, '|' ); +* // returns 'beep b|' +*/ +declare function truncate( str: string, len: number, ending?: string ): string; + + +// EXPORTS // + +export = truncate; diff --git a/lib/node_modules/@stdlib/string/base/truncate/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/truncate/docs/types/test.ts new file mode 100644 index 00000000000..ae2ff2f94f8 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate/docs/types/test.ts @@ -0,0 +1,69 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import truncate = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + truncate( 'abc', 1 ); // $ExpectType string + truncate( 'abcdefghi', 10, '|' ); // $ExpectType string +} + +// The compiler throws an error if the function is provided a value other than a string as its first argument... +{ + truncate( true, 1 ); // $ExpectError + truncate( false, 1 ); // $ExpectError + truncate( null, 1 ); // $ExpectError + truncate( undefined, 1 ); // $ExpectError + truncate( 5, 1 ); // $ExpectError + truncate( [], 1 ); // $ExpectError + truncate( {}, 1 ); // $ExpectError + truncate( ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument that is not a number... +{ + truncate( 'abc', true ); // $ExpectError + truncate( 'abc', false ); // $ExpectError + truncate( 'abc', null ); // $ExpectError + truncate( 'abc', 'abc' ); // $ExpectError + truncate( 'abc', [] ); // $ExpectError + truncate( 'abc', {} ); // $ExpectError + truncate( 'abc', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument that is not a string... +{ + truncate( 'beep boop', 4, true ); // $ExpectError + truncate( 'beep boop', 4, false ); // $ExpectError + truncate( 'beep boop', 4, null ); // $ExpectError + truncate( 'beep boop', 4, 123 ); // $ExpectError + truncate( 'beep boop', 4, [], 0 ); // $ExpectError + truncate( 'beep boop', 4, {}, 0 ); // $ExpectError + truncate( 'beep boop', 4, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + truncate(); // $ExpectError + truncate( 'abc' ); // $ExpectError + truncate( 'abc', 1, '.', true ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/base/truncate/examples/index.js b/lib/node_modules/@stdlib/string/base/truncate/examples/index.js new file mode 100644 index 00000000000..71c27cec48f --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate/examples/index.js @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var truncate = require( './../lib' ); + +var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; +var out = truncate( str, 14 ); +console.log( out ); +// => 'Lorem ipsum...' + +str = 'To be or not to be, that is the question'; +out = truncate( str, 19, '!' ); +console.log( out ); +// => 'To be or not to be!' + +str = 'The quick fox jumps over the lazy dog.'; +out = truncate( str, 16, '...' ); +console.log( out ); +// => 'The quick fox...' diff --git a/lib/node_modules/@stdlib/string/base/truncate/lib/index.js b/lib/node_modules/@stdlib/string/base/truncate/lib/index.js new file mode 100644 index 00000000000..0d7d2604298 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Truncate UTF-16 code units of a string to a specified length. +* +* @module @stdlib/string/base/truncate +* +* @example +* var truncate = require( '@stdlib/string/base/truncate' ); +* +* var out = truncate( 'beep boop', 7 ); +* // returns 'beep...' +* +* out = truncate( 'beep boop', 7, '|' ); +* // returns 'beep b|' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/base/truncate/lib/main.js b/lib/node_modules/@stdlib/string/base/truncate/lib/main.js new file mode 100644 index 00000000000..8146faa67ae --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate/lib/main.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Truncates UTF-16 code units of a string to a specified length. +* +* @param {string} str - input string +* @param {NonNegativeInteger} len - output string length (including ending) +* @param {string} [ending='...'] - custom ending +* @returns {string} truncated string +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 7 ); +* // returns 'beep...' +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 5, '>>>' ); +* // returns 'be>>>' +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 10 ); +* // returns 'beep boop' +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 0 ); +* // returns '' +* +* @example +* var str = 'beep boop'; +* var out = truncate( str, 2 ); +* // returns '..' +*/ +function truncate( str, len, ending ) { + ending = ending || '...'; + if ( len >= str.length ) { + return str; + } + if ( len - ending.length < 0 ) { + return ending.slice( 0, len ); + } + return str.substring( 0, len - ending.length ) + ending; +} + + +// EXPORTS // + +module.exports = truncate; diff --git a/lib/node_modules/@stdlib/string/base/truncate/package.json b/lib/node_modules/@stdlib/string/base/truncate/package.json new file mode 100644 index 00000000000..60596d188b0 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/string/base/truncate", + "version": "0.0.0", + "description": "Truncate UTF-16 code units of a string to a specified length.", + "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": {}, + "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", + "pad", + "string", + "str", + "truncate", + "trunc", + "shorten", + "short" + ] +} diff --git a/lib/node_modules/@stdlib/string/base/truncate/test/test.js b/lib/node_modules/@stdlib/string/base/truncate/test/test.js new file mode 100644 index 00000000000..9251482b432 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate/test/test.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var 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' ); + + 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' ); + + t.end(); +}); 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 00000000000..f07f6e3d4ef --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/README.md @@ -0,0 +1,154 @@ + + +# numCodePoints + +> Return the number of 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 +``` + +
+ + + +
+ +## Examples + + + +```javascript +var numCodePoints = require( '@stdlib/string/num-code-points' ); + +var str = numCodePoints( 'last man standing' ); +// returns 17 + +str = numCodePoints( '六书/六書' ); +// returns 5 + +str = numCodePoints( 'अनुच्छेद' ); +// returns 8 +``` + +
+ + + +* * * + +
+ +## 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 00000000000..f95a5f49877 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/benchmark/benchmark.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var fromCodePoint = require( '@stdlib/string/from-code-point' ); +var UNICODE_MAX = require( '@stdlib/constants/unicode/max' ); +var randu = require( '@stdlib/random/base/randu' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var pkg = require( './../package.json' ).name; +var numCodePoints = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var str; + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + str = fromCodePoint( floor( randu() * UNICODE_MAX ) ) + 'eep boop'; + out = numCodePoints( str ); + if ( out < 0 ) { + b.fail( 'should never be a negative integer' ); + } + } + b.toc(); + 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 00000000000..714df7e4205 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/bin/cli @@ -0,0 +1,106 @@ +#!/usr/bin/env node + +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/cli/ctor' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var RE_EOL = require( '@stdlib/regexp/eol' ).REGEXP; +var numCodePoints = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var flags; + var lines; + var args; + var cli; + var i; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + return stdin( onRead ); + } + if ( flags.lines ) { + lines = args[ 0 ].split( RE_EOL ); + for ( i = 0; i < lines.length; i++ ) { + console.log( numCodePoints( lines[ i ] ) ); // eslint-disable-line no-console + } + } else { + console.log( numCodePoints( args[ 0 ] ) ); // eslint-disable-line no-console + } + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + data = data.toString(); + if ( flags.lines ) { + lines = data.split( RE_EOL ); + for ( i = 0; i < lines.length; i++ ) { + console.log( numCodePoints( lines[ i ] ) ); // eslint-disable-line no-console + } + } else { + console.log( numCodePoints( data ) ); // eslint-disable-line no-console + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/string/num-code-points/docs/repl.txt b/lib/node_modules/@stdlib/string/num-code-points/docs/repl.txt new file mode 100644 index 00000000000..ad4e0ac0605 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/docs/repl.txt @@ -0,0 +1,24 @@ + +{{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 00000000000..cf3ec894ea6 --- /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) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 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 00000000000..f08f9c4993f --- /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) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import 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 00000000000..7ecfdfa2eeb --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/docs/usage.txt @@ -0,0 +1,9 @@ + +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 00000000000..d5d5629e9e2 --- /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 00000000000..bc0959336e1 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/examples/index.js @@ -0,0 +1,30 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var numCodePoints = require( './../lib' ); + +console.log( numCodePoints( 'last man standing' ) ); +// => 17 + +console.log( numCodePoints( '六书/六書' ) ); +// => 5 + +console.log( numCodePoints( 'अनुच्छेद' ) ); +// => 8 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 00000000000..6adf273d4d8 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return the 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 00000000000..78571b0f2ef --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/lib/main.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var 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 ch1; + var ch2; + 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++ ) { + ch1 = str[ i ]; + + // Check for a high UTF-16 surrogate... + if ( RE_UTF16_HIGH_SURROGATE.test( ch1 ) ) { + // Check for an unpaired surrogate at the end of the input string... + if ( i === str.length-1 ) { + // We found an unpaired surrogate... + break; + } + // 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: + 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 00000000000..dec608eca59 --- /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 00000000000..b42c70b3b55 --- /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) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/lib/node_modules/@stdlib/string/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 00000000000..5193adf818c --- /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) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var replace = require( '@stdlib/string/replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var EXEC_PATH = require( '@stdlib/process/exec-path' ); + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface prints the number of code points', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'beep\\nboop\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '9\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-l` flag, the command-line interface prints the number of code points for individual lines', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'beep\\nboop\'; process.argv[ 3 ] = \'-l\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '4\n4\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "beep\nboop六"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '10\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream with `-l` flag, the command-line interface prints the number of code points for each line', opts, function test( t ) { + var cmd = [ + 'printf "beep\nboop六"', + '|', + EXEC_PATH, + fpath, + '-l' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '4\n5\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/lib/node_modules/@stdlib/string/num-code-points/test/test.js b/lib/node_modules/@stdlib/string/num-code-points/test/test.js new file mode 100644 index 00000000000..b9e89f29948 --- /dev/null +++ b/lib/node_modules/@stdlib/string/num-code-points/test/test.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var 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 grapheme clusters (Unpaired 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' ); + t.strictEqual( out, 0, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/string/truncate/README.md b/lib/node_modules/@stdlib/string/truncate/README.md index bf86b54fe5e..bb3e725ec66 100644 --- a/lib/node_modules/@stdlib/string/truncate/README.md +++ b/lib/node_modules/@stdlib/string/truncate/README.md @@ -40,7 +40,7 @@ limitations under the License. var truncate = require( '@stdlib/string/truncate' ); ``` -#### truncate( str, len\[, ending] ) +#### truncate( str, len\[, ending]\[, options] ) Truncates a string to a specified length. @@ -59,6 +59,16 @@ out = truncate( 'beep boop', 7, '!!!' ); // returns 'beep!!!' ``` +The function supports the following options: + +- **mode**: type of characters to return. Must be one of the following: + + - `'grapheme'`: grapheme clusters. Appropriate for strings containing visual characters which can span multiple Unicode code points (e.g., emoji). + - `'code_point'`: Unicode code points. Appropriate for strings containing visual characters which are comprised of more than one Unicode code unit (e.g., ideographic symbols and punctuation and mathematical alphanumerics). + - `'code_unit'`: UTF-16 code units. Appropriate for strings containing visual characters drawn from the basic multilingual plane (BMP) (e.g., common characters, such as those from the Latin, Greek, and Cyrillic alphabets). + + Default: `'grapheme'`. + @@ -67,6 +77,18 @@ out = truncate( 'beep boop', 7, '!!!' );
+## Notes + +- By default, the function assumes the general case in which an input string may contain an arbitrary number of grapheme clusters. This assumption comes with a performance cost. Accordingly, if an input string is known to only contain visual characters of a particular type (e.g., only alphanumeric), one can achieve better performance by specifying the appropriate `mode` option. + +
+ + + + + +
+
@@ -127,6 +149,7 @@ Options: --len length String length. --ending str Custom ending. Default: '...'. --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. + --mode mode Type of character to return. Default: 'grapheme'. ``` diff --git a/lib/node_modules/@stdlib/string/truncate/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/truncate/benchmark/benchmark.js index e7c47f509a5..48cb0e63bd4 100644 --- a/lib/node_modules/@stdlib/string/truncate/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/string/truncate/benchmark/benchmark.js @@ -22,70 +22,124 @@ var bench = require( '@stdlib/bench' ); var isString = require( '@stdlib/assert/is-string' ).isPrimitive; -var fromCodePoint = require( '@stdlib/string/from-code-point' ); var pkg = require( './../package.json' ).name; var truncate = require( './../lib' ); -// FUNCTIONS // +// MAIN // -/** -* Creates a benchmark function. -* -* @private -* @param {PositiveInteger} len - string length -* @returns {Function} benchmark function -*/ -function createBenchmark( len ) { - return benchmark; - - /** - * Benchmark function. - * - * @private - * @param {Benchmark} b - benchmark instance - */ - function benchmark( b ) { - var out; - var i; - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - out = truncate( fromCodePoint( i%126 )+'eep boop', len ); - if ( typeof out !== 'string' ) { - b.fail( 'should return a string' ); - } - } - b.toc(); - if ( !isString( out ) ) { +bench( pkg, function benchmark( b ) { + var values; + var out; + var i; + + values = [ + 'beep boop', + 'foo bar', + 'xyz abc' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = truncate( values[ i%values.length ], 5 ); + if ( typeof out !== 'string' ) { b.fail( 'should return a string' ); } - b.pass( 'benchmark finished' ); - b.end(); } -} + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); +bench( pkg+':mode=grapheme', function benchmark( b ) { + var values; + var opts; + var out; + var i; -// MAIN // + values = [ + 'beep boop', + 'foo bar', + 'xyz abc' + ]; + opts = { + 'mode': 'grapheme' + }; -/** -* Main execution sequence. -* -* @private -*/ -function main() { - var min; - var max; - var f; + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = truncate( values[ i%values.length ], 5, opts ); + 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(); +}); + +bench( pkg+':mode=code_point', function benchmark( b ) { + var values; + var opts; + var out; var i; - min = 1; - max = 10; + values = [ + 'beep boop', + 'foo bar', + 'xyz abc' + ]; + opts = { + 'mode': 'code_point' + }; - for ( i = min; i <= max; i++ ) { - f = createBenchmark( i ); - bench( pkg+':len='+i, f ); + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = truncate( values[ i%values.length ], 5, opts ); + 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(); +}); -main(); +bench( pkg+':mode=code_unit', function benchmark( b ) { + var values; + var opts; + var out; + var i; + + values = [ + 'beep boop', + 'foo bar', + 'xyz abc' + ]; + opts = { + 'mode': 'code_unit' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = truncate( values[ i%values.length ], 5, opts ); + 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/truncate/bin/cli b/lib/node_modules/@stdlib/string/truncate/bin/cli index a306c0c52a1..1502e2b731e 100644 --- a/lib/node_modules/@stdlib/string/truncate/bin/cli +++ b/lib/node_modules/@stdlib/string/truncate/bin/cli @@ -42,9 +42,11 @@ var truncate = require( './../lib' ); * @returns {void} */ function main() { + var ending; var split; var flags; var args; + var opts; var cli; var len; @@ -62,6 +64,16 @@ function main() { if ( flags.help || flags.version ) { return; } + if ( flags.ending ) { + ending = flags.ending; + } + else { + ending = '...'; + } + opts = {}; + if ( flags.mode ) { + opts.mode = flags.mode; + } // Get any provided command-line arguments: args = cli.args(); @@ -80,10 +92,10 @@ function main() { } return stdin( onRead ); } - if ( flags.ending ) { - console.log( truncate( args[ 0 ], len, flags.ending ) ); // eslint-disable-line no-console - } else { - console.log( truncate( args[ 0 ], len ) ); // eslint-disable-line no-console + try { + console.log( truncate( args[ 0 ], len, ending, opts ) ); // eslint-disable-line no-console + } catch ( error ) { + return cli.error( error ); } /** @@ -106,15 +118,16 @@ function main() { if ( lines[ lines.length-1 ] === '' ) { lines.pop(); } - if ( flags.ending ) { - for ( i = 0; i < lines.length; i++ ) { - console.log( truncate( lines[ i ], len, flags.ending ) ); // eslint-disable-line no-console - } - } else { - for ( i = 0; i < lines.length; i++ ) { - console.log( truncate( lines[ i ], len ) ); // eslint-disable-line no-console + if ( lines.length ) { + try { + console.log( truncate( lines[ 0 ], len, ending, opts ) ); // eslint-disable-line no-console + } catch ( error ) { + return cli.error( error ); } } + for ( i = 1; i < lines.length; i++ ) { + console.log( truncate( lines[ i ], len, ending, opts ) ); // eslint-disable-line no-console + } } } diff --git a/lib/node_modules/@stdlib/string/truncate/docs/repl.txt b/lib/node_modules/@stdlib/string/truncate/docs/repl.txt index 0ba0c4f35b6..5ea94d4977d 100644 --- a/lib/node_modules/@stdlib/string/truncate/docs/repl.txt +++ b/lib/node_modules/@stdlib/string/truncate/docs/repl.txt @@ -1,5 +1,5 @@ -{{alias}}( str, len[, ending] ) +{{alias}}( str, len[, ending][, options] ) Truncates a string to a specified length. Parameters @@ -13,6 +13,25 @@ ending: string (optional) Custom ending. Default: '...'. + options: Object (optional) + Options. + + options.mode: string (optional) + Type of characters to return. The following modes are supported: + + - grapheme: grapheme clusters. Appropriate for strings containing visual + characters which can span multiple Unicode code points (e.g., emoji). + - code_point: Unicode code points. Appropriate for strings containing + visual characters which are comprised of more than one Unicode code + unit (e.g., ideographic symbols and punctuation and mathematical + alphanumerics). + - code_unit': UTF-16 code units. Appropriate for strings containing + visual characters drawn from the basic multilingual plane (BMP) (e.g., + common characters, such as those from the Latin, Greek, and Cyrillic + alphabets). + + Default: 'grapheme'. + Returns ------- out: string diff --git a/lib/node_modules/@stdlib/string/truncate/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/truncate/docs/types/index.d.ts index ad332539b54..7fd6251f594 100644 --- a/lib/node_modules/@stdlib/string/truncate/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/string/truncate/docs/types/index.d.ts @@ -20,6 +20,48 @@ /// +// tslint:disable:unified-signatures + +/** +* Interface describing function options. +*/ +interface Options { + /** + * Specifies the type of characters to return (default: 'grapheme'). + * + * ## Notes + * + * - The following option values are supported: + * + * - `'grapheme'`: grapheme clusters. Appropriate for strings containing visual characters which can span multiple Unicode code points (e.g., emoji). + * - `'code_point'`: Unicode code points. Appropriate for strings containing visual characters which are comprised of more than one Unicode code unit (e.g., ideographic symbols and punctuation and mathematical alphanumerics). + * - `'code_unit'`: UTF-16 code units. Appropriate for strings containing visual characters drawn from the basic multilingual plane (BMP) (e.g., common characters, such as those from the Latin, Greek, and Cyrillic alphabets). + */ + mode?: 'grapheme' | 'code_point' | 'code_unit'; +} + +/** +* Truncates a string to a specified length. +* +* @param str - input string +* @param len - output string length (including ending) +* @param options - options +* @returns truncated string +* +* @example +* var out = truncate( 'beep boop', 7, { +* 'mode': 'code_unit' +* }); +* // returns 'beep...' +* +* @example +* var out = truncate( '🐶🐮🐷🐰🐸', 5, { +* 'mode': 'grapheme' +* }); +* // returns '🐶🐮...' +*/ +declare function truncate( str: string, len: number, options?: Options ): string; + /** * Truncates a string to a specified length. * @@ -38,6 +80,27 @@ */ declare function truncate( str: string, len: number, ending?: string ): string; +/** +* Truncates a string to a specified length. +* +* @param str - input string +* @param len - output string length (including ending) +* @param ending - custom ending (default: `...`) +* @returns truncated string +* +* @example +* var out = truncate( 'beep boop', 7, { +* 'mode': 'code_unit' +* }); +* // returns 'beep...' +* +* @example +* var out = truncate( 'beep boop', 7, '|', { +* 'mode': 'grapheme' +* }); +* // returns 'beep b|' +*/ +declare function truncate( str: string, len: number, ending: string, options?: Options ): string; // EXPORTS // diff --git a/lib/node_modules/@stdlib/string/truncate/docs/types/test.ts b/lib/node_modules/@stdlib/string/truncate/docs/types/test.ts index d97d791827c..7dda55a3e27 100644 --- a/lib/node_modules/@stdlib/string/truncate/docs/types/test.ts +++ b/lib/node_modules/@stdlib/string/truncate/docs/types/test.ts @@ -25,6 +25,8 @@ import truncate = require( './index' ); { truncate( 'abcdefghi', 3 ); // $ExpectType string truncate( 'abcdefghi', 10, '|' ); // $ExpectType string + truncate( 'abcdefghi', 5, {} ); // $ExpectType string + truncate( 'abcdefghi', 5, '~', {} ); // $ExpectType string } // The compiler throws an error if the function is not provided a string as its first argument... @@ -57,8 +59,19 @@ import truncate = require( './index' ); truncate( 'beep boop', 4, ( x: number ): number => x, 0 ); // $ExpectError } +// The compiler throws an error if the function is provided an invalid `mode` option... +{ + truncate( 'abc', 1, '~', { 'mode': true } ); // $ExpectError + truncate( 'abc', 1, '~', { 'mode': false } ); // $ExpectError + truncate( 'abc', 1, '~', { 'mode': null } ); // $ExpectError + truncate( 'abc', 1, '~', { 'mode': '' } ); // $ExpectError + truncate( 'abc', 1, '~', { 'mode': [] } ); // $ExpectError + truncate( 'abc', 1, '~', { 'mode': {} } ); // $ExpectError + truncate( 'abc', 1, '~', { 'mode': ( x: number ): number => x } ); // $ExpectError +} + // The compiler throws an error if the function is provided an unsupported number of arguments... { truncate(); // $ExpectError - truncate( 'abc', 4, '|', true ); // $ExpectError + truncate( 'abc', 4, '|', {}, true ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/string/truncate/docs/usage.txt b/lib/node_modules/@stdlib/string/truncate/docs/usage.txt index 2f3734bdadb..2b741cd5ec6 100644 --- a/lib/node_modules/@stdlib/string/truncate/docs/usage.txt +++ b/lib/node_modules/@stdlib/string/truncate/docs/usage.txt @@ -8,3 +8,4 @@ Options: --len length String length. --ending str Custom ending. Default: '...'. --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. + --mode mode Type of character to return. Default: 'grapheme'. diff --git a/lib/node_modules/@stdlib/string/truncate/etc/cli_opts.json b/lib/node_modules/@stdlib/string/truncate/etc/cli_opts.json index 5dd6d7cb540..4e441a45ae2 100644 --- a/lib/node_modules/@stdlib/string/truncate/etc/cli_opts.json +++ b/lib/node_modules/@stdlib/string/truncate/etc/cli_opts.json @@ -2,7 +2,8 @@ "string": [ "len", "ending", - "split" + "split", + "mode" ], "boolean": [ "help", diff --git a/lib/node_modules/@stdlib/string/truncate/lib/main.js b/lib/node_modules/@stdlib/string/truncate/lib/main.js index 5aadc30d4da..26cbc74231b 100644 --- a/lib/node_modules/@stdlib/string/truncate/lib/main.js +++ b/lib/node_modules/@stdlib/string/truncate/lib/main.js @@ -22,11 +22,26 @@ var isString = require( '@stdlib/assert/is-string' ).isPrimitive; var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; -var numGraphemeClusters = require( '@stdlib/string/num-grapheme-clusters' ); -var nextGraphemeClusterBreak = require( '@stdlib/string/next-grapheme-cluster-break' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var contains = require( '@stdlib/array/base/assert/contains' ).factory; +var truncateGraphemeCluster = require( '@stdlib/string/base/truncate-grapheme-cluster' ); +var truncateCodePoint = require( '@stdlib/string/base/truncate-code-point' ); +var truncateCodeUnit = require( '@stdlib/string/base/truncate' ); var format = require( '@stdlib/string/format' ); +// VARIABLES // + +var MODES = [ 'grapheme', 'code_point', 'code_unit' ]; +var FCNS = { + 'grapheme': truncateGraphemeCluster, + 'code_point': truncateCodePoint, + 'code_unit': truncateCodeUnit +}; +var isMode = contains( MODES ); + + // MAIN // /** @@ -35,9 +50,13 @@ var format = require( '@stdlib/string/format' ); * @param {string} str - input string * @param {integer} len - output string length (including ending) * @param {string} [ending='...'] - custom ending +* @param {Options} [options] - options +* @param {string} [options.mode="grapheme"] - type of "character" to return (must be either `grapheme`, `code_point`, or `code_unit`) * @throws {TypeError} first argument must be a string * @throws {TypeError} second argument must be a nonnegative integer * @throws {TypeError} third argument must be a string +* @throws {TypeError} options argument must be an object +* @throws {TypeError} must provide valid options * @returns {string} truncated string * * @example @@ -70,38 +89,53 @@ var format = require( '@stdlib/string/format' ); * var out = truncate( str, 6 ); * // returns '🐺 W...' */ -function truncate( str, len, ending ) { - var endingLength; - var fromIndex; - var nVisual; - var idx; +function truncate( str, len ) { + var options; + var ending; + var nargs; + var opts; + if ( !isString( str ) ) { throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); } if ( !isNonNegativeInteger( len ) ) { throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', len ) ); } - if ( arguments.length > 2 ) { + opts = { + 'mode': 'grapheme' + }; + nargs = arguments.length; + + if (nargs === 2 ) { + ending = '...'; + } else if ( nargs === 3 ) { + ending = arguments[ 2 ]; + if ( isPlainObject( ending )) { + options = ending; + ending = '...'; + } else if ( !isString( ending ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a string. Value: `%s`.', str ) ); + } + } else { + ending = arguments[ 2 ]; if ( !isString( ending ) ) { - throw new TypeError( format( 'invalid argument. Third argument must be a string. Value: `%s`.', ending ) ); + throw new TypeError( format( 'invalid argument. Third argument must be a string. Value: `%s`.', str ) ); + } + options = arguments[ 3 ]; + if ( !isPlainObject( options ) ) { + console.log('!!!!!!!'); + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); } } - ending = ending || '...'; - endingLength = numGraphemeClusters( ending ); - fromIndex = 0; - if ( len > numGraphemeClusters( str ) ) { - return str; - } - if ( len - endingLength < 0 ) { - return ending.slice( 0, len ); - } - nVisual = 0; - while ( nVisual < len - endingLength ) { - idx = nextGraphemeClusterBreak( str, fromIndex ); - fromIndex = idx; - nVisual += 1; + if ( options ) { + if ( hasOwnProp( options, 'mode' ) ) { + opts.mode = options.mode; + if ( !isMode( opts.mode ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Value: `%s`.', 'mode', MODES.join( '", "' ), opts.mode ) ); + } + } } - return str.substring( 0, idx ) + ending; + return FCNS[ opts.mode ]( str, len, ending ); } diff --git a/lib/node_modules/@stdlib/string/truncate/test/test.cli.js b/lib/node_modules/@stdlib/string/truncate/test/test.cli.js index 75d1c10b824..b651b696030 100644 --- a/lib/node_modules/@stdlib/string/truncate/test/test.cli.js +++ b/lib/node_modules/@stdlib/string/truncate/test/test.cli.js @@ -190,6 +190,46 @@ tape( 'the command-line interface truncates a string (custom ending)', opts, fun } }); +tape( 'the command-line interface supports specifying the type of characters to return', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'beep boop\'; process.argv[ 3 ] = \'--len=4\'; process.argv[ 4 ] = \'--mode=code_point\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'b...\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'if provided an invalid option, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'beep\'; process.argv[ 3 ] = \'--len=4\'; process.argv[ 4 ] = \'--mode=foo\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), 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().length > 0, true, 'expected value' ); + t.end(); + } +}); + tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { var cmd = [ 'printf "beep boop\nHello World!"', @@ -258,6 +298,29 @@ tape( 'the command-line interface supports specifying a custom delimiter when us } }); +tape( 'the command-line interface supports specifying the type of characters to return when used as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf \'beep boop\tHello World!\'', + '|', + EXEC_PATH, + fpath, + '--len=5', + '--mode code_point' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'be...\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (regexp)', opts, function test( t ) { var cmd = [ 'printf \'beep boop\tHello World!\'', @@ -281,6 +344,29 @@ tape( 'the command-line interface supports specifying a custom delimiter when us } }); +tape( 'when used as a standard stream, if provided an invalid option, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var cmd = [ + 'printf \'beep boop\tHello World!\'', + '|', + EXEC_PATH, + fpath, + '--len=5', + '--mode=foo' + ]; + + exec( cmd.join( ' ' ), 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().length > 0, true, 'expected value' ); + 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; diff --git a/lib/node_modules/@stdlib/string/truncate/test/test.js b/lib/node_modules/@stdlib/string/truncate/test/test.js index 897a7e844f2..b533557c1b1 100644 --- a/lib/node_modules/@stdlib/string/truncate/test/test.js +++ b/lib/node_modules/@stdlib/string/truncate/test/test.js @@ -59,6 +59,33 @@ tape( 'the function throws an error if not provided a string primitive', functio } }); +tape( 'the function throws an error if not provided a string primitive (options)', function test( t ) { + var values; + var i; + + values = [ + 5, + NaN, + null, + void 0, + true, + [], + {}, + 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() { + truncate( value, {} ); + }; + } +}); + tape( 'the function throws an error if not provided a nonnegative integer as its second argument', function test( t ) { var values; var i; @@ -88,6 +115,35 @@ tape( 'the function throws an error if not provided a nonnegative integer as its } }); +tape( 'the function throws an error if not provided a nonnegative integer as its third argument (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + -5, + 3.14, + NaN, + null, + void 0, + true, + [], + {}, + 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() { + truncate( 'beep boop', value, {} ); + }; + } +}); + tape( 'the function throws an error if provided a non-string as a third argument', function test( t ) { var values; var i; @@ -99,7 +155,6 @@ tape( 'the function throws an error if provided a non-string as a third argument void 0, true, [], - {}, function noop() {} ]; @@ -115,7 +170,117 @@ tape( 'the function throws an error if provided a non-string as a third argument } }); -tape( 'the function truncates a string to the specified length', function test( t ) { +tape( 'the function throws an error if provided a non-string as a third argument (options)', function test( t ) { + var values; + var i; + + values = [ + 5, + NaN, + null, + void 0, + true, + [], + {}, + 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() { + truncate( 'beep boop', 5, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var i; + + values = [ + 3, + 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() { + truncate( 'beep', 1, '!!', value ); + }; + } +}); + +tape( 'the function throws an error if provided a `mode` option which is not a supported mode (third argument)', function test( t ) { + var values; + var i; + + values = [ + 3, + 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() { + truncate( 'beep', 1, { + 'mode': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `mode` option which is not a supported mode (fourth argument)', function test( t ) { + var values; + var i; + + values = [ + 'abc', + 3, + 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() { + truncate( 'beep', 1, '!', { + 'mode': value + }); + }; + } +}); + +tape( 'the function truncates a string to the specified length (default)', function test( t ) { var expected; var actual; var str; @@ -154,7 +319,7 @@ tape( 'the function truncates a string to the specified length', function test( t.end(); }); -tape( 'the function truncates a string to the specified length (custom ending)', function test( t ) { +tape( 'the function truncates a string to the specified length (custom ending, deafult)', function test( t ) { var expected; var actual; var str; @@ -198,3 +363,237 @@ tape( 'the function truncates a string to the specified length (custom ending)', t.end(); }); + +tape( 'the function truncates a string to the specified length (mode=grapheme)', function test( t ) { + var expected; + var actual; + var opts; + var str; + var len; + + opts = { + 'mode': 'grapheme' + }; + + str = 'beep boop'; + len = 5; + expected = 'be...'; + actual = truncate( str, len, opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 10; + expected = 'beep boop'; + actual = truncate( str, len, opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 0; + expected = ''; + actual = truncate( str, len, opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 1; + expected = '.'; + actual = truncate( str, len, opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🐺 Wolf Brothers 🐺'; + len = 6; + expected = '🐺 W...'; + actual = truncate( str, len, opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function truncates a string to the specified length (custom ending, mode=grapheme)', function test( t ) { + var expected; + var actual; + var opts; + var str; + var len; + + opts = { + 'mode': 'grapheme' + }; + + str = 'beep boop'; + len = 5; + expected = 'beep|'; + actual = truncate( str, len, '|', opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 10; + expected = 'beep boop'; + actual = truncate( str, len, '!', opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 3; + expected = 'be!'; + actual = truncate( str, len, '!', opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 5; + expected = 'beep😃'; + actual = truncate( str, len, '😃', opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop foo bar'; + len = 10; + expected = 'beep 🙃 🙃 🙃'; + actual = truncate( str, len, ' 🙃 🙃 🙃', opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🐺 Wolf Brothers 🐺'; + len = 8; + expected = '🐺 Wolf 🐺'; + actual = truncate( str, len, '🐺', opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function truncates a string to the specified length (mode=code_point)', function test( t ) { + var expected; + var actual; + var opts; + var str; + var len; + + opts = { + 'mode': 'code_point' + }; + + str = 'beep boop'; + len = 5; + expected = 'be...'; + actual = truncate( str, len, opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 10; + expected = 'beep boop'; + actual = truncate( str, len, opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 0; + expected = ''; + actual = truncate( str, len, opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 1; + expected = '.'; + actual = truncate( str, len, opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function truncates a string to the specified length (custom ending, mode=code_point)', function test( t ) { + var expected; + var actual; + var opts; + var str; + var len; + + opts = { + 'mode': 'code_point' + }; + + str = 'beep boop'; + len = 5; + expected = 'beep|'; + actual = truncate( str, len, '|', opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 10; + expected = 'beep boop'; + actual = truncate( str, len, '!', opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 3; + expected = 'be!'; + actual = truncate( str, len, '!', opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function truncates a string to the specified length (mode=code_unit)', function test( t ) { + var expected; + var actual; + var opts; + var str; + var len; + + opts = { + 'mode': 'code_unit' + }; + + str = 'beep boop'; + len = 5; + expected = 'be...'; + actual = truncate( str, len, opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 10; + expected = 'beep boop'; + actual = truncate( str, len, opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 0; + expected = ''; + actual = truncate( str, len, opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 1; + expected = '.'; + actual = truncate( str, len, opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function truncates a string to the specified length (custom ending, mode=code_unit)', function test( t ) { + var expected; + var actual; + var opts; + var str; + var len; + + opts = { + 'mode': 'code_unit' + }; + + str = 'beep boop'; + len = 5; + expected = 'beep|'; + actual = truncate( str, len, '|', opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 10; + expected = 'beep boop'; + actual = truncate( str, len, '!', opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 3; + expected = 'be!'; + actual = truncate( str, len, '!', opts ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +});