From cc88384e2a9937bf36016c80102d9c5913956c45 Mon Sep 17 00:00:00 2001 From: adityacodes30 Date: Tue, 5 Mar 2024 02:53:19 +0530 Subject: [PATCH 1/4] feat: add string/base/last issue #1696 --- .../@stdlib/string/base/last/README.md | 100 ++++++++++++++++++ .../string/base/last/benchmark/benchmark.js | 60 +++++++++++ .../@stdlib/string/base/last/docs/repl.txt | 29 +++++ .../string/base/last/docs/types/index.d.ts | 49 +++++++++ .../string/base/last/docs/types/test.ts | 57 ++++++++++ .../string/base/last/examples/index.js | 33 ++++++ .../@stdlib/string/base/last/lib/index.js | 43 ++++++++ .../@stdlib/string/base/last/lib/main.js | 55 ++++++++++ .../@stdlib/string/base/last/package.json | 67 ++++++++++++ .../@stdlib/string/base/last/test/test.js | 94 ++++++++++++++++ 10 files changed, 587 insertions(+) create mode 100644 lib/node_modules/@stdlib/string/base/last/README.md create mode 100644 lib/node_modules/@stdlib/string/base/last/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/string/base/last/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/string/base/last/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/string/base/last/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/string/base/last/examples/index.js create mode 100644 lib/node_modules/@stdlib/string/base/last/lib/index.js create mode 100644 lib/node_modules/@stdlib/string/base/last/lib/main.js create mode 100644 lib/node_modules/@stdlib/string/base/last/package.json create mode 100644 lib/node_modules/@stdlib/string/base/last/test/test.js diff --git a/lib/node_modules/@stdlib/string/base/last/README.md b/lib/node_modules/@stdlib/string/base/last/README.md new file mode 100644 index 000000000000..3f761585f654 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/last/README.md @@ -0,0 +1,100 @@ + + +# last + +> Return the last `n` UTF-16 code units of a string. + +
+ +## Usage + +```javascript +var last = require( '@stdlib/string/base/last' ); +``` + +#### last( str, n ) + +Returns the last `n` UTF-16 code units of a string. + +```javascript +var s = last( 'hello world', 1 ); +// returns 'd' + +s = last( 'this is stdlib', 1 ); +// returns 'b' + +s = last( 'foo', 2 ); +// returns 'oo' + +s = last( 'foo bar', 10 ); +// returns 'foo bar' +``` + +
+ + + +
+ +## Examples + + + +```javascript +var last = require( '@stdlib/string/base/last' ); + +var str = last( 'elections', 1 ); +// returns 's' + +str = last( 'JavaScript', 1 ); +// returns 't' + +str = last( 'good night', 5 ); +// returns 'night' +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/string/base/last/benchmark/benchmark.js b/lib/node_modules/@stdlib/string/base/last/benchmark/benchmark.js new file mode 100644 index 000000000000..228884023eb4 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/last/benchmark/benchmark.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var last = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var out; + var i; + + values = [ + 'hello world', + 'abc', + '', + 'Javascript', + 'beep boop', + 'foo bar', + 'xyz abc', + '🐶🐮🐷🐰🐸' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = last( 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/last/docs/repl.txt b/lib/node_modules/@stdlib/string/base/last/docs/repl.txt new file mode 100644 index 000000000000..215c0c32b888 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/last/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( str, n ) + Returns the last `n` UTF-16 code units of a string. + + Parameters + ---------- + str: string + Input string. + + n: integer + Number of UTF-16 code units to return. + + Returns + ------- + out: string + Output string. + + Examples + -------- + > var out = {{alias}}( 'hello', 1 ) + 'o' + > out = {{alias}}( 'JavaScript', 6 ) + 'Script' + > out = {{alias}}( 'foo bar', 10 ) + 'foo bar' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/string/base/last/docs/types/index.d.ts b/lib/node_modules/@stdlib/string/base/last/docs/types/index.d.ts new file mode 100644 index 000000000000..0d4da1ca1a0f --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/last/docs/types/index.d.ts @@ -0,0 +1,49 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/** +* Returns the last `n` UTF-16 code units of a string. +* +* @param str - input string +* @param n - number of code units to return +* @returns output string +* +* @example +* var s = last( 'hello world', 1 ); +* // returns 'd' +* +* @example +* var s = last( 'foo', 2 ); +* // returns 'oo' +* +* @example +* var s = last( 'JavaScript', 6 ); +* // returns 'Script' +* +* @example +* var s = last( 'foo bar', 10 ); +* // returns 'foo bar' +*/ +declare function last( str: string, n: number ): string; + + +// EXPORTS // + +export = last; diff --git a/lib/node_modules/@stdlib/string/base/last/docs/types/test.ts b/lib/node_modules/@stdlib/string/base/last/docs/types/test.ts new file mode 100644 index 000000000000..310a416d4bc1 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/last/docs/types/test.ts @@ -0,0 +1,57 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import last = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + last( 'abc', 1 ); // $ExpectType string +} + +// The compiler throws an error if the function is provided a value other than a string... +{ + last( true, 1 ); // $ExpectError + last( false, 1 ); // $ExpectError + last( null, 1 ); // $ExpectError + last( undefined, 1 ); // $ExpectError + last( 5, 1 ); // $ExpectError + last( [], 1 ); // $ExpectError + last( {}, 1 ); // $ExpectError + last( ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument that is not a number... +{ + last( 'abc', true ); // $ExpectError + last( 'abc', false ); // $ExpectError + last( 'abc', null ); // $ExpectError + last( 'abc', 'abc' ); // $ExpectError + last( 'abc', [] ); // $ExpectError + last( 'abc', {} ); // $ExpectError + last( 'abc', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + last(); // $ExpectError + last( 'abc' ); // $ExpectError + last( 'abc', 1, 2 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/string/base/last/examples/index.js b/lib/node_modules/@stdlib/string/base/last/examples/index.js new file mode 100644 index 000000000000..614b8257435a --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/last/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var last = require( './../lib' ); + +console.log( last( 'hello world', 1 ) ); +// => 'd' + +console.log( last( 'JavaScript', 6 ) ); +// => 'Script' + +console.log( last( 'New', 6 ) ); +// => 'New' + +console.log( last( 'EVENING', 5 ) ); +// => 'ENING' diff --git a/lib/node_modules/@stdlib/string/base/last/lib/index.js b/lib/node_modules/@stdlib/string/base/last/lib/index.js new file mode 100644 index 000000000000..63f776f85d7a --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/last/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return the last `n` UTF-16 code units of a string. +* +* @module @stdlib/string/base/last +* +* @example +* var last = require( '@stdlib/string/base/last' ); +* +* var s = last( 'hello world', 1 ); +* // returns 'd' +* +* var out = last( 'JavaScript', 6 ); +* // returns 'Script' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/string/base/last/lib/main.js b/lib/node_modules/@stdlib/string/base/last/lib/main.js new file mode 100644 index 000000000000..d130799cc972 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/last/lib/main.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Returns the last `n` UTF-16 code units of a string. +* +* @param {string} str - input string +* @param {NonNegativeInteger} n - number of UTF-16 code units to return +* @returns {string} output string +* +* @example +* var s = last( 'hello world', 1 ); +* // returns 'd' +* +* @example +* var s = last( 'this is stdlib', 1 ); +* // returns 'b' +* +* @example +* var out = last( 'JavaScript', 6 ); +* // returns 'Script' +* +* @example +* var out = last( 'New', 6 ); +* // returns 'New' +*/ +function last( str, n ) { + var len; + len = str.length; + return str.substring( len - n, len ); +} + + +// EXPORTS // + +module.exports = last; diff --git a/lib/node_modules/@stdlib/string/base/last/package.json b/lib/node_modules/@stdlib/string/base/last/package.json new file mode 100644 index 000000000000..a2691facd594 --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/last/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/string/base/last", + "version": "0.0.0", + "description": "Return the last UTF-16 code unit of 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" + } + ], + "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", + "last", + "character", + "char", + "codeunit", + "unicode" + ] +} diff --git a/lib/node_modules/@stdlib/string/base/last/test/test.js b/lib/node_modules/@stdlib/string/base/last/test/test.js new file mode 100644 index 000000000000..81601be143dc --- /dev/null +++ b/lib/node_modules/@stdlib/string/base/last/test/test.js @@ -0,0 +1,94 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var last = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof last, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns an empty string if provided an empty string', function test( t ) { + t.strictEqual( last( '', 1 ), '', 'returns expected value' ); + t.strictEqual( last( '', 2 ), '', 'returns expected value' ); + t.strictEqual( last( '', 3 ), '', 'returns expected value' ); + t.strictEqual( last( '', 10 ), '', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns an empty string if provided zero as the second argument', function test( t ) { + t.strictEqual( last( 'hello world', 0 ), '', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns the last UTF-16 code unit of a provided string', function test( t ) { + var s; + + s = last( 'hello world', 1 ); + t.strictEqual( s, 'd', 'returns expected value' ); + + s = last( 'JavaScript', 1 ); + t.strictEqual( s, 't', 'returns expected value' ); + + s = last( 'EVENING', 1 ); + t.strictEqual( s, 'G', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports returning the last `n` UTF-16 code units of a provided string', function test( t ) { + var s; + + s = last( 'hello world', 1 ); + t.strictEqual( s, 'd', 'returns expected value' ); + + s = last( 'hello world', 5 ); + t.strictEqual( s, 'world', 'returns expected value' ); + + s = last( 'new', 3 ); + t.strictEqual( s, 'new', 'returns expected value' ); + + s = last( '<--->', 2 ); + t.strictEqual( s, '->', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns whole strings if n is greater than length of string', function test( t ) { + var s; + + s = last( 'hello world', 20 ); + t.strictEqual( s, 'hello world', 'returns expected value' ); + + s = last( 'new', 5 ); + t.strictEqual( s, 'new', 'returns expected value' ); + + s = last( 'green', 8 ); + t.strictEqual( s, 'green', 'returns expected value' ); + + t.end(); +}); From 20a9fd568ebd3d8295cf2a733c09cd26c3b63c50 Mon Sep 17 00:00:00 2001 From: adityacodes30 Date: Tue, 5 Mar 2024 03:03:43 +0530 Subject: [PATCH 2/4] feat: add string/base/last issue #1696 --- lib/node_modules/@stdlib/string/base/last/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/string/base/last/test/test.js b/lib/node_modules/@stdlib/string/base/last/test/test.js index 81601be143dc..a49222ea615c 100644 --- a/lib/node_modules/@stdlib/string/base/last/test/test.js +++ b/lib/node_modules/@stdlib/string/base/last/test/test.js @@ -78,7 +78,7 @@ tape( 'the function supports returning the last `n` UTF-16 code units of a provi t.end(); }); -tape( 'the function returns whole strings if n is greater than length of string', function test( t ) { +tape( 'the function returns whole string if `n` is greater than length of string', function test( t ) { var s; s = last( 'hello world', 20 ); From 33470465bece949ccfdb04a1aaba1c82f56837ab Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Thu, 7 Mar 2024 20:30:28 -0500 Subject: [PATCH 3/4] Update lib/node_modules/@stdlib/string/base/last/lib/main.js Signed-off-by: Philipp Burckhardt --- lib/node_modules/@stdlib/string/base/last/lib/main.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/string/base/last/lib/main.js b/lib/node_modules/@stdlib/string/base/last/lib/main.js index d130799cc972..0aa1cbfd3b26 100644 --- a/lib/node_modules/@stdlib/string/base/last/lib/main.js +++ b/lib/node_modules/@stdlib/string/base/last/lib/main.js @@ -44,8 +44,7 @@ * // returns 'New' */ function last( str, n ) { - var len; - len = str.length; + var len = str.length; return str.substring( len - n, len ); } From f45a6fcd85747f03a9936052030b307a2cdcbd28 Mon Sep 17 00:00:00 2001 From: Philipp Burckhardt Date: Thu, 7 Mar 2024 20:30:36 -0500 Subject: [PATCH 4/4] Update lib/node_modules/@stdlib/string/base/last/test/test.js Signed-off-by: Philipp Burckhardt --- lib/node_modules/@stdlib/string/base/last/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/string/base/last/test/test.js b/lib/node_modules/@stdlib/string/base/last/test/test.js index a49222ea615c..a238babe05d2 100644 --- a/lib/node_modules/@stdlib/string/base/last/test/test.js +++ b/lib/node_modules/@stdlib/string/base/last/test/test.js @@ -78,7 +78,7 @@ tape( 'the function supports returning the last `n` UTF-16 code units of a provi t.end(); }); -tape( 'the function returns whole string if `n` is greater than length of string', function test( t ) { +tape( 'the function returns the original string if `n` is greater than the length of the string', function test( t ) { var s; s = last( 'hello world', 20 );