diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 6917be2..80fabbb 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -12,6 +12,7 @@ Anudeep Sanapala Athan Reines Brendan Graetz Bruno Fenzl +Bryan Elee Chinmay Joshi <86140365+JawHawk@users.noreply.github.com> Christopher Dambamuromo Dan Rose diff --git a/base/last-grapheme-cluster/README.md b/base/last-grapheme-cluster/README.md new file mode 100644 index 0000000..0314025 --- /dev/null +++ b/base/last-grapheme-cluster/README.md @@ -0,0 +1,95 @@ + + +# lastGraphemeCluster + +> Return the last `n` grapheme clusters (i.e., user-perceived characters) of a string. + +
+ +## Usage + +```javascript +var lastGraphemeCluster = require( '@stdlib/string/base/last-grapheme-cluster' ); +``` + +#### lastGraphemeCluster( str, n ) + +Returns the last `n` grapheme clusters (i.e., user-perceived characters) of a string. + +```javascript +var out = lastGraphemeCluster( 'Hello World', 1 ); +// returns 'd' + +out = lastGraphemeCluster( 'Evening', 3 ); +// returns 'ing' + +out = lastGraphemeCluster( 'foo bar', 10 ); +// returns 'foo bar' +``` + +
+ + + +
+ +## Examples + + + +```javascript +var lastGraphemeCluster = require( '@stdlib/string/base/last-grapheme-cluster' ); + +var str = lastGraphemeCluster( 'Hello World!', 1 ); +// returns '!' + +str = lastGraphemeCluster( 'JavaScript', 6 ); +// returns 'Script' + +str = lastGraphemeCluster( 'stdlib', 10 ); +// returns 'stdlib' + +str = lastGraphemeCluster( '🐶🐮🐷🐰🐸', 2 ); +// returns '🐰🐸' + +str = lastGraphemeCluster( '六书/六書', 2 ); +// returns '六書' +``` + +
+ + + + + + + + + + + + + + diff --git a/base/last-grapheme-cluster/benchmark/benchmark.js b/base/last-grapheme-cluster/benchmark/benchmark.js new file mode 100644 index 0000000..677642f --- /dev/null +++ b/base/last-grapheme-cluster/benchmark/benchmark.js @@ -0,0 +1,58 @@ +/** +* @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 = [ + '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/base/last-grapheme-cluster/docs/repl.txt b/base/last-grapheme-cluster/docs/repl.txt new file mode 100644 index 0000000..50e902a --- /dev/null +++ b/base/last-grapheme-cluster/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( str, n ) + Returns the last `n` grapheme clusters (i.e., user-perceived characters) of + a string. + + Parameters + ---------- + str: string + Input string. + + n: integer + Number of grapheme clusters to return. + + Returns + ------- + out: string + Output string. + + Examples + -------- + > var out = {{alias}}( 'beep', 1 ) + 'p' + > out = {{alias}}( 'Boop', 2 ) + 'op' + > out = {{alias}}( 'JavaScript', 6 ) + 'Script' + + See Also + -------- + diff --git a/base/last-grapheme-cluster/docs/types/index.d.ts b/base/last-grapheme-cluster/docs/types/index.d.ts new file mode 100644 index 0000000..2009262 --- /dev/null +++ b/base/last-grapheme-cluster/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @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` grapheme clusters (i.e., user-perceived characters) of a string. +* +* @param str - input string +* @param n - number of grapheme clusters to return +* @returns output string +* +* @example +* var out = last( 'Hello World', 1 ); +* // returns 'd' +* +* @example +* var out = last( 'Evening', 3 ); +* // returns 'ing' +* +* @example +* var out = last( 'JavaScript', 6 ); +* // returns 'Script' +* +* @example +* var out = last( '🐶🐮🐷🐰🐸', 2 ); +* // returns '🐰🐸' +* +* @example +* var out = last( 'foo bar', 5 ); +* // returns 'o bar' +*/ +declare function last( str: string, n: number ): string; + + +// EXPORTS // + +export = last; diff --git a/base/last-grapheme-cluster/docs/types/test.ts b/base/last-grapheme-cluster/docs/types/test.ts new file mode 100644 index 0000000..310a416 --- /dev/null +++ b/base/last-grapheme-cluster/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/base/last-grapheme-cluster/examples/index.js b/base/last-grapheme-cluster/examples/index.js new file mode 100644 index 0000000..db26239 --- /dev/null +++ b/base/last-grapheme-cluster/examples/index.js @@ -0,0 +1,36 @@ +/** +* @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 lastGraphemeCluster = require( './../lib' ); + +console.log( lastGraphemeCluster( 'Hello World!', 1 ) ); +// => '!' + +console.log( lastGraphemeCluster( 'JavaScript', 6 ) ); +// => 'Script' + +console.log( lastGraphemeCluster( 'stdlib', 10 ) ); +// => 'stdlib' + +console.log( lastGraphemeCluster( '🐶🐮🐷🐰🐸', 2 ) ); +// => '🐰🐸' + +console.log( lastGraphemeCluster( '六书/六書', 2 ) ); +// => '六書' diff --git a/base/last-grapheme-cluster/lib/index.js b/base/last-grapheme-cluster/lib/index.js new file mode 100644 index 0000000..9db7136 --- /dev/null +++ b/base/last-grapheme-cluster/lib/index.js @@ -0,0 +1,46 @@ +/** +* @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` grapheme clusters (i.e., user-perceived characters) of a string. +* +* @module @stdlib/string/base/last-grapheme-cluster +* +* @example +* var last = require( '@stdlib/string/base/last-grapheme-cluster' ); +* +* var out = last( 'Hello', 1 ); +* // returns 'o'; +* +* out = last( 'JavaScript', 1 ); +* // returns 'Script'; +* +* out = last( '🐮🐷🐸🐵', 2 ); +* // returns '🐸🐵'; +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/base/last-grapheme-cluster/lib/main.js b/base/last-grapheme-cluster/lib/main.js new file mode 100644 index 0000000..60dd881 --- /dev/null +++ b/base/last-grapheme-cluster/lib/main.js @@ -0,0 +1,105 @@ +/** +* @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 nextGraphemeClusterBreak = require( './../../../next-grapheme-cluster-break' ); +var CircularBuffer = require( '@stdlib/utils/circular-buffer' ); +var zeros = require( '@stdlib/array/base/zeros' ); + + +// MAIN // + +/** +* Returns the last `n` grapheme clusters (i.e., user-perceived characters) of a string. +* +* @param {string} str - input string +* @param {NonNegativeInteger} n - number of grapheme clusters to return +* @returns {string} output string +* +* @example +* var out = last( 'Hello World', 1 ); +* // returns 'd' +* +* @example +* var out = last( 'Evening', 3 ); +* // returns 'ing' +* +* @example +* var out = last( 'JavaScript', 6 ); +* // returns 'Script' +* +* @example +* var out = last( '六书/六書', 1 ); +* // returns '書' +* +* @example +* var out = last( '🐶🐮🐷🐰🐸', 2 ); +* // returns '🐰🐸' +*/ +function last( str, n ) { + var count; + var cbuf; + var buf; + var i; + + if ( n === 0 || str === '' ) { + return ''; + } + // Resolve the first cluster break: + i = nextGraphemeClusterBreak( str, 0 ); + + // If we received a sentinel value, return the input string, as there are no more cluster breaks to iterate over... + if ( i === -1 ) { + return str; + } + // Initialize a buffer for keeping track of cluster break indices: + buf = zeros( n ); + + // Wrap the buffer to create a circular buffer serving as a FIFO stack where we can keep at most `n` indices as we iterate from left-to-right: + cbuf = new CircularBuffer( buf ); + + // Add the first character index: + cbuf.push( 0 ); + + // Add the index of the first grapheme cluster break to our buffer: + cbuf.push( i ); + + // Slide a window over the string from left-to-right... + count = 0; + while ( true ) { + count += 1; + i = nextGraphemeClusterBreak( str, i ); + if ( i === -1 ) { + break; + } + cbuf.push( i ); + } + // Resolve the leftmost index: + i = buf[ (count+1)%n ]; // count+1 as count%n corresponds to the index of the "newest" element in the circular buffer and count+1 is the next element to replace (i.e., the "oldest" index) + + // Return the last `n` grapheme clusters: + return str.substring( i ); +} + + +// EXPORTS // + +module.exports = last; diff --git a/base/last-grapheme-cluster/package.json b/base/last-grapheme-cluster/package.json new file mode 100644 index 0000000..590dad3 --- /dev/null +++ b/base/last-grapheme-cluster/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/string/base/last-grapheme-cluster", + "version": "0.0.0", + "description": "Return the last grapheme cluster (i.e., user-perceived character) 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", + "grapheme", + "cluster", + "unicode" + ] +} diff --git a/base/last-grapheme-cluster/test/test.js b/base/last-grapheme-cluster/test/test.js new file mode 100644 index 0000000..328eb89 --- /dev/null +++ b/base/last-grapheme-cluster/test/test.js @@ -0,0 +1,129 @@ +/** +* @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.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 grapheme cluster of a provided string (ascii)', function test( t ) { + var out; + + out = last( 'hello world', 1 ); + t.strictEqual( out, 'd', 'returns expected value' ); + + out = last( 'h', 1 ); + t.strictEqual( out, 'h', 'returns expected value' ); + + out = last( '!!!', 1 ); + t.strictEqual( out, '!', 'returns expected value' ); + + out = last( 'JavaScript', 1 ); + t.strictEqual( out, 't', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the last grapheme cluster of a provided string (Unicode)', function test( t ) { + var out; + + out = last( 'अनुच्छेद', 1 ); + t.strictEqual( out, 'द', 'returns expected value' ); + + out = last( '六书/六書', 1 ); + t.strictEqual( out, '書', 'returns expected value' ); + + out = last( '書', 1 ); + t.strictEqual( out, '書', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the last grapheme cluster of a provided string (emoji)', function test( t ) { + var out; + + out = last( '🌷', 1 ); + t.strictEqual( out, '🌷', 'returns expected value' ); + + out = last( '🏝️🌷', 1 ); + t.strictEqual( out, '🌷', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports returning the last `n` grapheme clusters of a provided string', function test( t ) { + var out; + + out = last( 'hello world', 1 ); + t.strictEqual( out, 'd', 'returns expected value' ); + + out = last( 'hello world', 7 ); + t.strictEqual( out, 'o world', 'returns expected value' ); + + out = last( 'JavaScript', 15 ); + t.strictEqual( out, 'JavaScript', 'returns expected value' ); + + out = last( '!!!', 1 ); + t.strictEqual( out, '!', 'returns expected value' ); + + out = last( '!!!', 2 ); + t.strictEqual( out, '!!', 'returns expected value' ); + + out = last( 'अनुच्छेद', 1 ); + t.strictEqual( out, 'द', 'returns expected value' ); + + out = last( 'अनुच्छेद+o', 3 ); + t.strictEqual( out, 'द+o', 'returns expected value' ); + + out = last( '六书/六書', 2 ); + t.strictEqual( out, '六書', 'returns expected value' ); + + out = last( '六书/六書()!', 4 ); + t.strictEqual( out, '書()!', 'returns expected value' ); + + out = last( '六书/六書', 10 ); + t.strictEqual( out, '六书/六書', 'returns expected value' ); + + out = last( '🌷🌷🌷🌷🌷', 2 ); + t.strictEqual( out, '🌷🌷', 'returns expected value' ); + + t.end(); +});