Skip to content

Commit 7bce237

Browse files
adityacodes30PranavchikuPlaneshifter
authored
feat: add string/base/last-code-point
PR-URL: #1755 Closes: #1702 --------- Signed-off-by: Aditya Sapra <110766802+adityacodes30@users.noreply.github.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Pranav <85227306+Pranavchiku@users.noreply.github.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Pranav <85227306+Pranavchiku@users.noreply.github.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 6ea8070 commit 7bce237

File tree

10 files changed

+671
-0
lines changed

10 files changed

+671
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
# lastCodePoint
22+
23+
> Return the last `n` Unicode code points of a string.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var lastCodePoint = require( '@stdlib/string/base/last-code-point' );
31+
```
32+
33+
#### lastCodePoint( str, n )
34+
35+
Returns the last `n` Unicode code points of a string.
36+
37+
```javascript
38+
var s = lastCodePoint( 'JavaScript', 1 );
39+
// returns 't'
40+
41+
s = lastCodePoint( 'Hello World', 5 );
42+
// returns 'World'
43+
44+
s = lastCodePoint( 'Good Evening', 9 );
45+
// returns 'd Evening'
46+
47+
s = lastCodePoint( '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 lastCodePoint = require( '@stdlib/string/base/last-code-point' );
63+
64+
var str = lastCodePoint( 'Hello World', 1 );
65+
// returns 'd'
66+
67+
str = lastCodePoint( 'JavaScript', 6 );
68+
// returns 'Script'
69+
70+
str = lastCodePoint( 'ASCII', 2 );
71+
// returns 'II'
72+
73+
str = lastCodePoint( 'index', 10 );
74+
// returns 'index'
75+
76+
str = lastCodePoint( 'अनुच्छेद', 1 );
77+
// returns 'द'
78+
79+
str = lastCodePoint( '六书/六書', 3 );
80+
// returns '/六書'
81+
```
82+
83+
</section>
84+
85+
<!-- /.examples -->
86+
87+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
88+
89+
<section class="related">
90+
91+
92+
</section>
93+
94+
<!-- /.related -->
95+
96+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
97+
98+
<section class="links">
99+
100+
<!-- <related-links> -->
101+
102+
103+
<!-- </related-links> -->
104+
105+
</section>
106+
107+
<!-- /.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+
'new',
39+
'JavaScript',
40+
'beep boop',
41+
'foo bar',
42+
'xyz abc',
43+
'🐶🐮🐷🐰🐸',
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` Unicode code points of a string.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Input string.
9+
10+
n: integer
11+
Number of Unicode code points to return.
12+
13+
Returns
14+
-------
15+
out: string
16+
Output string.
17+
18+
Examples
19+
--------
20+
> var out = {{alias}}( 'hello world', 1 )
21+
'd'
22+
> out = {{alias}}( 'JavaScript', 6 )
23+
'Script'
24+
> out = {{alias}}( 'अनुच्छेद', 1 )
25+
'द'
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` Unicode code points of a string.
23+
*
24+
* @param str - input string
25+
* @param n - number of code points 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( 'JavaScript', 6 );
34+
* // returns 'Script'
35+
*
36+
* @example
37+
* var out = last( 'New', 5 );
38+
* // returns 'New'
39+
*
40+
* @example
41+
* var out = last( 'अनुच्छेद', 1 );
42+
* // returns 'द'
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: 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 lastCodePoint = require( './../lib' );
22+
23+
console.log( lastCodePoint( 'Hello World', 1 ) );
24+
// => 'd'
25+
26+
console.log( lastCodePoint( 'JavaScript', 6 ) );
27+
// => 'Script'
28+
29+
console.log( lastCodePoint( 'index', 10 ) );
30+
// => 'index'
31+
32+
console.log( lastCodePoint( 'अनुच्छेद', 1 ) );
33+
// => 'द'
34+
35+
console.log( lastCodePoint( '六书/六書', 3 ) );
36+
// => '/六書'
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` Unicode code points of a string.
23+
*
24+
* @module @stdlib/string/base/last-code-point
25+
*
26+
* @example
27+
* var last = require( '@stdlib/string/base/last-code-point' );
28+
*
29+
* var out = last( 'Hello World', 1 );
30+
* // returns 'd'
31+
*
32+
* out = last( 'अनुच्छेद', 1 );
33+
* // returns 'द';
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)