Skip to content

Commit 38b39db

Browse files
headlessNodekgryte
andauthored
feat: add string/base/slice
PR-URL: #5395 Closes: stdlib-js/metr-issue-tracker#33 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Muhammad Haris <harriskhan047@outlook.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent ff9453b commit 38b39db

File tree

10 files changed

+603
-0
lines changed

10 files changed

+603
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
# slice
22+
23+
> Slice UTF-16 code units from a string.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var slice = require( '@stdlib/string/base/slice' );
31+
```
32+
33+
#### slice( str, start, end )
34+
35+
Slices UTF-16 code units from a string.
36+
37+
```javascript
38+
var out = slice( 'last man standing', 1, 17 );
39+
// returns 'ast man standing'
40+
41+
out = slice( 'Hidden Treasures', 0, 6 );
42+
// returns 'Hidden'
43+
44+
out = slice( 'foo bar', 2, 7 );
45+
// returns 'o bar'
46+
47+
out = slice( 'foo bar', -1, 7 );
48+
// returns 'r'
49+
```
50+
51+
The function accepts the following arguments:
52+
53+
- **str**: input string.
54+
- **start**: slice start index (inclusive).
55+
- **end**: slice end index (exclusive).
56+
57+
</section>
58+
59+
<!-- /.usage -->
60+
61+
<section class="examples">
62+
63+
## Examples
64+
65+
<!-- eslint no-undef: "error" -->
66+
67+
```javascript
68+
var slice = require( '@stdlib/string/base/slice' );
69+
70+
var str = slice( 'presidential election', 1, 21 );
71+
// returns 'residential election'
72+
73+
str = slice( 'JavaScript', 4, 10 );
74+
// returns 'Script'
75+
76+
str = slice( 'The Last of the Mohicans', 5, 24 );
77+
// returns 'ast of the Mohicans'
78+
```
79+
80+
</section>
81+
82+
<!-- /.examples -->
83+
84+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
85+
86+
<section class="related">
87+
88+
</section>
89+
90+
<!-- /.related -->
91+
92+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
93+
94+
<section class="links">
95+
96+
<!-- <related-links> -->
97+
98+
<!-- </related-links> -->
99+
100+
</section>
101+
102+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 slice = 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+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
out = slice( values[ i%values.length ], 1, 6 );
46+
if ( typeof out !== 'string' ) {
47+
b.fail( 'should return a string' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isString( out ) ) {
52+
b.fail( 'should return a string' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
{{alias}}( str, start, end )
3+
Slices UTF-16 code units from a string.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Input string.
9+
10+
start: integer
11+
Slice start index (inclusive).
12+
13+
end: integer
14+
Slice end index (exclusive).
15+
16+
Returns
17+
-------
18+
out: string
19+
Output string.
20+
21+
Examples
22+
--------
23+
> var out = {{alias}}( 'beep', 1, 4 )
24+
'eep'
25+
> out = {{alias}}( 'Boop', 1, 4 )
26+
'oop'
27+
> out = {{alias}}( 'foo bar', 5, 7 )
28+
'ar'
29+
30+
See Also
31+
--------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 UTF-16 code units from a string.
23+
*
24+
* @param str - input string
25+
* @param start - slice start index (inclusive)
26+
* @param end - slice end index (exclusive)
27+
* @returns output string
28+
*
29+
* @example
30+
* var out = slice( 'last man standing', 1, 17 );
31+
* // returns 'ast man standing'
32+
*
33+
* @example
34+
* var out = slice( 'presidential election', 1, 21 );
35+
* // returns 'residential election'
36+
*
37+
* @example
38+
* var out = slice( 'JavaScript', 4, 10 );
39+
* // returns 'Script'
40+
*
41+
* @example
42+
* var out = slice( 'Hidden Treasures', 0, 6 );
43+
* // returns 'Hidden'
44+
*
45+
* @example
46+
* var out = slice( 'foo bar', 2, 7 );
47+
* // returns 'ar'
48+
*
49+
* @example
50+
* var out = slice( 'foo bar', -1, 7 );
51+
* // returns 'r'
52+
*/
53+
declare function slice( str: string, start: number, end: number ): string;
54+
55+
56+
// EXPORTS //
57+
58+
export = slice;
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 slice = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
slice( 'abc', 1, 4 ); // $ExpectType string
27+
}
28+
29+
// The compiler throws an error if the function is provided a first argument that is not a string...
30+
{
31+
slice( true, 1, 3 ); // $ExpectError
32+
slice( false, 1, 3 ); // $ExpectError
33+
slice( null, 1, 3 ); // $ExpectError
34+
slice( undefined, 1, 3 ); // $ExpectError
35+
slice( 5, 1, 3 ); // $ExpectError
36+
slice( [], 1, 3 ); // $ExpectError
37+
slice( {}, 1, 3 ); // $ExpectError
38+
slice( ( x: number ): number => x, 1, 3 ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided a second argument that is not a number...
42+
{
43+
slice( 'abc', true, 3 ); // $ExpectError
44+
slice( 'abc', false, 3 ); // $ExpectError
45+
slice( 'abc', null, 3 ); // $ExpectError
46+
slice( 'abc', 'abc', 3 ); // $ExpectError
47+
slice( 'abc', [], 3 ); // $ExpectError
48+
slice( 'abc', {}, 3 ); // $ExpectError
49+
slice( 'abc', ( x: number ): number => x, 3 ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided a third argument that is not a number...
53+
{
54+
slice( 'abc', 1, true ); // $ExpectError
55+
slice( 'abc', 1, false ); // $ExpectError
56+
slice( 'abc', 1, null ); // $ExpectError
57+
slice( 'abc', 1, 'abc' ); // $ExpectError
58+
slice( 'abc', 1, [] ); // $ExpectError
59+
slice( 'abc', 1, {} ); // $ExpectError
60+
slice( '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+
slice(); // $ExpectError
66+
slice( 'abc' ); // $ExpectError
67+
slice( 'abc', 1, 2, {} ); // $ExpectError
68+
}
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 slice = require( './../lib' );
22+
23+
console.log( slice( 'presidential election', 1, 21 ) );
24+
// => 'residential election'
25+
26+
console.log( slice( 'JavaScript', 4, 10 ) );
27+
// => 'Script'
28+
29+
console.log( slice( 'The Last of the Mohicans', 5, 24 ) );
30+
// => 'ast of the Mohicans'
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 UTF-16 code units from a string.
23+
*
24+
* @module @stdlib/string/base/slice
25+
*
26+
* @example
27+
* var slice = require( '@stdlib/string/base/slice' );
28+
*
29+
* var out = slice( 'last man standing', 1, 17 );
30+
* // returns 'ast man standing'
31+
*
32+
* out = removeFirst( 'Hidden Treasures', 0, 6 );
33+
* // returns 'Hidden';
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)