From 5474a93b4f55ed7fe4e5f47198b58418936eb144 Mon Sep 17 00:00:00 2001 From: Stephannie Jimenez Date: Mon, 30 Oct 2023 12:38:48 -0500 Subject: [PATCH 1/6] feat: add base/truncate-middle string package --- .../string/base/truncate-middle/README.md | 94 +++++++++++++++++++ .../truncate-middle/benchmark/benchmark.js | 56 +++++++++++ .../string/base/truncate-middle/docs/repl.txt | 31 ++++++ .../truncate-middle/docs/types/index.d.ts | 59 ++++++++++++ .../base/truncate-middle/docs/types/test.ts | 64 +++++++++++++ .../base/truncate-middle/examples/index.js | 36 +++++++ .../string/base/truncate-middle/lib/index.js | 43 +++++++++ .../string/base/truncate-middle/lib/main.js | 87 +++++++++++++++++ .../string/base/truncate-middle/package.json | 68 ++++++++++++++ .../string/base/truncate-middle/test/test.js | 93 ++++++++++++++++++ 10 files changed, 631 insertions(+) create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle/README.md create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle/examples/index.js create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle/lib/index.js create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle/package.json create mode 100644 lib/node_modules/@stdlib/string/base/truncate-middle/test/test.js diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/README.md b/lib/node_modules/@stdlib/string/base/truncate-middle/README.md new file mode 100644 index 000000000000..465d75b63cad --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/README.md @@ -0,0 +1,94 @@ + + +# truncateMiddle + +> Truncate UTF-16 code units of string in the middle to a specified length. + +
+ +## Usage + +```javascript +var truncateMiddle = require( '@stdlib/string/base/truncate-middle' ); +``` + +#### truncateMiddle( str, len\[, seq] ) + +Truncates UTF-16 code units of string in the middle to a specified length. + +```javascript +var out = truncateMiddle( '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 = truncateMiddle( 'beep boop', 7, '!' ); +// returns 'bee!oop' + +out = truncateMiddle( 'beep boop', 7, '!!!' ); +// returns 'be!!!op' +``` + +
+ + + +
+ +## Examples + + + +```javascript +var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; +var out = truncateMiddle( str, 15 ); +// returns 'Lorem ... elit.' + +str = 'To be or not to be, that is the question'; +out = truncateMiddle( str, 19, '|' ); +// returns 'To be or | question' + +str = 'The quick fox jumps over the lazy dog.'; +out = truncateMiddle( str, 28, '...' ); +// returns 'The quick fox...he lazy dog.' +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/truncate-middle/benchmark/benchmark.js new file mode 100644 index 000000000000..dd7188b820d4 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/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/docs/repl.txt b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt new file mode 100644 index 000000000000..c7881a45aae6 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( str, len[, seq] ) + Truncates UTF-16 code units 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/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/index.d.ts new file mode 100644 index 000000000000..522df3c5f645 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/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 + +/** +* Truncates UTF-16 code units 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/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/test.ts new file mode 100644 index 000000000000..e202cfab6c1d --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/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/examples/index.js b/lib/node_modules/@stdlib/string/base/truncate-middle/examples/index.js new file mode 100644 index 000000000000..4510ab96c0df --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/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 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.' diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/lib/index.js b/lib/node_modules/@stdlib/string/base/truncate-middle/lib/index.js new file mode 100644 index 000000000000..3a176f11a426 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/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 string in the middle to a specified length. +* +* @module @stdlib/string/base/truncate-middle +* +* @example +* var truncateMiddle = require( '@stdlib/string/base/truncate-middle' ); +* +* 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/lib/main.js b/lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js new file mode 100644 index 000000000000..eb80ff6340e2 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js @@ -0,0 +1,87 @@ +/** +* @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 round = require( '@stdlib/math/base/special/round' ); +var floor = require( '@stdlib/math/base/special/floor' ); + + +// MAIN // + +/** +* Truncates UTF-16 code units 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 strLength; + var seqStart; + var seqEnd; + + seq = seq || '...'; + seqLength = seq.length; + strLength = str.length; + 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 ); + + return str.substring( 0, seqStart ) + seq + str.substring( seqEnd ); +} + + +// EXPORTS // + +module.exports = truncateMiddle; diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/package.json b/lib/node_modules/@stdlib/string/base/truncate-middle/package.json new file mode 100644 index 000000000000..22a1720eafd5 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/string/base/truncate-middle", + "version": "0.0.0", + "description": "Truncate UTF-16 code units 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/test/test.js b/lib/node_modules/@stdlib/string/base/truncate-middle/test/test.js new file mode 100644 index 000000000000..2efbfbcb63d3 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/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 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' ); + + 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' ); + + t.end(); +}); From 5cd53a6d54ca7ad080c0c53385163efeee655663 Mon Sep 17 00:00:00 2001 From: Stephannie Jimenez Date: Mon, 30 Oct 2023 12:44:44 -0500 Subject: [PATCH 2/6] fix: add missing import in readme examples --- lib/node_modules/@stdlib/string/base/truncate-middle/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/README.md b/lib/node_modules/@stdlib/string/base/truncate-middle/README.md index 465d75b63cad..afeb925d8611 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/README.md +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/README.md @@ -60,6 +60,8 @@ out = truncateMiddle( 'beep boop', 7, '!!!' ); ```javascript +var truncateMiddle = require( '@stdlib/string/base/truncate-middle' ); + var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; var out = truncateMiddle( str, 15 ); // returns 'Lorem ... elit.' From ad0ec7da1c9d12e2ba9ea2693548bbcc3d66c5a8 Mon Sep 17 00:00:00 2001 From: Stephannie Jimenez Date: Wed, 8 Nov 2023 11:27:52 -0500 Subject: [PATCH 3/6] docs: update package description and set seq argument mandatory --- .../string/base/truncate-middle/README.md | 16 +++++------ .../truncate-middle/benchmark/benchmark.js | 2 +- .../string/base/truncate-middle/docs/repl.txt | 10 +++---- .../truncate-middle/docs/types/index.d.ts | 14 +++++----- .../base/truncate-middle/docs/types/test.ts | 27 +++++++++---------- .../base/truncate-middle/examples/index.js | 2 +- .../string/base/truncate-middle/lib/index.js | 4 +-- .../string/base/truncate-middle/lib/main.js | 13 +++++---- .../string/base/truncate-middle/package.json | 2 +- .../string/base/truncate-middle/test/test.js | 8 +++--- 10 files changed, 46 insertions(+), 52 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/README.md b/lib/node_modules/@stdlib/string/base/truncate-middle/README.md index afeb925d8611..528c5adee929 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/README.md +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/README.md @@ -20,7 +20,7 @@ limitations under the License. # truncateMiddle -> Truncate UTF-16 code units of string in the middle to a specified length. +> Truncate the middle UTF-16 code units of a string to a specified length.
@@ -30,19 +30,15 @@ limitations under the License. var truncateMiddle = require( '@stdlib/string/base/truncate-middle' ); ``` -#### truncateMiddle( str, len\[, seq] ) +#### truncateMiddle( str, len, seq ) -Truncates UTF-16 code units of string in the middle to a specified length. +Truncates the middle UTF-16 code units of a string to a specified length. ```javascript -var out = truncateMiddle( 'beep boop', 7 ); +var out = truncateMiddle( '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 = truncateMiddle( 'beep boop', 7, '!' ); +out = truncateMiddle( 'beep boop', 7, '!' ); // returns 'bee!oop' out = truncateMiddle( 'beep boop', 7, '!!!' ); @@ -63,7 +59,7 @@ out = truncateMiddle( 'beep boop', 7, '!!!' ); var truncateMiddle = require( '@stdlib/string/base/truncate-middle' ); var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; -var out = truncateMiddle( str, 15 ); +var out = truncateMiddle( 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/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/truncate-middle/benchmark/benchmark.js index dd7188b820d4..6b9fc723d42d 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/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/docs/repl.txt b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt index c7881a45aae6..902947769d82 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt @@ -1,6 +1,6 @@ -{{alias}}( str, len[, seq] ) - Truncates UTF-16 code units of string in the middle to a specified length. +{{alias}}( str, len, seq ) + Truncates the middle UTF-16 code units of a string 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/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/index.d.ts index 522df3c5f645..c2ed25afe3db 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/index.d.ts @@ -19,16 +19,16 @@ // TypeScript Version: 4.1 /** -* Truncates UTF-16 code units of string in the middle to a specified length. +* Truncates the middle UTF-16 code units of a string to a specified length. * * @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/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/test.ts index e202cfab6c1d..f7c6020e7022 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/test.ts +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/test.ts @@ -23,28 +23,27 @@ import truncateMiddle = require( './index' ); // The function returns a string... { - truncateMiddle( 'abcdefghi', 3 ); // $ExpectType string - truncateMiddle( 'abcdefghi', 10, '|' ); // $ExpectType string + truncateMiddle( 'abcdefghi', 3, '...' ); // $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/examples/index.js b/lib/node_modules/@stdlib/string/base/truncate-middle/examples/index.js index 4510ab96c0df..50d16125b276 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/examples/index.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/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.' diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/lib/index.js b/lib/node_modules/@stdlib/string/base/truncate-middle/lib/index.js index 3a176f11a426..9a11af2a81e8 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/lib/index.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/lib/index.js @@ -19,14 +19,14 @@ 'use strict'; /** -* Truncate UTF-16 code units of string in the middle to a specified length. +* Truncate the middle UTF-16 code units of a string to a specified length. * * @module @stdlib/string/base/truncate-middle * * @example * var truncateMiddle = require( '@stdlib/string/base/truncate-middle' ); * -* 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/lib/main.js b/lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js index eb80ff6340e2..65959be85866 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js @@ -27,16 +27,16 @@ var floor = require( '@stdlib/math/base/special/floor' ); // MAIN // /** -* Truncates UTF-16 code units of string in the middle to a specified length. +* Truncates the middle UTF-16 code units of a string to a specified length. * * @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 @@ -46,17 +46,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 ) { @@ -65,7 +65,6 @@ function truncateMiddle( str, len, seq ) { var seqStart; var seqEnd; - seq = seq || '...'; seqLength = seq.length; strLength = str.length; if ( len > strLength ) { diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/package.json b/lib/node_modules/@stdlib/string/base/truncate-middle/package.json index 22a1720eafd5..1b4b887df911 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/package.json +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/string/base/truncate-middle", "version": "0.0.0", - "description": "Truncate UTF-16 code units of string in the middle to a specified length.", + "description": "Truncate the middle UTF-16 code units of a string to a specified length.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/test/test.js b/lib/node_modules/@stdlib/string/base/truncate-middle/test/test.js index 2efbfbcb63d3..ccabaf53e871 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/test/test.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/test/test.js @@ -41,25 +41,25 @@ 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' ); t.end(); From 910bd934b18077a953921e2d87340bace1496c14 Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 8 Nov 2023 14:01:01 -0800 Subject: [PATCH 4/6] Apply suggestions from code review --- .../@stdlib/string/base/truncate-middle/docs/repl.txt | 1 - .../@stdlib/string/base/truncate-middle/docs/types/index.d.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt index 902947769d82..82b45ae91305 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt @@ -23,7 +23,6 @@ > var str = 'beep boop'; > var out = {{alias}}( str, 5, '...' ) 'b...p' - > out = {{alias}}( str, 5, '|' ) 'be|op' diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/index.d.ts index c2ed25afe3db..945592aece1b 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/index.d.ts @@ -51,7 +51,7 @@ * 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; // EXPORTS // From 8f97891d8007f00c2ff4a55c03b3e4e3cfdb9bba Mon Sep 17 00:00:00 2001 From: Stephannie Jimenez Date: Fri, 10 Nov 2023 11:25:32 -0500 Subject: [PATCH 5/6] fix: add review changes --- .../@stdlib/string/base/truncate-middle/README.md | 4 ++-- .../string/base/truncate-middle/benchmark/benchmark.js | 2 +- .../@stdlib/string/base/truncate-middle/docs/repl.txt | 3 ++- .../string/base/truncate-middle/docs/types/index.d.ts | 2 +- .../@stdlib/string/base/truncate-middle/lib/index.js | 2 +- .../@stdlib/string/base/truncate-middle/lib/main.js | 10 ++++++---- .../@stdlib/string/base/truncate-middle/package.json | 2 +- .../@stdlib/string/base/truncate-middle/test/test.js | 4 ++-- 8 files changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/README.md b/lib/node_modules/@stdlib/string/base/truncate-middle/README.md index 528c5adee929..3f50acde8557 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/README.md +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/README.md @@ -20,7 +20,7 @@ limitations under the License. # truncateMiddle -> Truncate the middle UTF-16 code units of a string to a specified length. +> Truncate the middle UTF-16 code units of a string to return a string having a specified length.
@@ -32,7 +32,7 @@ var truncateMiddle = require( '@stdlib/string/base/truncate-middle' ); #### truncateMiddle( str, len, seq ) -Truncates the middle UTF-16 code units of a string to a specified length. +Truncates the middle UTF-16 code units of a string to return a string having a specified length. ```javascript var out = truncateMiddle( 'beep boop', 7, '...' ); diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/truncate-middle/benchmark/benchmark.js index 6b9fc723d42d..6da36d6bd77a 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/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 ], 7, '...' ); if ( typeof out !== 'string' ) { b.fail( 'should return a string' ); } diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt index 82b45ae91305..39ca0eb420c1 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/repl.txt @@ -1,6 +1,7 @@ {{alias}}( str, len, seq ) - Truncates the middle UTF-16 code units of a string to a specified length. + Truncates the middle UTF-16 code units of a string to return a string + having a specified length. Parameters ---------- diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/index.d.ts index 945592aece1b..712847d96f1c 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/docs/types/index.d.ts @@ -19,7 +19,7 @@ // TypeScript Version: 4.1 /** -* Truncates the middle UTF-16 code units of a string to a specified length. +* Truncates the middle UTF-16 code units of a string to return a string having a specified length. * * @param str - input string * @param len - output string length (including sequence) diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/lib/index.js b/lib/node_modules/@stdlib/string/base/truncate-middle/lib/index.js index 9a11af2a81e8..4690cc0cb53e 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/lib/index.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Truncate the middle UTF-16 code units of a string to a specified length. +* Truncate the middle UTF-16 code units of a string to return a string having a specified length. * * @module @stdlib/string/base/truncate-middle * diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js b/lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js index 65959be85866..e423e0393c26 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js @@ -27,7 +27,7 @@ var floor = require( '@stdlib/math/base/special/floor' ); // MAIN // /** -* Truncates the middle UTF-16 code units of a string to a specified length. +* Truncates the middle UTF-16 code units of a string to return a string having a specified length. * * @param {string} str - input string * @param {integer} len - output string length (including sequence) @@ -60,6 +60,7 @@ var floor = require( '@stdlib/math/base/special/floor' ); * // returns '..' */ function truncateMiddle( str, len, seq ) { + var finalLength; var seqLength; var strLength; var seqStart; @@ -70,12 +71,13 @@ function truncateMiddle( str, len, seq ) { if ( len > strLength ) { return str; } - if ( len - seqLength < 0 ) { + finalLength = len - seqLength; + if ( finalLength < 0 ) { return seq.slice( 0, len ); } - seqStart = round( ( len - seqLength ) / 2 ); - seqEnd = strLength - floor( ( len - seqLength ) / 2 ); + seqStart = round( finalLength / 2 ); + seqEnd = strLength - floor( finalLength / 2 ); return str.substring( 0, seqStart ) + seq + str.substring( seqEnd ); } diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/package.json b/lib/node_modules/@stdlib/string/base/truncate-middle/package.json index 1b4b887df911..490b3e3a4756 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/package.json +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/string/base/truncate-middle", "version": "0.0.0", - "description": "Truncate the middle UTF-16 code units of a string to a specified length.", + "description": "Truncate the middle UTF-16 code units of a string to return a string having a specified length.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/test/test.js b/lib/node_modules/@stdlib/string/base/truncate-middle/test/test.js index ccabaf53e871..0ba15ad10b54 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/test/test.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/test/test.js @@ -78,8 +78,8 @@ tape( 'the function truncates a string to the specified length (custom replaceme t.strictEqual( actual, expected, 'returns expected value' ); str = 'beep boop'; - len = 10; - expected = 'beep boop'; + len = 7; + expected = 'bee!oop'; actual = truncateMiddle( str, len, '!' ); t.strictEqual( actual, expected, 'returns expected value' ); From b93b71466c830c960b70803585b56ed50c4b294b Mon Sep 17 00:00:00 2001 From: Athan Date: Fri, 10 Nov 2023 13:12:13 -0800 Subject: [PATCH 6/6] Apply suggestions from code review --- .../@stdlib/string/base/truncate-middle/lib/main.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js b/lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js index e423e0393c26..19525c71a34c 100644 --- a/lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/truncate-middle/lib/main.js @@ -20,8 +20,8 @@ // MODULES // +var isOdd = require( '@stdlib/math/base/assert/is-odd' ); var round = require( '@stdlib/math/base/special/round' ); -var floor = require( '@stdlib/math/base/special/floor' ); // MAIN // @@ -77,7 +77,8 @@ function truncateMiddle( str, len, seq ) { } seqStart = round( finalLength / 2 ); - seqEnd = strLength - floor( finalLength / 2 ); + seqEnd = ( isOdd( finalLength ) ) ? seqStart-1 : seqStart; + seqEnd = strLength - seqEnd; return str.substring( 0, seqStart ) + seq + str.substring( seqEnd ); }