Skip to content

Commit c30bb89

Browse files
authored
feat: add string/base/slice-code-points
PR-URL: #5414 Closes: stdlib-js/metr-issue-tracker#34 Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Muhammad Haris <harriskhan047@outlook.com>
1 parent c9e3ff1 commit c30bb89

File tree

10 files changed

+683
-0
lines changed

10 files changed

+683
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# sliceCodePoints
22+
23+
> Slice a string based on Unicode code point indices.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var sliceCodePoints = require( '@stdlib/string/base/slice-code-points' );
31+
```
32+
33+
#### sliceCodePoints( str, start, end )
34+
35+
Slices a string based on Unicode code point indices.
36+
37+
```javascript
38+
var out = sliceCodePoints( 'Hello 👋 World', 0, 7 );
39+
// returns 'Hello 👋'
40+
41+
out = sliceCodePoints( '👋👋👋', 1, 2 );
42+
// returns '👋'
43+
```
44+
45+
The function accepts the following arguments:
46+
47+
- **str**: input string.
48+
- **start**: the `ith` Unicode code point to start a slice (inclusive).
49+
- **end**: the `jth` Unicode code point to end a slice (exclusive).
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="examples">
56+
57+
## Examples
58+
59+
```javascript
60+
var sliceCodePoints = require( '@stdlib/string/base/slice-code-points' );
61+
62+
console.log( sliceCodePoints( 'Hello 👋 World', 0, 7 ) );
63+
// => 'Hello 👋'
64+
65+
console.log( sliceCodePoints( 'last man standing', 1, 17 ) );
66+
// => 'ast man standing'
67+
68+
console.log( sliceCodePoints( '六书/六書', 1, 14 ) );
69+
// => '书/六書'
70+
```
71+
72+
</section>
73+
74+
<!-- /.examples -->
75+
76+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
77+
78+
<section class="related">
79+
80+
</section>
81+
82+
<!-- /.related -->
83+
84+
<!-- Section for all links. Do not manually edit this section, as it is automatically populated. -->
85+
86+
<section class="links">
87+
88+
</section>
89+
90+
<!-- /.links -->
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) 2025 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 sliceCodePoints = 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+
'Iñtërnâtiônàlizætiøn',
38+
'presidential election',
39+
'🐶🐮🐷🐰🐸',
40+
'Hello 👋 World',
41+
'अनुच्छेद'
42+
];
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
out = sliceCodePoints( values[ i%values.length ], 1, 3 );
47+
if ( typeof out !== 'string' ) {
48+
b.fail( 'should return a string' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isString( out ) ) {
53+
b.fail( 'should return a string' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( str, start, end )
3+
Slices a string based on Unicode code point indices.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Input string.
9+
start: integer
10+
The `ith` Unicode code point to start a slice (inclusive).
11+
end: integer
12+
The `jth` Unicode code point to end a slice (exclusive).
13+
14+
Returns
15+
-------
16+
out: string
17+
Output string.
18+
19+
Examples
20+
--------
21+
> var out = {{alias}}( 'Hello 👋 World', 1, 3 )
22+
'el'
23+
> out = {{alias}}( '👋👋👋', 1, 2 )
24+
'👋'
25+
> out = {{alias}}( '六书/六書', 1, 14 )
26+
'书/六書'
27+
28+
See Also
29+
--------
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
* Slices a string based on Unicode code point indices.
23+
*
24+
* @param str - input string
25+
* @param start - the `ith` Unicode code point to start a slice (inclusive)
26+
* @param end - the `jth` Unicode code point to end a slice (exclusive)
27+
* @returns output string
28+
*
29+
* @example
30+
* var out = sliceCodePoints( 'Hello 👋 World', 1, 3 );
31+
* // returns 'el'
32+
*
33+
* @example
34+
* var out = sliceCodePoints( '👋👋👋', 1, 2 );
35+
* // returns '👋'
36+
*/
37+
declare function sliceCodePoints( str: string, start: number, end: number ): string;
38+
39+
40+
// EXPORTS //
41+
42+
export = sliceCodePoints;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 sliceCodePoints = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
sliceCodePoints( 'abc', 1, 2 ); // $ExpectType string
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a string...
30+
{
31+
sliceCodePoints( true, 1, 2 ); // $ExpectError
32+
sliceCodePoints( false, 1, 2 ); // $ExpectError
33+
sliceCodePoints( null, 1, 2 ); // $ExpectError
34+
sliceCodePoints( undefined, 1, 2 ); // $ExpectError
35+
sliceCodePoints( 5, 1, 2 ); // $ExpectError
36+
sliceCodePoints( [], 1, 2 ); // $ExpectError
37+
sliceCodePoints( {}, 1, 2 ); // $ExpectError
38+
sliceCodePoints( ( x: number ): number => x, 1, 2 ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided a second argument that is not a number...
42+
{
43+
sliceCodePoints( 'abc', true, 2 ); // $ExpectError
44+
sliceCodePoints( 'abc', false, 2 ); // $ExpectError
45+
sliceCodePoints( 'abc', null, 2 ); // $ExpectError
46+
sliceCodePoints( 'abc', 'abc', 2 ); // $ExpectError
47+
sliceCodePoints( 'abc', [], 2 ); // $ExpectError
48+
sliceCodePoints( 'abc', {}, 2 ); // $ExpectError
49+
sliceCodePoints( 'abc', ( x: number ): number => x, 2 ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided a third argument that is not a number...
53+
{
54+
sliceCodePoints( 'abc', 1, true ); // $ExpectError
55+
sliceCodePoints( 'abc', 1, false ); // $ExpectError
56+
sliceCodePoints( 'abc', 1, null ); // $ExpectError
57+
sliceCodePoints( 'abc', 1, 'abc' ); // $ExpectError
58+
sliceCodePoints( 'abc', 1, [] ); // $ExpectError
59+
sliceCodePoints( 'abc', 1, {} ); // $ExpectError
60+
sliceCodePoints( 'abc', 1, ( x: number ): number => x ); // $ExpectError
61+
}
62+
63+
// The compiler throws an error if the function is provided an unsupported number of arguments...
64+
{
65+
sliceCodePoints(); // $ExpectError
66+
sliceCodePoints( 'abc' ); // $ExpectError
67+
sliceCodePoints( 'abc', 1, 2, {} ); // $ExpectError
68+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 sliceCodePoints = require( './../lib' );
22+
23+
console.log( sliceCodePoints( 'Hello 👋 World', 0, 7 ) );
24+
// => 'Hello 👋'
25+
26+
console.log( sliceCodePoints( 'last man standing', 1, 17 ) );
27+
// => 'ast man standing'
28+
29+
console.log( sliceCodePoints( '六书/六書', 1, 14 ) );
30+
// => '书/六書'
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) 2025 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+
* Slice a string based on Unicode code point indices.
23+
*
24+
* @module @stdlib/string/base/slice-code-points
25+
*
26+
* @example
27+
* var sliceCodePoints = require( '@stdlib/string/base/slice-code-points' );
28+
*
29+
* var out = sliceCodePoints( 'Hello 👋 World', 1, 3 );
30+
* // returns 'el'
31+
*
32+
* out = sliceCodePoints( '👋👋👋', 1, 2 );
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)