Skip to content

Commit 3b6b680

Browse files
feat: add string/base/last
PR-URL: #1697 Closes: #1696 --------- Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 2a852fd commit 3b6b680

File tree

10 files changed

+586
-0
lines changed

10 files changed

+586
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 `n` UTF-16 code units of a string.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var last = require( '@stdlib/string/base/last' );
31+
```
32+
33+
#### last( str, n )
34+
35+
Returns the last `n` UTF-16 code units of a string.
36+
37+
```javascript
38+
var s = last( 'hello world', 1 );
39+
// returns 'd'
40+
41+
s = last( 'this is stdlib', 1 );
42+
// returns 'b'
43+
44+
s = last( 'foo', 2 );
45+
// returns 'oo'
46+
47+
s = last( 'foo bar', 10 );
48+
// returns 'foo bar'
49+
```
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="examples">
56+
57+
## Examples
58+
59+
<!-- eslint no-undef: "error" -->
60+
61+
```javascript
62+
var last = require( '@stdlib/string/base/last' );
63+
64+
var str = last( 'elections', 1 );
65+
// returns 's'
66+
67+
str = last( 'JavaScript', 1 );
68+
// returns 't'
69+
70+
str = last( 'good night', 5 );
71+
// returns 'night'
72+
```
73+
74+
</section>
75+
76+
<!-- /.examples -->
77+
78+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
79+
80+
<section class="related">
81+
82+
* * *
83+
84+
85+
</section>
86+
87+
<!-- /.related -->
88+
89+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
90+
91+
<section class="links">
92+
93+
<!-- <related-links> -->
94+
95+
96+
<!-- </related-links> -->
97+
98+
</section>
99+
100+
<!-- /.links -->
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
'hello world',
38+
'abc',
39+
'',
40+
'Javascript',
41+
'beep boop',
42+
'foo bar',
43+
'xyz abc',
44+
'🐶🐮🐷🐰🐸'
45+
];
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
out = last( values[ i%values.length ], 1 );
50+
if ( typeof out !== 'string' ) {
51+
b.fail( 'should return a string' );
52+
}
53+
}
54+
b.toc();
55+
if ( !isString( out ) ) {
56+
b.fail( 'should return a string' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( str, n )
3+
Returns the last `n` UTF-16 code units of a string.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Input string.
9+
10+
n: integer
11+
Number of UTF-16 code units to return.
12+
13+
Returns
14+
-------
15+
out: string
16+
Output string.
17+
18+
Examples
19+
--------
20+
> var out = {{alias}}( 'hello', 1 )
21+
'o'
22+
> out = {{alias}}( 'JavaScript', 6 )
23+
'Script'
24+
> out = {{alias}}( 'foo bar', 10 )
25+
'foo bar'
26+
27+
See Also
28+
--------
29+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Returns the last `n` UTF-16 code units of a string.
23+
*
24+
* @param str - input string
25+
* @param n - number of code units to return
26+
* @returns output string
27+
*
28+
* @example
29+
* var s = last( 'hello world', 1 );
30+
* // returns 'd'
31+
*
32+
* @example
33+
* var s = last( 'foo', 2 );
34+
* // returns 'oo'
35+
*
36+
* @example
37+
* var s = last( 'JavaScript', 6 );
38+
* // returns 'Script'
39+
*
40+
* @example
41+
* var s = last( 'foo bar', 10 );
42+
* // returns 'foo bar'
43+
*/
44+
declare function last( str: string, n: number ): string;
45+
46+
47+
// EXPORTS //
48+
49+
export = last;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
import last = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
last( 'abc', 1 ); // $ExpectType string
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a string...
30+
{
31+
last( true, 1 ); // $ExpectError
32+
last( false, 1 ); // $ExpectError
33+
last( null, 1 ); // $ExpectError
34+
last( undefined, 1 ); // $ExpectError
35+
last( 5, 1 ); // $ExpectError
36+
last( [], 1 ); // $ExpectError
37+
last( {}, 1 ); // $ExpectError
38+
last( ( x: number ): number => x, 1 ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided a second argument that is not a number...
42+
{
43+
last( 'abc', true ); // $ExpectError
44+
last( 'abc', false ); // $ExpectError
45+
last( 'abc', null ); // $ExpectError
46+
last( 'abc', 'abc' ); // $ExpectError
47+
last( 'abc', [] ); // $ExpectError
48+
last( 'abc', {} ); // $ExpectError
49+
last( 'abc', ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided an unsupported number of arguments...
53+
{
54+
last(); // $ExpectError
55+
last( 'abc' ); // $ExpectError
56+
last( 'abc', 1, 2 ); // $ExpectError
57+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
var last = require( './../lib' );
22+
23+
console.log( last( 'hello world', 1 ) );
24+
// => 'd'
25+
26+
console.log( last( 'JavaScript', 6 ) );
27+
// => 'Script'
28+
29+
console.log( last( 'New', 6 ) );
30+
// => 'New'
31+
32+
console.log( last( 'EVENING', 5 ) );
33+
// => 'ENING'
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
/**
22+
* Return the last `n` UTF-16 code units of a string.
23+
*
24+
* @module @stdlib/string/base/last
25+
*
26+
* @example
27+
* var last = require( '@stdlib/string/base/last' );
28+
*
29+
* var s = last( 'hello world', 1 );
30+
* // returns 'd'
31+
*
32+
* var out = last( 'JavaScript', 6 );
33+
* // returns 'Script'
34+
*/
35+
36+
// MODULES //
37+
38+
var main = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = main;

0 commit comments

Comments
 (0)