-
-
Notifications
You must be signed in to change notification settings - Fork 837
feat: add string/base/slice
#5395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
054640b
feat: add string/base/slice
headlessNode c1f409d
test: add test case
headlessNode 38a7898
docs: apply suggestions from code review
headlessNode 56a0788
docs: apply suggestions from code review
headlessNode 32e02da
docs: update comment
kgryte 300917a
Apply suggestions from code review
kgryte 03c6312
refactor: cache reference to built-in
kgryte e53f1e7
test: remove test
kgryte 2bf8375
style: add empty line
kgryte c29cbf3
docs: add function args description
headlessNode 32e6c62
docs: fix typo
headlessNode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2025 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# slice | ||
|
||
> Slice UTF-16 code units from a string. | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var slice = require( '@stdlib/string/base/slice' ); | ||
``` | ||
|
||
#### slice( str, start, end ) | ||
|
||
Slices UTF-16 code units from a string. | ||
|
||
```javascript | ||
var out = slice( 'last man standing', 1, 17 ); | ||
// returns 'ast man standing' | ||
|
||
out = slice( 'Hidden Treasures', 0, 6 ); | ||
// returns 'Hidden' | ||
|
||
out = slice( 'foo bar', 2, 7 ); | ||
// returns 'o bar' | ||
|
||
out = slice( 'foo bar', -1, 7 ); | ||
// returns 'r' | ||
``` | ||
|
||
The function accepts the following arguments: | ||
|
||
- **str**: input string. | ||
- **start**: slice start index (inclusive). | ||
- **end**: slice end index (exclusive). | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var slice = require( '@stdlib/string/base/slice' ); | ||
|
||
var str = slice( 'presidential election', 1, 21 ); | ||
// returns 'residential election' | ||
|
||
str = slice( 'JavaScript', 4, 10 ); | ||
// returns 'Script' | ||
|
||
str = slice( 'The Last of the Mohicans', 5, 24 ); | ||
// returns 'ast of the Mohicans' | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
<!-- <related-links> --> | ||
|
||
<!-- </related-links> --> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
56 changes: 56 additions & 0 deletions
56
lib/node_modules/@stdlib/string/base/slice/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var isString = require( '@stdlib/assert/is-string' ).isPrimitive; | ||
var pkg = require( './../package.json' ).name; | ||
var slice = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var values; | ||
var out; | ||
var i; | ||
|
||
values = [ | ||
'beep boop', | ||
'foo bar', | ||
'xyz abc', | ||
'🐶🐮🐷🐰🐸' | ||
]; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
out = slice( values[ i%values.length ], 1, 6 ); | ||
if ( typeof out !== 'string' ) { | ||
b.fail( 'should return a string' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isString( out ) ) { | ||
b.fail( 'should return a string' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
{{alias}}( str, start, end ) | ||
Slices UTF-16 code units from a string. | ||
|
||
Parameters | ||
---------- | ||
str: string | ||
Input string. | ||
|
||
start: integer | ||
Slice start index (inclusive). | ||
|
||
end: integer | ||
Slice end index (exclusive). | ||
|
||
Returns | ||
------- | ||
out: string | ||
Output string. | ||
|
||
Examples | ||
-------- | ||
> var out = {{alias}}( 'beep', 1, 4 ) | ||
'eep' | ||
> out = {{alias}}( 'Boop', 1, 4 ) | ||
'oop' | ||
> out = {{alias}}( 'foo bar', 5, 7 ) | ||
'ar' | ||
|
||
See Also | ||
-------- |
58 changes: 58 additions & 0 deletions
58
lib/node_modules/@stdlib/string/base/slice/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// TypeScript Version: 4.1 | ||
|
||
/** | ||
* Slices UTF-16 code units from a string. | ||
* | ||
* @param str - input string | ||
* @param start - slice start index (inclusive) | ||
* @param end - slice end index (exclusive) | ||
* @returns output string | ||
* | ||
* @example | ||
* var out = slice( 'last man standing', 1, 17 ); | ||
* // returns 'ast man standing' | ||
* | ||
* @example | ||
* var out = slice( 'presidential election', 1, 21 ); | ||
* // returns 'residential election' | ||
* | ||
* @example | ||
* var out = slice( 'JavaScript', 4, 10 ); | ||
* // returns 'Script' | ||
* | ||
* @example | ||
* var out = slice( 'Hidden Treasures', 0, 6 ); | ||
* // returns 'Hidden' | ||
* | ||
* @example | ||
* var out = slice( 'foo bar', 2, 7 ); | ||
* // returns 'ar' | ||
* | ||
* @example | ||
* var out = slice( 'foo bar', -1, 7 ); | ||
* // returns 'r' | ||
*/ | ||
declare function slice( str: string, start: number, end: number ): string; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = slice; |
68 changes: 68 additions & 0 deletions
68
lib/node_modules/@stdlib/string/base/slice/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import slice = require( './index' ); | ||
|
||
|
||
// TESTS // | ||
|
||
// The function returns a string... | ||
{ | ||
slice( 'abc', 1, 4 ); // $ExpectType string | ||
} | ||
|
||
// The compiler throws an error if the function is provided a first argument that is not a string... | ||
{ | ||
slice( true, 1, 3 ); // $ExpectError | ||
slice( false, 1, 3 ); // $ExpectError | ||
slice( null, 1, 3 ); // $ExpectError | ||
slice( undefined, 1, 3 ); // $ExpectError | ||
slice( 5, 1, 3 ); // $ExpectError | ||
slice( [], 1, 3 ); // $ExpectError | ||
slice( {}, 1, 3 ); // $ExpectError | ||
slice( ( x: number ): number => x, 1, 3 ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided a second argument that is not a number... | ||
{ | ||
slice( 'abc', true, 3 ); // $ExpectError | ||
slice( 'abc', false, 3 ); // $ExpectError | ||
slice( 'abc', null, 3 ); // $ExpectError | ||
slice( 'abc', 'abc', 3 ); // $ExpectError | ||
slice( 'abc', [], 3 ); // $ExpectError | ||
slice( 'abc', {}, 3 ); // $ExpectError | ||
slice( 'abc', ( x: number ): number => x, 3 ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided a third argument that is not a number... | ||
{ | ||
slice( 'abc', 1, true ); // $ExpectError | ||
slice( 'abc', 1, false ); // $ExpectError | ||
slice( 'abc', 1, null ); // $ExpectError | ||
slice( 'abc', 1, 'abc' ); // $ExpectError | ||
slice( 'abc', 1, [] ); // $ExpectError | ||
slice( 'abc', 1, {} ); // $ExpectError | ||
slice( 'abc', 1, ( x: number ): number => x ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if the function is provided an unsupported number of arguments... | ||
{ | ||
slice(); // $ExpectError | ||
slice( 'abc' ); // $ExpectError | ||
slice( 'abc', 1, 2, {} ); // $ExpectError | ||
} |
30 changes: 30 additions & 0 deletions
30
lib/node_modules/@stdlib/string/base/slice/examples/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var slice = require( './../lib' ); | ||
|
||
console.log( slice( 'presidential election', 1, 21 ) ); | ||
// => 'residential election' | ||
|
||
console.log( slice( 'JavaScript', 4, 10 ) ); | ||
// => 'Script' | ||
|
||
console.log( slice( 'The Last of the Mohicans', 5, 24 ) ); | ||
// => 'ast of the Mohicans' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* Slice UTF-16 code units from a string. | ||
* | ||
* @module @stdlib/string/base/slice | ||
* | ||
* @example | ||
* var slice = require( '@stdlib/string/base/slice' ); | ||
* | ||
* var out = slice( 'last man standing', 1, 17 ); | ||
* // returns 'ast man standing' | ||
* | ||
* out = removeFirst( 'Hidden Treasures', 0, 6 ); | ||
* // returns 'Hidden'; | ||
*/ | ||
|
||
// MODULES // | ||
|
||
var main = require( './main.js' ); | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = main; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.