From ccb5ff639237080e1c6b6d5796f1b02cb54fb80e Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 9 Apr 2024 15:08:26 +0000 Subject: [PATCH] Auto-generated commit --- last/README.md | 222 ++++++++++++++ last/benchmark/benchmark.js | 145 +++++++++ last/bin/cli | 130 +++++++++ last/docs/repl.txt | 48 +++ last/docs/types/index.d.ts | 118 ++++++++ last/docs/types/test.ts | 111 +++++++ last/docs/usage.txt | 10 + last/etc/cli_opts.json | 19 ++ last/examples/index.js | 42 +++ last/lib/index.js | 46 +++ last/lib/main.js | 131 +++++++++ last/package.json | 70 +++++ last/test/fixtures/stdin_error.js.txt | 33 +++ last/test/test.cli.js | 389 ++++++++++++++++++++++++ last/test/test.js | 406 ++++++++++++++++++++++++++ 15 files changed, 1920 insertions(+) create mode 100644 last/README.md create mode 100644 last/benchmark/benchmark.js create mode 100755 last/bin/cli create mode 100644 last/docs/repl.txt create mode 100644 last/docs/types/index.d.ts create mode 100644 last/docs/types/test.ts create mode 100644 last/docs/usage.txt create mode 100644 last/etc/cli_opts.json create mode 100644 last/examples/index.js create mode 100644 last/lib/index.js create mode 100644 last/lib/main.js create mode 100644 last/package.json create mode 100644 last/test/fixtures/stdin_error.js.txt create mode 100644 last/test/test.cli.js create mode 100644 last/test/test.js diff --git a/last/README.md b/last/README.md new file mode 100644 index 00000000..0199aeb3 --- /dev/null +++ b/last/README.md @@ -0,0 +1,222 @@ + + +# last + +> Return the last character(s) of a string. + +
+ +## Usage + +```javascript +var last = require( '@stdlib/string/last' ); +``` + +#### last( str\[, n]\[, options] ) + +Returns the last character(s) of a string. + +```javascript +var out = last( 'last man standing' ); +// returns 'g' + +out = last( 'Hidden Treasures' ); +// returns 's' +``` + +The function supports the following options: + +- **mode**: type of characters to return. Must be one of the following: + + - `'grapheme'`: grapheme clusters. Appropriate for strings containing visual characters which can span multiple Unicode code points (e.g., emoji). + - `'code_point'`: Unicode code points. Appropriate for strings containing visual characters which are comprised of more than one Unicode code unit (e.g., ideographic symbols and punctuation and mathematical alphanumerics). + - `'code_unit'`: UTF-16 code units. Appropriate for strings containing visual characters drawn from the basic multilingual plane (BMP) (e.g., common characters, such as those from the Latin, Greek, and Cyrillic alphabets). + + Default: `'grapheme'`. + +By default, the function returns the last character. To return the last `n` characters, provide a second argument specifying the number of characters to return. + +```javascript +var out = last( 'foo bar', 3 ); +// returns 'bar' + +out = last( 'foo bar', 10 ); +// returns 'foo bar' +``` + +
+ + + + + +
+ +## Notes + +- By default, the function assumes the general case in which an input string may contain an arbitrary number of grapheme clusters. This assumption comes with a performance cost. Accordingly, if an input string is known to only contain visual characters of a particular type (e.g., only alphanumeric), one can achieve better performance by specifying the appropriate `mode` option. + +
+ + + +
+ +## Examples + + + +```javascript +var last = require( '@stdlib/string/last' ); + +var str = last( 'last man standing' ); +// returns 'g' + +str = last( 'presidential election' ); +// returns 'n' + +str = last( 'javaScript' ); +// returns 't' + +str = last( 'Hidden Treasures' ); +// returns 's' + +str = last( 'The Last of the Mohicans', 8 ); +// returns 'Mohicans' + +str = last( '🐶🐮🐷🐰🐸', 2 ); +// returns '🐰🐸' + +str = last( '🐶🐮🐷🐰🐸', 10 ); +// returns '🐶🐮🐷🐰🐸' +``` + +
+ + + +* * * + +
+ +## CLI + +
+ +### Usage + +```text +Usage: last [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --n num Number of characters to return. Default: 1. + --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. + --mode mode Type of character to return. Default: 'grapheme'. +``` + +
+ + + + + +
+ +### Notes + +- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes. + + ```bash + # Not escaped... + $ echo -n $'beep\nboop' | last --split /\r?\n/ + + # Escaped... + $ echo -n $'beep\nboop' | last --split /\\r?\\n/ + ``` + +- The implementation ignores trailing delimiters. + +
+ + + +
+ +### Examples + +```bash +$ last beep +p +``` + +To use as a [standard stream][standard-streams], + +```bash +$ echo -n 'beep\nboop' | last --n=2 +ep +op +``` + +By default, when used as a [standard stream][standard-streams], the implementation assumes newline-delimited data. To specify an alternative delimiter, set the `split` option. + +```bash +$ echo -n 'beep\tboop' | last --split '\t' +p +p +``` + +
+ + + +
+ + + + + + + + + + + + + + diff --git a/last/benchmark/benchmark.js b/last/benchmark/benchmark.js new file mode 100644 index 00000000..d8c28d0a --- /dev/null +++ b/last/benchmark/benchmark.js @@ -0,0 +1,145 @@ +/** +* @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 ] ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':mode=grapheme', function benchmark( b ) { + var values; + var opts; + var out; + var i; + + values = [ + 'beep boop', + 'foo bar', + 'xyz abc' + ]; + opts = { + 'mode': 'grapheme' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = last( values[ i%values.length ], opts ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':mode=code_point', function benchmark( b ) { + var values; + var opts; + var out; + var i; + + values = [ + 'beep boop', + 'foo bar', + 'xyz abc' + ]; + opts = { + 'mode': 'code_point' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = last( values[ i%values.length ], opts ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':mode=code_unit', function benchmark( b ) { + var values; + var opts; + var out; + var i; + + values = [ + 'beep boop', + 'foo bar', + 'xyz abc' + ]; + opts = { + 'mode': 'code_unit' + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = last( values[ i%values.length ], opts ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/last/bin/cli b/last/bin/cli new file mode 100755 index 00000000..659742b8 --- /dev/null +++ b/last/bin/cli @@ -0,0 +1,130 @@ +#!/usr/bin/env node + +/** +* @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 resolve = require( 'path' ).resolve; +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var CLI = require( '@stdlib/cli/ctor' ); +var stdin = require( '@stdlib/process/read-stdin' ); +var stdinStream = require( '@stdlib/streams/node/stdin' ); +var RE_EOL = require( '@stdlib/regexp/eol' ).REGEXP; +var isRegExpString = require( '@stdlib/assert/is-regexp-string' ); +var reFromString = require( '@stdlib/utils/regexp-from-string' ); +var last = require( './../lib' ); + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +* @returns {void} +*/ +function main() { + var split; + var flags; + var args; + var opts; + var cli; + var n; + + // Create a command-line interface: + cli = new CLI({ + 'pkg': require( './../package.json' ), + 'options': require( './../etc/cli_opts.json' ), + 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }) + }); + + // Get any provided command-line options: + flags = cli.flags(); + if ( flags.help || flags.version ) { + return; + } + if ( flags.n ) { + n = parseInt( flags.n, 10 ); + } else { + n = 1; + } + opts = {}; + if ( flags.mode ) { + opts.mode = flags.mode; + } + + // Get any provided command-line arguments: + args = cli.args(); + + // Check if we are receiving data from `stdin`... + if ( !stdinStream.isTTY ) { + if ( flags.split ) { + if ( !isRegExpString( flags.split ) ) { + flags.split = '/'+flags.split+'/'; + } + split = reFromString( flags.split ); + } else { + split = RE_EOL; + } + return stdin( onRead ); + } + try { + console.log( last( args[ 0 ], n, opts ) ); // eslint-disable-line no-console + } catch ( error ) { + return cli.error( error ); + } + + /** + * Callback invoked upon reading from `stdin`. + * + * @private + * @param {(Error|null)} error - error object + * @param {Buffer} data - data + * @returns {void} + */ + function onRead( error, data ) { + var lines; + var i; + if ( error ) { + return cli.error( error ); + } + lines = data.toString().split( split ); + + // Remove any trailing separators (e.g., trailing newline)... + if ( lines[ lines.length-1 ] === '' ) { + lines.pop(); + } + if ( lines.length ) { + try { + console.log( last( lines[ 0 ], n, opts ) ); // eslint-disable-line no-console + } catch ( error ) { + return cli.error( error ); + } + for ( i = 1; i < lines.length; i++ ) { + console.log( last( lines[ i ], n, opts ) ); // eslint-disable-line no-console + } + } + } +} + +main(); diff --git a/last/docs/repl.txt b/last/docs/repl.txt new file mode 100644 index 00000000..20b38de7 --- /dev/null +++ b/last/docs/repl.txt @@ -0,0 +1,48 @@ + +{{alias}}( str[, n][, options] ) + Returns the last character(s) of a string. + + Parameters + ---------- + str: string + Input string. + + n: integer (optional) + Number of characters to return. Default: 1. + + options: Object (optional) + Options. + + options.mode: string (optional) + Type of characters to return. The following modes are supported: + + - grapheme: grapheme clusters. Appropriate for strings containing visual + characters which can span multiple Unicode code points (e.g., emoji). + - code_point: Unicode code points. Appropriate for strings containing + visual characters which are comprised of more than one Unicode code + unit (e.g., ideographic symbols and punctuation and mathematical + alphanumerics). + - code_unit': UTF-16 code units. Appropriate for strings containing + visual characters drawn from the basic multilingual plane (BMP) (e.g., + common characters, such as those from the Latin, Greek, and Cyrillic + alphabets). + + Default: 'grapheme'. + + Returns + ------- + out: string + Output string. + + Examples + -------- + > var out = {{alias}}( 'beep' ) + 'p' + > out = {{alias}}( 'Boop', 2 ) + 'op' + > out = {{alias}}( 'foo bar', 3 ) + 'bar' + + See Also + -------- + diff --git a/last/docs/types/index.d.ts b/last/docs/types/index.d.ts new file mode 100644 index 00000000..4582fa02 --- /dev/null +++ b/last/docs/types/index.d.ts @@ -0,0 +1,118 @@ +/* +* @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 + +/* eslint-disable @typescript-eslint/unified-signatures */ + +/** +* Interface describing function options. +*/ +interface Options { + /** + * Specifies the type of characters to return (default: 'grapheme'). + * + * ## Notes + * + * - The following option values are supported: + * + * - `'grapheme'`: grapheme clusters. Appropriate for strings containing visual characters which can span multiple Unicode code points (e.g., emoji). + * - `'code_point'`: Unicode code points. Appropriate for strings containing visual characters which are comprised of more than one Unicode code unit (e.g., ideographic symbols and punctuation and mathematical alphanumerics). + * - `'code_unit'`: UTF-16 code units. Appropriate for strings containing visual characters drawn from the basic multilingual plane (BMP) (e.g., common characters, such as those from the Latin, Greek, and Cyrillic alphabets). + */ + mode?: 'grapheme' | 'code_point' | 'code_unit'; +} + +/** +* Returns the last `n` characters of a string. +* +* @param str - input string +* @param n - number of characters to return +* @param options - options +* @returns updated string +* +* @example +* var out = last( 'last man standing', 2 ); +* // returns 'ng' +* +* @example +* var out = last( '🐶🐮🐷🐰🐸', 2, { +* 'mode': 'grapheme' +* }); +* // returns '🐰🐸' +*/ +declare function last( str: string, n: number, options?: Options ): string; + +/** +* Returns the last character of a string. +* +* @param str - input string +* @param options - options +* @returns updated string +* +* @example +* var out = last( 'last man standing', { +* 'mode': 'code_unit' +* }); +* // returns 'g' +* +* @example +* var out = last( '🐶🐮🐷🐰🐸', { +* 'mode': 'grapheme' +* }); +* // returns '🐰🐸' +*/ +declare function last( str: string, options?: Options ): string; + +/** +* Returns the last character(s) of a string. +* +* @param str - input string +* @param n - number of characters to return (default: 1) +* @returns updated string +* +* @example +* var out = last( 'last man standing' ); +* // returns 'g' +* +* @example +* var out = last( 'presidential election' ); +* // returns 'n' +* +* @example +* var out = last( 'javaScript' ); +* // returns 't' +* +* @example +* var out = last( 'Hidden Treasures' ); +* // returns 's' +* +* @example +* var out = last( '🐶🐮🐷🐰🐸', 2 ); +* // returns '🐰🐸' +* +* @example +* var out = last( 'foo bar', 3 ); +* // returns 'bar' +*/ +declare function last( str: string, n?: number ): string; + + +// EXPORTS // + +export = last; diff --git a/last/docs/types/test.ts b/last/docs/types/test.ts new file mode 100644 index 00000000..d5f37d5c --- /dev/null +++ b/last/docs/types/test.ts @@ -0,0 +1,111 @@ +/* +* @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' ); // $ExpectType string + last( 'abc', 1 ); // $ExpectType string + last( 'abc', {} ); // $ExpectType string + last( 'abc', 1, {} ); // $ExpectType string +} + +// The compiler throws an error if the function is provided a first argument which is not a string... +{ + last( true ); // $ExpectError + last( false ); // $ExpectError + last( null ); // $ExpectError + last( undefined ); // $ExpectError + last( 5 ); // $ExpectError + last( [] ); // $ExpectError + last( {} ); // $ExpectError + last( ( x: number ): number => x ); // $ExpectError + + 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 + + last( true, {} ); // $ExpectError + last( false, {} ); // $ExpectError + last( null, {} ); // $ExpectError + last( undefined, {} ); // $ExpectError + last( 5, {} ); // $ExpectError + last( [], {} ); // $ExpectError + last( {}, {} ); // $ExpectError + last( ( x: number ): number => x, {} ); // $ExpectError + + 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 an invalid second argument... +{ + last( 'abc', true ); // $ExpectError + last( 'abc', false ); // $ExpectError + last( 'abc', null ); // $ExpectError + last( 'abc', '' ); // $ExpectError + last( 'abc', [] ); // $ExpectError + last( 'abc', ( x: number ): number => x ); // $ExpectError + + last( 'abc', true, {} ); // $ExpectError + last( 'abc', false, {} ); // $ExpectError + last( 'abc', null, {} ); // $ExpectError + last( '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 invalid `mode` option... +{ + last( 'abc', { 'mode': true } ); // $ExpectError + last( 'abc', { 'mode': false } ); // $ExpectError + last( 'abc', { 'mode': null } ); // $ExpectError + last( 'abc', { 'mode': '' } ); // $ExpectError + last( 'abc', { 'mode': [] } ); // $ExpectError + last( 'abc', { 'mode': ( x: number ): number => x } ); // $ExpectError + + last( 'abc', 1, { 'mode': true } ); // $ExpectError + last( 'abc', 1, { 'mode': false } ); // $ExpectError + last( 'abc', 1, { 'mode': null } ); // $ExpectError + last( 'abc', 1, { 'mode': '' } ); // $ExpectError + last( 'abc', 1, { 'mode': [] } ); // $ExpectError + last( 'abc', 1, { 'mode': {} } ); // $ExpectError + last( 'abc', 1, { 'mode': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + last(); // $ExpectError + last( 'abc', 1, {}, {} ); // $ExpectError +} diff --git a/last/docs/usage.txt b/last/docs/usage.txt new file mode 100644 index 00000000..8e24879d --- /dev/null +++ b/last/docs/usage.txt @@ -0,0 +1,10 @@ + +Usage: last [options] [] + +Options: + + -h, --help Print this message. + -V, --version Print the package version. + --n num Number of characters to return. Default: 1. + --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. + --mode mode Type of character to return. Default: 'grapheme'. diff --git a/last/etc/cli_opts.json b/last/etc/cli_opts.json new file mode 100644 index 00000000..2ceae458 --- /dev/null +++ b/last/etc/cli_opts.json @@ -0,0 +1,19 @@ +{ + "boolean": [ + "help", + "version" + ], + "string": [ + "n", + "split", + "mode" + ], + "alias": { + "help": [ + "h" + ], + "version": [ + "V" + ] + } +} diff --git a/last/examples/index.js b/last/examples/index.js new file mode 100644 index 00000000..7e5e6b5b --- /dev/null +++ b/last/examples/index.js @@ -0,0 +1,42 @@ +/** +* @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( 'last man standing' ) ); +// => 'g' + +console.log( last( 'presidential election' ) ); +// => 'n' + +console.log( last( 'javaScript' ) ); +// => 't' + +console.log( last( 'Hidden Treasures' ) ); +// => 's' + +console.log( last( 'The Last of the Mohicans', 8 ) ); +// => 'Mohicans' + +console.log( last( '🐶🐮🐷🐰🐸', 2 ) ); +// => '🐰🐸' + +console.log( last( '🐶🐮🐷🐰🐸', 10 ) ); +// => '🐶🐮🐷🐰🐸' diff --git a/last/lib/index.js b/last/lib/index.js new file mode 100644 index 00000000..601e157f --- /dev/null +++ b/last/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 character(s) of a string. +* +* @module @stdlib/string/last +* +* @example +* var last = require( '@stdlib/string/last' ); +* +* var out = last( 'last man standing' ); +* // returns 'g' +* +* out = last( 'Hidden Treasures' ); +* // returns 's'; +* +* out = last( '🐮🐷🐸🐵', 2 ); +* // returns '🐸🐵' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/last/lib/main.js b/last/lib/main.js new file mode 100644 index 00000000..74eb1b81 --- /dev/null +++ b/last/lib/main.js @@ -0,0 +1,131 @@ +/** +* @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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var contains = require( '@stdlib/array/base/assert/contains' ).factory; +var lastCodeUnit = require( './../../base/last' ); +var lastCodePoint = require( './../../base/last-code-point' ); +var lastGraphemeCluster = require( './../../base/last-grapheme-cluster' ); +var format = require( './../../format' ); + + +// VARIABLES // + +var MODES = [ 'grapheme', 'code_point', 'code_unit' ]; +var FCNS = { + 'grapheme': lastGraphemeCluster, + 'code_point': lastCodePoint, + 'code_unit': lastCodeUnit +}; +var isMode = contains( MODES ); + + +// MAIN // + +/** +* Returns the last character(s) of a string. +* +* @param {string} str - input string +* @param {NonNegativeInteger} [n=1] - number of characters to return +* @param {Options} [options] - options +* @param {string} [options.mode="grapheme"] - type of "character" to return (must be either `grapheme`, `code_point`, or `code_unit`) +* @throws {TypeError} last argument must be a string +* @throws {TypeError} second argument must be a nonnegative integer +* @throws {TypeError} options argument must be an object +* @throws {TypeError} must provide valid options +* @returns {string} output string +* +* @example +* var out = last( 'last man standing' ); +* // returns 'g' +* +* @example +* var out = last( 'presidential election' ); +* // returns 'n' +* +* @example +* var out = last( 'javaScript' ); +* // returns 't' +* +* @example +* var out = last( 'Hidden Treasures' ); +* // returns 's' +* +* @example +* var out = last( '🐶🐮🐷🐰🐸', 2 ); +* // returns '🐰🐸' +* +* @example +* var out = last( 'foo bar', 3 ); +* // returns 'bar' +*/ +function last( str ) { + var options; + var nargs; + var opts; + var n; + + if ( !isString( str ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); + } + opts = { + 'mode': 'grapheme' + }; + nargs = arguments.length; + if ( nargs === 1 ) { + n = 1; + } else if ( nargs === 2 ) { + n = arguments[ 1 ]; + if ( isPlainObject( n ) ) { + options = n; + n = 1; + } else if ( !isNonNegativeInteger( n ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', n ) ); + } + } else { // nargs > 2 + n = arguments[ 1 ]; + if ( !isNonNegativeInteger( n ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', n ) ); + } + options = arguments[ 2 ]; + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + } + if ( options ) { + if ( hasOwnProp( options, 'mode' ) ) { + opts.mode = options.mode; + if ( !isMode( opts.mode ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Value: `%s`.', 'mode', MODES.join( '", "' ), opts.mode ) ); + } + } + } + return FCNS[ opts.mode ]( str, n ); +} + + +// EXPORTS // + +module.exports = last; diff --git a/last/package.json b/last/package.json new file mode 100644 index 00000000..4467cb8b --- /dev/null +++ b/last/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/string/last", + "version": "0.0.0", + "description": "Return the last character(s) 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" + } + ], + "bin": { + "last": "./bin/cli" + }, + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdstring", + "utilities", + "utility", + "utils", + "util", + "string", + "str", + "last", + "character", + "char", + "grapheme", + "unicode", + "codepoint" + ] +} diff --git a/last/test/fixtures/stdin_error.js.txt b/last/test/fixtures/stdin_error.js.txt new file mode 100644 index 00000000..c7e3c506 --- /dev/null +++ b/last/test/fixtures/stdin_error.js.txt @@ -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. +*/ + +var proc = require( 'process' ); +var resolve = require( 'path' ).resolve; +var proxyquire = require( 'proxyquire' ); + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); + +proc.stdin.isTTY = false; + +proxyquire( fpath, { + '@stdlib/process/read-stdin': stdin +}); + +function stdin( clbk ) { + clbk( new Error( 'beep' ) ); +} diff --git a/last/test/test.cli.js b/last/test/test.cli.js new file mode 100644 index 00000000..dbce6097 --- /dev/null +++ b/last/test/test.cli.js @@ -0,0 +1,389 @@ +/** +* @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 resolve = require( 'path' ).resolve; +var exec = require( 'child_process' ).exec; +var tape = require( 'tape' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); +var replace = require( './../../replace' ); +var readFileSync = require( '@stdlib/fs/read-file' ).sync; +var EXEC_PATH = require( '@stdlib/process/exec-path' ); + + +// VARIABLES // + +var fpath = resolve( __dirname, '..', 'bin', 'cli' ); +var opts = { + 'skip': IS_BROWSER || IS_WINDOWS +}; + + +// FIXTURES // + +var PKG_VERSION = require( './../package.json' ).version; + + +// TESTS // + +tape( 'command-line interface', function test( t ) { + t.ok( true, __filename ); + t.end(); +}); + +tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '--help' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) { + var expected; + var cmd; + + expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { + 'encoding': 'utf8' + }); + cmd = [ + EXEC_PATH, + fpath, + '-h' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), expected+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '--version' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + fpath, + '-V' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' ); + } + t.end(); + } +}); + +tape( 'the command-line interface returns the last character of a string argument', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'beep\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'p\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface returns the last `n` characters of a string argument', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'beep\'; process.argv[ 3 ] = \'--n=2\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'ep\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports specifying the type of characters to return', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'beep\'; process.argv[ 3 ] = \'--mode=code_point\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'p\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'if provided an invalid option, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var cmd = [ + EXEC_PATH, + '-e', + '"process.stdin.isTTY = true; process.argv[ 2 ] = \'beep\'; process.argv[ 3 ] = \'--mode=foo\'; require( \''+fpath+'\' );"' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString().length > 0, true, 'expected value' ); + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf "beep\nboop"', + '|', + EXEC_PATH, + fpath + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'p\np\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports use as a standard stream (return `n` characters)', opts, function test( t ) { + var cmd = [ + 'printf "foo\nbar"', + '|', + EXEC_PATH, + fpath, + '--n=2' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'oo\nar\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (string)', opts, function test( t ) { + var cmd = [ + 'printf \'foo\tbar\tbaz\'', + '|', + EXEC_PATH, + fpath, + '--split \'\t\'' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'o\nr\nz\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports specifying the type of characters to return when used as a standard stream', opts, function test( t ) { + var cmd = [ + 'printf \'foo\nbar\nbaz\'', + '|', + EXEC_PATH, + fpath, + '--mode code_point' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'o\nr\nz\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'the command-line interface supports specifying a custom delimiter when used as a standard stream (regexp)', opts, function test( t ) { + var cmd = [ + 'printf \'foo\tbar\tbaz\'', + '|', + EXEC_PATH, + fpath, + '--split=/\\\\t/' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.fail( error.message ); + } else { + t.strictEqual( stdout.toString(), 'o\nr\nz\n', 'expected value' ); + t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' ); + } + t.end(); + } +}); + +tape( 'when used as a standard stream, if provided an invalid option, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var cmd = [ + 'printf \'foo\tbar\tbaz\'', + '|', + EXEC_PATH, + fpath, + '--mode=foo' + ]; + + exec( cmd.join( ' ' ), done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString().length > 0, true, 'expected value' ); + t.end(); + } +}); + +tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) { + var script; + var opts; + var cmd; + + script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), { + 'encoding': 'utf8' + }); + + // Replace single quotes with double quotes: + script = replace( script, '\'', '"' ); + + cmd = [ + EXEC_PATH, + '-e', + '\''+script+'\'' + ]; + + opts = { + 'cwd': __dirname + }; + + exec( cmd.join( ' ' ), opts, done ); + + function done( error, stdout, stderr ) { + if ( error ) { + t.pass( error.message ); + t.strictEqual( error.code, 1, 'expected exit code' ); + } + t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' ); + t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' ); + t.end(); + } +}); diff --git a/last/test/test.js b/last/test/test.js new file mode 100644 index 00000000..5dc3f06d --- /dev/null +++ b/last/test/test.js @@ -0,0 +1,406 @@ +/** +* @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 throws an error if not provided a string', function test( t ) { + var values; + var i; + + values = [ + 5, + null, + true, + void 0, + NaN, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + last( value ); + }; + } +}); + +tape( 'the function throws an error if not provided a string (options)', function test( t ) { + var values; + var i; + + values = [ + 5, + null, + true, + void 0, + NaN, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + last( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a nonnegative integer', function test( t ) { + var values; + var i; + + values = [ + 'abc', + 3.14, + null, + true, + void 0, + NaN, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + last( 'beep', value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a nonnegative integer (options)', function test( t ) { + var values; + var i; + + values = [ + 'abc', + 3.14, + null, + true, + void 0, + NaN, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + last( 'beep', value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var i; + + values = [ + 'abc', + 3, + null, + true, + void 0, + NaN, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + last( 'beep', 1, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `mode` option which is not a supported mode (second argument)', function test( t ) { + var values; + var i; + + values = [ + 'abc', + 3, + null, + true, + void 0, + NaN, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + last( 'beep', { + 'mode': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `mode` option which is not a supported mode (third argument)', function test( t ) { + var values; + var i; + + values = [ + 'abc', + 3, + null, + true, + void 0, + NaN, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + last( 'beep', 1, { + 'mode': value + }); + }; + } +}); + +tape( 'the function returns an empty string if provided an empty string', function test( t ) { + t.strictEqual( last( '' ), '', 'returns expected value' ); + t.strictEqual( last( '', 1 ), '', 'returns expected value' ); + t.strictEqual( last( '', {} ), '', 'returns expected value' ); + t.strictEqual( last( '', 1, {} ), '', 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns the last character of a provided string', function test( t ) { + var out; + + out = last( 'hello world' ); + t.strictEqual( out, 'd', 'returns expected value' ); + + out = last( '!!!' ); + t.strictEqual( out, '!', 'returns expected value' ); + + out = last( 'Hello' ); + t.strictEqual( out, 'o', 'returns expected value' ); + + out = last( 'अनुच्छेद' ); + t.strictEqual( out, 'द', 'returns expected value' ); + + out = last( '六书/六書' ); + t.strictEqual( out, '書', 'returns expected value' ); + + out = last( '🌷' ); + t.strictEqual( out, '🌷', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an empty string if provided zero as the second argument', function test( t ) { + var out; + + out = last( 'hello world', 0 ); + t.strictEqual( out, '', 'returns expected value' ); + + out = last( 'JavaScript', 0, {} ); + t.strictEqual( out, '', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports returning the last `n` characters of a provided string (default)', 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( '!!!', 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( '六书/六書', 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 supports returning the last `n` characters of a provided string (mode=grapheme)', function test( t ) { + var opts; + var out; + + opts = { + 'mode': 'grapheme' + }; + + out = last( 'hello world', 1, opts ); + t.strictEqual( out, 'd', 'returns expected value' ); + + out = last( 'hello world', 7, opts ); + t.strictEqual( out, 'o world', 'returns expected value' ); + + out = last( '!!!', 1, opts ); + t.strictEqual( out, '!', 'returns expected value' ); + + out = last( '!!!', 2, opts ); + t.strictEqual( out, '!!', 'returns expected value' ); + + out = last( 'अनुच्छेद', 1, opts ); + t.strictEqual( out, 'द', 'returns expected value' ); + + out = last( '六书/六書', 1, opts ); + t.strictEqual( out, '書', 'returns expected value' ); + + out = last( '🌷', 1, opts ); + t.strictEqual( out, '🌷', 'returns expected value' ); + + out = last( '👉🏿', 1, opts ); + t.strictEqual( out, '👉🏿', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports returning the last `n` characters of a provided string (mode=code_point)', function test( t ) { + var opts; + var out; + + opts = { + 'mode': 'code_point' + }; + + out = last( 'hello world', 1, opts ); + t.strictEqual( out, 'd', 'returns expected value' ); + + out = last( 'hello world', 7, opts ); + t.strictEqual( out, 'o world', 'returns expected value' ); + + out = last( '!!!', 1, opts ); + t.strictEqual( out, '!', 'returns expected value' ); + + out = last( '!!!', 2, opts ); + t.strictEqual( out, '!!', 'returns expected value' ); + + out = last( 'अनुच्छेद', 1, opts ); + t.strictEqual( out, 'द', 'returns expected value' ); + + out = last( '六书/六書', 1, opts ); + t.strictEqual( out, '書', 'returns expected value' ); + + out = last( '🌷', 1, opts ); + t.strictEqual( out, '🌷', 'returns expected value' ); + + out = last( '👉🏿', 1, opts ); + t.strictEqual( out, '🏿', 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports returning the last `n` characters of a provided string (mode=code_unit)', function test( t ) { + var opts; + var out; + + opts = { + 'mode': 'code_unit' + }; + + out = last( 'hello world', 1, opts ); + t.strictEqual( out, 'd', 'returns expected value' ); + + out = last( 'hello world', 7, opts ); + t.strictEqual( out, 'o world', 'returns expected value' ); + + out = last( '!!!', 1, opts ); + t.strictEqual( out, '!', 'returns expected value' ); + + out = last( '!!!', 2, opts ); + t.strictEqual( out, '!!', 'returns expected value' ); + + out = last( 'अनुच्छेद', 1, opts ); + t.strictEqual( out, 'द', 'returns expected value' ); + + out = last( '六书/六書', 1, opts ); + t.strictEqual( out, '書', 'returns expected value' ); + + out = last( '🌷', 1, opts ); + t.strictEqual( out, '\udf37', 'returns expected value' ); + + out = last( '👉🏿', 1, opts ); + t.strictEqual( out, '\udfff', 'returns expected value' ); + + t.end(); +});