Skip to content

Commit 22aaef5

Browse files
feat: add string/base/last
PR-URL: #2145 Closed: #854 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 8cc469d commit 22aaef5

File tree

15 files changed

+1920
-0
lines changed

15 files changed

+1920
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# last
22+
23+
> Return the last character(s) of a string.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var last = require( '@stdlib/string/last' );
31+
```
32+
33+
#### last( str\[, n]\[, options] )
34+
35+
Returns the last character(s) of a string.
36+
37+
```javascript
38+
var out = last( 'last man standing' );
39+
// returns 'g'
40+
41+
out = last( 'Hidden Treasures' );
42+
// returns 's'
43+
```
44+
45+
The function supports the following options:
46+
47+
- **mode**: type of characters to return. Must be one of the following:
48+
49+
- `'grapheme'`: grapheme clusters. Appropriate for strings containing visual characters which can span multiple Unicode code points (e.g., emoji).
50+
- `'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).
51+
- `'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).
52+
53+
Default: `'grapheme'`.
54+
55+
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.
56+
57+
```javascript
58+
var out = last( 'foo bar', 3 );
59+
// returns 'bar'
60+
61+
out = last( 'foo bar', 10 );
62+
// returns 'foo bar'
63+
```
64+
65+
</section>
66+
67+
<!-- /.usage -->
68+
69+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
70+
71+
<section class="notes">
72+
73+
## Notes
74+
75+
- 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.
76+
77+
</section>
78+
79+
<!-- /.notes -->
80+
81+
<section class="examples">
82+
83+
## Examples
84+
85+
<!-- eslint no-undef: "error" -->
86+
87+
```javascript
88+
var last = require( '@stdlib/string/last' );
89+
90+
var str = last( 'last man standing' );
91+
// returns 'g'
92+
93+
str = last( 'presidential election' );
94+
// returns 'n'
95+
96+
str = last( 'javaScript' );
97+
// returns 't'
98+
99+
str = last( 'Hidden Treasures' );
100+
// returns 's'
101+
102+
str = last( 'The Last of the Mohicans', 8 );
103+
// returns 'Mohicans'
104+
105+
str = last( '🐶🐮🐷🐰🐸', 2 );
106+
// returns '🐰🐸'
107+
108+
str = last( '🐶🐮🐷🐰🐸', 10 );
109+
// returns '🐶🐮🐷🐰🐸'
110+
```
111+
112+
</section>
113+
114+
<!-- /.examples -->
115+
116+
* * *
117+
118+
<section class="cli">
119+
120+
## CLI
121+
122+
<section class="usage">
123+
124+
### Usage
125+
126+
```text
127+
Usage: last [options] [<string>]
128+
129+
Options:
130+
131+
-h, --help Print this message.
132+
-V, --version Print the package version.
133+
--n num Number of characters to return. Default: 1.
134+
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
135+
--mode mode Type of character to return. Default: 'grapheme'.
136+
```
137+
138+
</section>
139+
140+
<!-- /.usage -->
141+
142+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
143+
144+
<section class="notes">
145+
146+
### Notes
147+
148+
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
149+
150+
```bash
151+
# Not escaped...
152+
$ echo -n $'beep\nboop' | last --split /\r?\n/
153+
154+
# Escaped...
155+
$ echo -n $'beep\nboop' | last --split /\\r?\\n/
156+
```
157+
158+
- The implementation ignores trailing delimiters.
159+
160+
</section>
161+
162+
<!-- /.notes -->
163+
164+
<section class="examples">
165+
166+
### Examples
167+
168+
```bash
169+
$ last beep
170+
p
171+
```
172+
173+
To use as a [standard stream][standard-streams],
174+
175+
```bash
176+
$ echo -n 'beep\nboop' | last --n=2
177+
ep
178+
op
179+
```
180+
181+
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.
182+
183+
```bash
184+
$ echo -n 'beep\tboop' | last --split '\t'
185+
p
186+
p
187+
```
188+
189+
</section>
190+
191+
<!-- /.examples -->
192+
193+
</section>
194+
195+
<!-- /.cli -->
196+
197+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
198+
199+
<section class="related">
200+
201+
* * *
202+
203+
204+
</section>
205+
206+
<!-- /.related -->
207+
208+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
209+
210+
<section class="links">
211+
212+
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
213+
214+
[mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
215+
216+
<!-- <related-links> -->
217+
218+
<!-- </related-links> -->
219+
220+
</section>
221+
222+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var last = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var out;
34+
var i;
35+
36+
values = [
37+
'beep boop',
38+
'foo bar',
39+
'xyz abc'
40+
];
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
out = last( values[ i%values.length ] );
45+
if ( typeof out !== 'string' ) {
46+
b.fail( 'should return a string' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isString( out ) ) {
51+
b.fail( 'should return a string' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
56+
57+
bench( pkg+':mode=grapheme', function benchmark( b ) {
58+
var values;
59+
var opts;
60+
var out;
61+
var i;
62+
63+
values = [
64+
'beep boop',
65+
'foo bar',
66+
'xyz abc'
67+
];
68+
opts = {
69+
'mode': 'grapheme'
70+
};
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
out = last( values[ i%values.length ], opts );
75+
if ( typeof out !== 'string' ) {
76+
b.fail( 'should return a string' );
77+
}
78+
}
79+
b.toc();
80+
if ( !isString( out ) ) {
81+
b.fail( 'should return a string' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
});
86+
87+
bench( pkg+':mode=code_point', function benchmark( b ) {
88+
var values;
89+
var opts;
90+
var out;
91+
var i;
92+
93+
values = [
94+
'beep boop',
95+
'foo bar',
96+
'xyz abc'
97+
];
98+
opts = {
99+
'mode': 'code_point'
100+
};
101+
102+
b.tic();
103+
for ( i = 0; i < b.iterations; i++ ) {
104+
out = last( values[ i%values.length ], opts );
105+
if ( typeof out !== 'string' ) {
106+
b.fail( 'should return a string' );
107+
}
108+
}
109+
b.toc();
110+
if ( !isString( out ) ) {
111+
b.fail( 'should return a string' );
112+
}
113+
b.pass( 'benchmark finished' );
114+
b.end();
115+
});
116+
117+
bench( pkg+':mode=code_unit', function benchmark( b ) {
118+
var values;
119+
var opts;
120+
var out;
121+
var i;
122+
123+
values = [
124+
'beep boop',
125+
'foo bar',
126+
'xyz abc'
127+
];
128+
opts = {
129+
'mode': 'code_unit'
130+
};
131+
132+
b.tic();
133+
for ( i = 0; i < b.iterations; i++ ) {
134+
out = last( values[ i%values.length ], opts );
135+
if ( typeof out !== 'string' ) {
136+
b.fail( 'should return a string' );
137+
}
138+
}
139+
b.toc();
140+
if ( !isString( out ) ) {
141+
b.fail( 'should return a string' );
142+
}
143+
b.pass( 'benchmark finished' );
144+
b.end();
145+
});

0 commit comments

Comments
 (0)