Skip to content

Commit bbc3cfc

Browse files
feat: add string/base/last-grapheme-cluster
PR-URL: #1810 Closes: #1729 Ref: #854 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent b9d0c57 commit bbc3cfc

File tree

10 files changed

+677
-0
lines changed

10 files changed

+677
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
# lastGraphemeCluster
22+
23+
> Return the last `n` grapheme clusters (i.e., user-perceived characters) of a string.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var lastGraphemeCluster = require( '@stdlib/string/base/last-grapheme-cluster' );
31+
```
32+
33+
#### lastGraphemeCluster( str, n )
34+
35+
Returns the last `n` grapheme clusters (i.e., user-perceived characters) of a string.
36+
37+
```javascript
38+
var out = lastGraphemeCluster( 'Hello World', 1 );
39+
// returns 'd'
40+
41+
out = lastGraphemeCluster( 'Evening', 3 );
42+
// returns 'ing'
43+
44+
out = lastGraphemeCluster( 'foo bar', 10 );
45+
// returns 'foo bar'
46+
```
47+
48+
</section>
49+
50+
<!-- /.usage -->
51+
52+
<section class="examples">
53+
54+
## Examples
55+
56+
<!-- eslint no-undef: "error" -->
57+
58+
```javascript
59+
var lastGraphemeCluster = require( '@stdlib/string/base/last-grapheme-cluster' );
60+
61+
var str = lastGraphemeCluster( 'Hello World!', 1 );
62+
// returns '!'
63+
64+
str = lastGraphemeCluster( 'JavaScript', 6 );
65+
// returns 'Script'
66+
67+
str = lastGraphemeCluster( 'stdlib', 10 );
68+
// returns 'stdlib'
69+
70+
str = lastGraphemeCluster( '🐶🐮🐷🐰🐸', 2 );
71+
// returns '🐰🐸'
72+
73+
str = lastGraphemeCluster( '六书/六書', 2 );
74+
// returns '六書'
75+
```
76+
77+
</section>
78+
79+
<!-- /.examples -->
80+
81+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
82+
83+
<section class="related">
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+
</section>
94+
95+
<!-- /.links -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
'六書',
40+
'xyz abc',
41+
'🐶🐮🐷🐰🐸',
42+
'🌷🌷🌷🌷🌷'
43+
];
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
out = last( values[ i%values.length ], 1 );
48+
if ( typeof out !== 'string' ) {
49+
b.fail( 'should return a string' );
50+
}
51+
}
52+
b.toc();
53+
if ( !isString( out ) ) {
54+
b.fail( 'should return a string' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( str, n )
3+
Returns the last `n` grapheme clusters (i.e., user-perceived characters) of
4+
a string.
5+
6+
Parameters
7+
----------
8+
str: string
9+
Input string.
10+
11+
n: integer
12+
Number of grapheme clusters to return.
13+
14+
Returns
15+
-------
16+
out: string
17+
Output string.
18+
19+
Examples
20+
--------
21+
> var out = {{alias}}( 'beep', 1 )
22+
'p'
23+
> out = {{alias}}( 'Boop', 2 )
24+
'op'
25+
> out = {{alias}}( 'JavaScript', 6 )
26+
'Script'
27+
28+
See Also
29+
--------
30+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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` grapheme clusters (i.e., user-perceived characters) of a string.
23+
*
24+
* @param str - input string
25+
* @param n - number of grapheme clusters to return
26+
* @returns output string
27+
*
28+
* @example
29+
* var out = last( 'Hello World', 1 );
30+
* // returns 'd'
31+
*
32+
* @example
33+
* var out = last( 'Evening', 3 );
34+
* // returns 'ing'
35+
*
36+
* @example
37+
* var out = last( 'JavaScript', 6 );
38+
* // returns 'Script'
39+
*
40+
* @example
41+
* var out = last( '🐶🐮🐷🐰🐸', 2 );
42+
* // returns '🐰🐸'
43+
*
44+
* @example
45+
* var out = last( 'foo bar', 5 );
46+
* // returns 'o bar'
47+
*/
48+
declare function last( str: string, n: number ): string;
49+
50+
51+
// EXPORTS //
52+
53+
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 lastGraphemeCluster = require( './../lib' );
22+
23+
console.log( lastGraphemeCluster( 'Hello World!', 1 ) );
24+
// => '!'
25+
26+
console.log( lastGraphemeCluster( 'JavaScript', 6 ) );
27+
// => 'Script'
28+
29+
console.log( lastGraphemeCluster( 'stdlib', 10 ) );
30+
// => 'stdlib'
31+
32+
console.log( lastGraphemeCluster( '🐶🐮🐷🐰🐸', 2 ) );
33+
// => '🐰🐸'
34+
35+
console.log( lastGraphemeCluster( '六书/六書', 2 ) );
36+
// => '六書'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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` grapheme clusters (i.e., user-perceived characters) of a string.
23+
*
24+
* @module @stdlib/string/base/last-grapheme-cluster
25+
*
26+
* @example
27+
* var last = require( '@stdlib/string/base/last-grapheme-cluster' );
28+
*
29+
* var out = last( 'Hello', 1 );
30+
* // returns 'o';
31+
*
32+
* out = last( 'JavaScript', 1 );
33+
* // returns 'Script';
34+
*
35+
* out = last( '🐮🐷🐸🐵', 2 );
36+
* // returns '🐸🐵';
37+
*/
38+
39+
// MODULES //
40+
41+
var main = require( './main.js' );
42+
43+
44+
// EXPORTS //
45+
46+
module.exports = main;

0 commit comments

Comments
 (0)