From 7f0fcabdef65d113f6dba18d7b2a933b1f97dcd5 Mon Sep 17 00:00:00 2001 From: Stephannie Jimenez Date: Mon, 30 Oct 2023 14:35:01 -0500 Subject: [PATCH 1/4] feat: add truncate-middle-grapheme-cluster string package --- .../README.md | 104 +++++++++++++++ .../benchmark/benchmark.js | 56 ++++++++ .../docs/repl.txt | 31 +++++ .../docs/types/index.d.ts | 59 +++++++++ .../docs/types/test.ts | 64 +++++++++ .../examples/index.js | 51 ++++++++ .../lib/index.js | 43 ++++++ .../lib/main.js | 107 +++++++++++++++ .../package.json | 68 ++++++++++ .../test/test.js | 123 ++++++++++++++++++ 10 files changed, 706 insertions(+) create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/examples/index.js create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/index.js create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/main.js create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/package.json create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/test/test.js diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md new file mode 100644 index 000000000000..cf1b95f9fabd --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md @@ -0,0 +1,104 @@ + + +# truncateMiddleGraphemeCluster + +> Truncate grapheme clusters of string in the middle to a specified length. + +
+ +## Usage + + + +```javascript +var truncateMiddleGraphemeCluster = require( '@stdlib/string/base/truncate-middle-grapheme-cluster' ); +``` + +#### truncateMiddleGraphemeCluster( str, len\[, seq] ) + +Truncates grapheme clusters of string in the middle to a specified length. + + + +```javascript +var out = truncateMiddleGraphemeCluster( 'beep boop', 7 ); +// returns 'be...op' +``` + +By default, the truncated string uses the replacement sequence `'...'`. To customize the replacement sequence, provide a `seq` argument: + + + +```javascript +var out = truncateMiddleGraphemeCluster( 'beep boop', 7, '!' ); +// returns 'bee!oop' + +out = truncateMiddleGraphemeCluster( 'beep boop', 7, '!!!' ); +// returns 'be!!!op' +``` + +
+ + + +
+ +## Examples + + + + + +```javascript +var truncateMiddleGraphemeCluster = require( '@stdlib/string/truncate-middle-grapheme-cluster' ); + +var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; +var out = truncateMiddleGraphemeCluster( str, 15 ); +// returns 'Lorem ... elit.' + +str = 'To be or not to be, that is the question'; +out = truncateMiddleGraphemeCluster( str, 19, '|' ); +// returns 'To be or | question' + +str = 'The quick fox jumps over the lazy dog.'; +out = truncateMiddleGraphemeCluster( str, 28, '...' ); +// returns 'The quick fox...he lazy dog.' +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/benchmark/benchmark.js new file mode 100644 index 000000000000..dd7188b820d4 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-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 truncateMiddle = 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 = truncateMiddle( 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-middle-grapheme-cluster/docs/repl.txt b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/repl.txt new file mode 100644 index 000000000000..66abc0f9c712 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( str, len[, seq] ) + Truncate grapheme clusters of string in the middle to a specified length. + + Parameters + ---------- + str: string + Input string. + + len: integer + Output string length. + + seq: string (optional) + Custom replacement sequence. Default: '...'. + + Returns + ------- + out: string + Truncated string. + + Examples + -------- + > var str = 'beep boop'; + > var out = {{alias}}( str, 5 ) + 'b...p' + + > out = {{alias}}( str, 5, '|' ) + 'be|op' + + See Also + -------- diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/index.d.ts new file mode 100644 index 000000000000..d5bce7bfa7a9 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/index.d.ts @@ -0,0 +1,59 @@ +/* +* @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: 4.1 + +/** +* Truncate grapheme clusters of string in the middle to a specified length. +* +* @param str - input string +* @param len - output string length (including sequence) +* @param seq - custom replacement sequence (default: `...`) +* @returns truncated string +* +* @example +* var str = 'beep boop'; +* var out = truncateMiddle( str, 5 ); +* // returns 'b...p' +* +* @example +* var str = 'beep boop'; +* var out = truncateMiddle( str, 5, '>>>' ); +* // returns 'b>>>p' +* +* @example +* var str = 'beep boop'; +* var out = truncateMiddle( str, 10 ); +* // returns 'beep boop' +* +* @example +* var str = 'beep boop'; +* var out = truncateMiddle( str, 0 ); +* // returns '' +* +* @example +* var str = 'beep boop'; +* var out = truncateMiddle( str, 2 ); +* // returns '..' +*/ +declare function truncateMiddle( str: string, len: number, seq?: string ): string; // tslint-disable-line max-line-length + + +// EXPORTS // + +export = truncateMiddle; diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/test.ts new file mode 100644 index 000000000000..e202cfab6c1d --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/test.ts @@ -0,0 +1,64 @@ +/* +* @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 truncateMiddle = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + truncateMiddle( 'abcdefghi', 3 ); // $ExpectType string + truncateMiddle( 'abcdefghi', 10, '|' ); // $ExpectType string +} + +// The compiler throws an error if the function is not provided a string as its first argument... +{ + truncateMiddle( true, 6 ); // $ExpectError + truncateMiddle( false, 6 ); // $ExpectError + truncateMiddle( 3, 6 ); // $ExpectError + truncateMiddle( [], 6 ); // $ExpectError + truncateMiddle( {}, 6 ); // $ExpectError + truncateMiddle( ( x: number ): number => x, 6 ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a number as its second argument... +{ + truncateMiddle( 'abd', true ); // $ExpectError + truncateMiddle( 'abd', false ); // $ExpectError + truncateMiddle( 'abd', 'abc' ); // $ExpectError + truncateMiddle( 'abd', [], 0 ); // $ExpectError + truncateMiddle( 'abd', {}, 0 ); // $ExpectError + truncateMiddle( 'abd', ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a string as its third argument... +{ + truncateMiddle( 'beep boop', 4, true ); // $ExpectError + truncateMiddle( 'beep boop', 4, false ); // $ExpectError + truncateMiddle( 'beep boop', 4, 123 ); // $ExpectError + truncateMiddle( 'beep boop', 4, [], 0 ); // $ExpectError + truncateMiddle( 'beep boop', 4, {}, 0 ); // $ExpectError + truncateMiddle( 'beep boop', 4, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + truncateMiddle(); // $ExpectError + truncateMiddle( 'abc', 4, '|', true ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/examples/index.js b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/examples/index.js new file mode 100644 index 000000000000..577875f2e0bf --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/examples/index.js @@ -0,0 +1,51 @@ +/** +* @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 truncateMiddle = require( './../lib' ); + +var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; +var out = truncateMiddle( str, 15 ); +console.log( out ); +// => 'Lorem ... elit.' + +str = 'To be or not to be, that is the question'; +out = truncateMiddle( str, 19, '|' ); +console.log( out ); +// => 'To be or | question' + +str = 'The quick fox jumps over the lazy dog.'; +out = truncateMiddle( str, 28, '...' ); +console.log( out ); +// => 'The quick fox...he lazy dog.' + +str = 'अनुच्छेद'; +out = truncateMiddle( str, 6, '...' ); +console.log( out ); +// => 'अनुच्...छेद' + +str = '🐺 Wolf Brothers 🐺'; +out = truncateMiddle( str, 7 ); +console.log( out ); +// => '🐺 ... 🐺' + +str = '🐺 Wolf Pack 🐺'; +out = truncateMiddle( str, 7, '🐺🐺🐺' ); +console.log( out ); +// => '🐺 🐺🐺🐺 🐺' diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/index.js b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/index.js new file mode 100644 index 000000000000..b2f420b218f8 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-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 string in the middle to a specified length. +* +* @module @stdlib/string/base/truncate-middle-grapheme-cluster +* +* @example +* var truncateMiddle = require( '@stdlib/string/base/truncate-middle-grapheme-cluster' ); +* +* var out = truncateMiddle( 'beep boop', 7 ); +* // returns 'be...op' +* +* out = truncateMiddle( 'beep boop', 7, '|' ); +* // returns 'bee|oop' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/main.js b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/main.js new file mode 100644 index 000000000000..826a5b86b940 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/main.js @@ -0,0 +1,107 @@ +/** +* @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' ); +var round = require( '@stdlib/math/base/special/round' ); +var floor = require( '@stdlib/math/base/special/floor' ); + + +// MAIN // + +/** +* Truncates code points of string in the middle to a specified length. +* +* @param {string} str - input string +* @param {integer} len - output string length (including sequence) +* @param {string} [seq='...'] - custom replacement sequence +* @returns {string} truncated string +* +* @example +* var str = 'beep boop'; +* var out = truncateMiddle( str, 5 ); +* // returns 'b...p' +* +* @example +* var str = 'beep boop'; +* var out = truncateMiddle( str, 5, '>>>' ); +* // returns 'b>>>p' +* +* @example +* var str = 'beep boop'; +* var out = truncateMiddle( str, 10 ); +* // returns 'beep boop' +* +* @example +* var str = 'beep boop'; +* var out = truncateMiddle( str, 0 ); +* // returns '' +* +* @example +* var str = 'beep boop'; +* var out = truncateMiddle( str, 2 ); +* // returns '..' +*/ +function truncateMiddle( str, len, seq ) { + var seqLength; + var fromIndex; + var strLength; + var seqStart; + var nVisual; + var seqEnd; + var idx2; + var idx1; + + seq = seq || '...'; + seqLength = numGraphemeClusters( seq ); + strLength = numGraphemeClusters( str ); + fromIndex = 0; + if ( len > strLength ) { + return str; + } + if ( len - seqLength < 0 ) { + return seq.slice( 0, len ); + } + seqStart = round( ( len - seqLength ) / 2 ); + seqEnd = strLength - floor( ( len - seqLength ) / 2 ); + nVisual = 0; + while ( nVisual < seqStart ) { + idx1 = nextGraphemeClusterBreak( str, fromIndex ); + fromIndex = idx1; + nVisual += 1; + } + idx2 = idx1; + while ( idx2 > 0 ) { + idx2 = nextGraphemeClusterBreak( str, fromIndex ); + if ( idx2 >= seqEnd + fromIndex - nVisual ) { + break; + } + fromIndex = idx2; + nVisual += 1; + } + return str.substring( 0, idx1 ) + seq + str.substring( idx2 ); +} + + +// EXPORTS // + +module.exports = truncateMiddle; diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/package.json b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/package.json new file mode 100644 index 000000000000..627172181a78 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/string/base/truncate-middle-code-point", + "version": "0.0.0", + "description": "Truncate grapheme clusters of string in the middle 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", + "string", + "str", + "base", + "truncate", + "middle", + "character", + "char", + "codeunit", + "unicode" + ] +} diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/test/test.js b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/test/test.js new file mode 100644 index 000000000000..776d214a8e27 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/test/test.js @@ -0,0 +1,123 @@ +/** +* @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 truncateMiddle = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof truncateMiddle, '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 = 'b...p'; + actual = truncateMiddle( str, len ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 10; + expected = 'beep boop'; + actual = truncateMiddle( str, len ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 0; + expected = ''; + actual = truncateMiddle( str, len ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 1; + expected = '.'; + actual = truncateMiddle( str, len ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🐺 Wolf Brothers 🐺'; + len = 7; + expected = '🐺 ... 🐺'; + actual = truncateMiddle( str, len ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function truncates a string to the specified length (custom replacement sequence)', function test( t ) { + var expected; + var actual; + var str; + var len; + + str = 'beep boop'; + len = 5; + expected = 'be|op'; + actual = truncateMiddle( str, len, '|' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 10; + expected = 'beep boop'; + actual = truncateMiddle( str, len, '!' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 3; + expected = 'b!p'; + actual = truncateMiddle( str, len, '!' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop'; + len = 5; + expected = 'be😃op'; + actual = truncateMiddle( str, len, '😃' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'beep boop foo bar'; + len = 10; + expected = 'be 🙃 🙃 🙃ar'; + actual = truncateMiddle( str, len, ' 🙃 🙃 🙃' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = '🐺 Wolf Brothers 🐺'; + len = 8; + expected = '🐺 Wo>s 🐺'; + actual = truncateMiddle( str, len, '>' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + str = 'Hello, world, says 🤖, the robot!'; + len = 10; + expected = 'Hello🤖bot!'; + actual = truncateMiddle( str, len, '🤖' ); + t.strictEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); From b4218a4246df43efd91390e30d24903af57b7766 Mon Sep 17 00:00:00 2001 From: Stephannie Jimenez Date: Mon, 30 Oct 2023 14:38:51 -0500 Subject: [PATCH 2/4] fix: remove double import --- .../string/base/truncate-middle-grapheme-cluster/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md index cf1b95f9fabd..ff570f90854d 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md @@ -68,8 +68,6 @@ out = truncateMiddleGraphemeCluster( 'beep boop', 7, '!!!' ); ```javascript -var truncateMiddleGraphemeCluster = require( '@stdlib/string/truncate-middle-grapheme-cluster' ); - var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; var out = truncateMiddleGraphemeCluster( str, 15 ); // returns 'Lorem ... elit.' From d5afe0931ed7cb92fe2bf0f83ad4e2973120ff77 Mon Sep 17 00:00:00 2001 From: Stephannie Jimenez Date: Mon, 30 Oct 2023 14:43:29 -0500 Subject: [PATCH 3/4] fix: fix result displayed in examples --- .../string/base/truncate-middle-grapheme-cluster/README.md | 2 ++ .../base/truncate-middle-grapheme-cluster/examples/index.js | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md index ff570f90854d..9b8f4e97d50a 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md @@ -68,6 +68,8 @@ out = truncateMiddleGraphemeCluster( 'beep boop', 7, '!!!' ); ```javascript +var truncateMiddleGraphemeCluster = require( '@stdlib/string/base/truncate-middle-grapheme-cluster' ); + var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; var out = truncateMiddleGraphemeCluster( str, 15 ); // returns 'Lorem ... elit.' diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/examples/index.js b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/examples/index.js index 577875f2e0bf..32ce00671e29 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/examples/index.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/examples/index.js @@ -36,9 +36,9 @@ console.log( out ); // => 'The quick fox...he lazy dog.' str = 'अनुच्छेद'; -out = truncateMiddle( str, 6, '...' ); +out = truncateMiddle( str, 5, '...' ); console.log( out ); -// => 'अनुच्...छेद' +// => 'अ...छेद' str = '🐺 Wolf Brothers 🐺'; out = truncateMiddle( str, 7 ); From 555f4313d534e00bd4712eb6bfc9458c719f7f36 Mon Sep 17 00:00:00 2001 From: Stephannie Jimenez Date: Wed, 8 Nov 2023 11:35:33 -0500 Subject: [PATCH 4/4] docs: set seq argument mandatory --- .../README.md | 14 +++------- .../benchmark/benchmark.js | 2 +- .../docs/repl.txt | 8 +++--- .../docs/types/index.d.ts | 12 ++++----- .../docs/types/test.ts | 26 +++++++++---------- .../examples/index.js | 4 +-- .../lib/index.js | 2 +- .../lib/main.js | 11 ++++---- .../test/test.js | 10 +++---- 9 files changed, 41 insertions(+), 48 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md index 9b8f4e97d50a..57dbbdbc42d6 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/README.md @@ -32,23 +32,17 @@ limitations under the License. var truncateMiddleGraphemeCluster = require( '@stdlib/string/base/truncate-middle-grapheme-cluster' ); ``` -#### truncateMiddleGraphemeCluster( str, len\[, seq] ) +#### truncateMiddleGraphemeCluster( str, len, seq ) Truncates grapheme clusters of string in the middle to a specified length. ```javascript -var out = truncateMiddleGraphemeCluster( 'beep boop', 7 ); +var out = truncateMiddleGraphemeCluster( 'beep boop', 7, '...' ); // returns 'be...op' -``` - -By default, the truncated string uses the replacement sequence `'...'`. To customize the replacement sequence, provide a `seq` argument: - - -```javascript -var out = truncateMiddleGraphemeCluster( 'beep boop', 7, '!' ); +out = truncateMiddleGraphemeCluster( 'beep boop', 7, '!' ); // returns 'bee!oop' out = truncateMiddleGraphemeCluster( 'beep boop', 7, '!!!' ); @@ -71,7 +65,7 @@ out = truncateMiddleGraphemeCluster( 'beep boop', 7, '!!!' ); var truncateMiddleGraphemeCluster = require( '@stdlib/string/base/truncate-middle-grapheme-cluster' ); var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; -var out = truncateMiddleGraphemeCluster( str, 15 ); +var out = truncateMiddleGraphemeCluster( str, 15, '...' ); // returns 'Lorem ... elit.' str = 'To be or not to be, that is the question'; diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/benchmark/benchmark.js index dd7188b820d4..6b9fc723d42d 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/benchmark/benchmark.js @@ -42,7 +42,7 @@ bench( pkg, function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = truncateMiddle( values[ i%values.length ], 1 ); + out = truncateMiddle( values[ i%values.length ], 1, '...' ); if ( typeof out !== 'string' ) { b.fail( 'should return a string' ); } diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/repl.txt b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/repl.txt index 66abc0f9c712..c5e4366ab96c 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/repl.txt +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/repl.txt @@ -1,5 +1,5 @@ -{{alias}}( str, len[, seq] ) +{{alias}}( str, len, seq ) Truncate grapheme clusters of string in the middle to a specified length. Parameters @@ -10,8 +10,8 @@ len: integer Output string length. - seq: string (optional) - Custom replacement sequence. Default: '...'. + seq: string + Custom replacement sequence. Returns ------- @@ -21,7 +21,7 @@ Examples -------- > var str = 'beep boop'; - > var out = {{alias}}( str, 5 ) + > var out = {{alias}}( str, 5, '...' ) 'b...p' > out = {{alias}}( str, 5, '|' ) diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/index.d.ts index d5bce7bfa7a9..9458ee8700ed 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/index.d.ts @@ -23,12 +23,12 @@ * * @param str - input string * @param len - output string length (including sequence) -* @param seq - custom replacement sequence (default: `...`) +* @param seq - custom replacement sequence * @returns truncated string * * @example * var str = 'beep boop'; -* var out = truncateMiddle( str, 5 ); +* var out = truncateMiddle( str, 5, '...' ); * // returns 'b...p' * * @example @@ -38,20 +38,20 @@ * * @example * var str = 'beep boop'; -* var out = truncateMiddle( str, 10 ); +* var out = truncateMiddle( str, 10, '...' ); * // returns 'beep boop' * * @example * var str = 'beep boop'; -* var out = truncateMiddle( str, 0 ); +* var out = truncateMiddle( str, 0, '...' ); * // returns '' * * @example * var str = 'beep boop'; -* var out = truncateMiddle( str, 2 ); +* var out = truncateMiddle( str, 2, '...' ); * // returns '..' */ -declare function truncateMiddle( str: string, len: number, seq?: string ): string; // tslint-disable-line max-line-length +declare function truncateMiddle( str: string, len: number, seq: string ): string; // tslint-disable-line max-line-length // EXPORTS // diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/test.ts index e202cfab6c1d..b43796e8bde0 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/test.ts +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/docs/types/test.ts @@ -23,28 +23,28 @@ import truncateMiddle = require( './index' ); // The function returns a string... { - truncateMiddle( 'abcdefghi', 3 ); // $ExpectType string + truncateMiddle( 'abcdefghi', 3, '...' ); // $ExpectType string truncateMiddle( 'abcdefghi', 10, '|' ); // $ExpectType string } // The compiler throws an error if the function is not provided a string as its first argument... { - truncateMiddle( true, 6 ); // $ExpectError - truncateMiddle( false, 6 ); // $ExpectError - truncateMiddle( 3, 6 ); // $ExpectError - truncateMiddle( [], 6 ); // $ExpectError - truncateMiddle( {}, 6 ); // $ExpectError - truncateMiddle( ( x: number ): number => x, 6 ); // $ExpectError + truncateMiddle( true, 6, '...' ); // $ExpectError + truncateMiddle( false, 6, '...' ); // $ExpectError + truncateMiddle( 3, 6, '...' ); // $ExpectError + truncateMiddle( [], 6, '...' ); // $ExpectError + truncateMiddle( {}, 6, '...' ); // $ExpectError + truncateMiddle( ( x: number ): number => x, 6, '...' ); // $ExpectError } // The compiler throws an error if the function is not provided a number as its second argument... { - truncateMiddle( 'abd', true ); // $ExpectError - truncateMiddle( 'abd', false ); // $ExpectError - truncateMiddle( 'abd', 'abc' ); // $ExpectError - truncateMiddle( 'abd', [], 0 ); // $ExpectError - truncateMiddle( 'abd', {}, 0 ); // $ExpectError - truncateMiddle( 'abd', ( x: number ): number => x, 0 ); // $ExpectError + truncateMiddle( 'abd', true, '...' ); // $ExpectError + truncateMiddle( 'abd', false, '...' ); // $ExpectError + truncateMiddle( 'abd', 'abc', '...' ); // $ExpectError + truncateMiddle( 'abd', [], '...' ); // $ExpectError + truncateMiddle( 'abd', {}, '...' ); // $ExpectError + truncateMiddle( 'abd', ( x: number ): number => x, '...' ); // $ExpectError } // The compiler throws an error if the function is not provided a string as its third argument... diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/examples/index.js b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/examples/index.js index 32ce00671e29..126147810c4f 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/examples/index.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/examples/index.js @@ -21,7 +21,7 @@ var truncateMiddle = require( './../lib' ); var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; -var out = truncateMiddle( str, 15 ); +var out = truncateMiddle( str, 15, '...' ); console.log( out ); // => 'Lorem ... elit.' @@ -41,7 +41,7 @@ console.log( out ); // => 'अ...छेद' str = '🐺 Wolf Brothers 🐺'; -out = truncateMiddle( str, 7 ); +out = truncateMiddle( str, 7, '...' ); console.log( out ); // => '🐺 ... 🐺' diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/index.js b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/index.js index b2f420b218f8..948988fa7244 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/index.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/index.js @@ -26,7 +26,7 @@ * @example * var truncateMiddle = require( '@stdlib/string/base/truncate-middle-grapheme-cluster' ); * -* var out = truncateMiddle( 'beep boop', 7 ); +* var out = truncateMiddle( 'beep boop', 7, '...' ); * // returns 'be...op' * * out = truncateMiddle( 'beep boop', 7, '|' ); diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/main.js b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/main.js index 826a5b86b940..666df3603e99 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/lib/main.js @@ -33,12 +33,12 @@ var floor = require( '@stdlib/math/base/special/floor' ); * * @param {string} str - input string * @param {integer} len - output string length (including sequence) -* @param {string} [seq='...'] - custom replacement sequence +* @param {string} seq - custom replacement sequence * @returns {string} truncated string * * @example * var str = 'beep boop'; -* var out = truncateMiddle( str, 5 ); +* var out = truncateMiddle( str, 5, '...' ); * // returns 'b...p' * * @example @@ -48,17 +48,17 @@ var floor = require( '@stdlib/math/base/special/floor' ); * * @example * var str = 'beep boop'; -* var out = truncateMiddle( str, 10 ); +* var out = truncateMiddle( str, 10, '...' ); * // returns 'beep boop' * * @example * var str = 'beep boop'; -* var out = truncateMiddle( str, 0 ); +* var out = truncateMiddle( str, 0, '...' ); * // returns '' * * @example * var str = 'beep boop'; -* var out = truncateMiddle( str, 2 ); +* var out = truncateMiddle( str, 2, '...' ); * // returns '..' */ function truncateMiddle( str, len, seq ) { @@ -71,7 +71,6 @@ function truncateMiddle( str, len, seq ) { var idx2; var idx1; - seq = seq || '...'; seqLength = numGraphemeClusters( seq ); strLength = numGraphemeClusters( str ); fromIndex = 0; diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/test/test.js b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/test/test.js index 776d214a8e27..d1b92faa4279 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/test/test.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle-grapheme-cluster/test/test.js @@ -41,31 +41,31 @@ tape( 'the function truncates a string to the specified length', function test( str = 'beep boop'; len = 5; expected = 'b...p'; - actual = truncateMiddle( str, len ); + actual = truncateMiddle( str, len, '...' ); t.strictEqual( actual, expected, 'returns expected value' ); str = 'beep boop'; len = 10; expected = 'beep boop'; - actual = truncateMiddle( str, len ); + actual = truncateMiddle( str, len, '...' ); t.strictEqual( actual, expected, 'returns expected value' ); str = 'beep boop'; len = 0; expected = ''; - actual = truncateMiddle( str, len ); + actual = truncateMiddle( str, len, '...' ); t.strictEqual( actual, expected, 'returns expected value' ); str = 'beep boop'; len = 1; expected = '.'; - actual = truncateMiddle( str, len ); + actual = truncateMiddle( str, len, '...' ); t.strictEqual( actual, expected, 'returns expected value' ); str = '🐺 Wolf Brothers 🐺'; len = 7; expected = '🐺 ... 🐺'; - actual = truncateMiddle( str, len ); + actual = truncateMiddle( str, len, '...' ); t.strictEqual( actual, expected, 'returns expected value' ); t.end();