Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Apr 9, 2024
1 parent 94cec0e commit ccb5ff6
Show file tree
Hide file tree
Showing 15 changed files with 1,920 additions and 0 deletions.
222 changes: 222 additions & 0 deletions last/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
<!--
@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.
-->

# last

> Return the last character(s) of a string.
<section class="usage">

## 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'
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## 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.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```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 '🐶🐮🐷🐰🐸'
```

</section>

<!-- /.examples -->

* * *

<section class="cli">

## CLI

<section class="usage">

### Usage

```text
Usage: last [options] [<string>]
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'.
```

</section>

<!-- /.usage -->

<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

### 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.

</section>

<!-- /.notes -->

<section class="examples">

### 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
```

</section>

<!-- /.examples -->

</section>

<!-- /.cli -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

* * *


</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams

[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
145 changes: 145 additions & 0 deletions last/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -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();
});
Loading

0 comments on commit ccb5ff6

Please sign in to comment.