Skip to content

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 11 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions lib/node_modules/@stdlib/string/base/slice/README.md
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 lib/node_modules/@stdlib/string/base/slice/benchmark/benchmark.js
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();
});
31 changes: 31 additions & 0 deletions lib/node_modules/@stdlib/string/base/slice/docs/repl.txt
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 lib/node_modules/@stdlib/string/base/slice/docs/types/index.d.ts
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 lib/node_modules/@stdlib/string/base/slice/docs/types/test.ts
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 lib/node_modules/@stdlib/string/base/slice/examples/index.js
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'
43 changes: 43 additions & 0 deletions lib/node_modules/@stdlib/string/base/slice/lib/index.js
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;
Loading