Skip to content

feat: add string/base/slice-code-points #5414

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 10 commits into from
Apr 9, 2025
90 changes: 90 additions & 0 deletions lib/node_modules/@stdlib/string/base/slice-code-points/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<!--

@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.

-->

# sliceCodePoints

> Slice a string based on Unicode code point indices.

<section class="usage">

## Usage

```javascript
var sliceCodePoints = require( '@stdlib/string/base/slice-code-points' );
```

#### sliceCodePoints( str, start, end )

Slices a string based on Unicode code point indices.

```javascript
var out = sliceCodePoints( 'Hello 👋 World', 0, 7 );
// returns 'Hello 👋'

out = sliceCodePoints( '👋👋👋', 1, 2 );
// returns '👋'
```

The function accepts the following arguments:

- **str**: input string.
- **start**: the `ith` Unicode code point to start a slice (inclusive).
- **end**: the `jth` Unicode code point to end a slice (exclusive).

</section>

<!-- /.usage -->

<section class="examples">

## Examples

```javascript
var sliceCodePoints = require( '@stdlib/string/base/slice-code-points' );

console.log( sliceCodePoints( 'Hello 👋 World', 0, 7 ) );
// => 'Hello 👋'

console.log( sliceCodePoints( 'last man standing', 1, 17 ) );
// => 'ast man standing'

console.log( sliceCodePoints( '六书/六書', 1, 14 ) );
// => '书/六書'
```

</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. Do not manually edit this section, as it is automatically populated. -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @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 sliceCodePoints = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var out;
var i;

values = [
'Iñtërnâtiônàlizætiøn',
'presidential election',
'🐶🐮🐷🐰🐸',
'Hello 👋 World',
'अनुच्छेद'
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = sliceCodePoints( values[ i%values.length ], 1, 3 );
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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

{{alias}}( str, start, end )
Slices a string based on Unicode code point indices.

Parameters
----------
str: string
Input string.
start: integer
The `ith` Unicode code point to start a slice (inclusive).
end: integer
The `jth` Unicode code point to end a slice (exclusive).

Returns
-------
out: string
Output string.

Examples
--------
> var out = {{alias}}( 'Hello 👋 World', 1, 3 )
'el'
> out = {{alias}}( '👋👋👋', 1, 2 )
'👋'
> out = {{alias}}( '六书/六書', 1, 14 )
'书/六書'

See Also
--------
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* @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 a string based on Unicode code point indices.
*
* @param str - input string
* @param start - the `ith` Unicode code point to start a slice (inclusive)
* @param end - the `jth` Unicode code point to end a slice (exclusive)
* @returns output string
*
* @example
* var out = sliceCodePoints( 'Hello 👋 World', 1, 3 );
* // returns 'el'
*
* @example
* var out = sliceCodePoints( '👋👋👋', 1, 2 );
* // returns '👋'
*/
declare function sliceCodePoints( str: string, start: number, end: number ): string;


// EXPORTS //

export = sliceCodePoints;
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 sliceCodePoints = require( './index' );


// TESTS //

// The function returns a string...
{
sliceCodePoints( 'abc', 1, 2 ); // $ExpectType string
}

// The compiler throws an error if the function is provided a value other than a string...
{
sliceCodePoints( true, 1, 2 ); // $ExpectError
sliceCodePoints( false, 1, 2 ); // $ExpectError
sliceCodePoints( null, 1, 2 ); // $ExpectError
sliceCodePoints( undefined, 1, 2 ); // $ExpectError
sliceCodePoints( 5, 1, 2 ); // $ExpectError
sliceCodePoints( [], 1, 2 ); // $ExpectError
sliceCodePoints( {}, 1, 2 ); // $ExpectError
sliceCodePoints( ( x: number ): number => x, 1, 2 ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument that is not a number...
{
sliceCodePoints( 'abc', true, 2 ); // $ExpectError
sliceCodePoints( 'abc', false, 2 ); // $ExpectError
sliceCodePoints( 'abc', null, 2 ); // $ExpectError
sliceCodePoints( 'abc', 'abc', 2 ); // $ExpectError
sliceCodePoints( 'abc', [], 2 ); // $ExpectError
sliceCodePoints( 'abc', {}, 2 ); // $ExpectError
sliceCodePoints( 'abc', ( x: number ): number => x, 2 ); // $ExpectError
}

// The compiler throws an error if the function is provided a third argument that is not a number...
{
sliceCodePoints( 'abc', 1, true ); // $ExpectError
sliceCodePoints( 'abc', 1, false ); // $ExpectError
sliceCodePoints( 'abc', 1, null ); // $ExpectError
sliceCodePoints( 'abc', 1, 'abc' ); // $ExpectError
sliceCodePoints( 'abc', 1, [] ); // $ExpectError
sliceCodePoints( 'abc', 1, {} ); // $ExpectError
sliceCodePoints( 'abc', 1, ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
sliceCodePoints(); // $ExpectError
sliceCodePoints( 'abc' ); // $ExpectError
sliceCodePoints( 'abc', 1, 2, {} ); // $ExpectError
}
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 sliceCodePoints = require( './../lib' );

console.log( sliceCodePoints( 'Hello 👋 World', 0, 7 ) );
// => 'Hello 👋'

console.log( sliceCodePoints( 'last man standing', 1, 17 ) );
// => 'ast man standing'

console.log( sliceCodePoints( '六书/六書', 1, 14 ) );
// => '书/六書'
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 a string based on Unicode code point indices.
*
* @module @stdlib/string/base/slice-code-points
*
* @example
* var sliceCodePoints = require( '@stdlib/string/base/slice-code-points' );
*
* var out = sliceCodePoints( 'Hello 👋 World', 1, 3 );
* // returns 'el'
*
* out = sliceCodePoints( '👋👋👋', 1, 2 );
* // returns '👋'
*/

// MODULES //

var main = require( './main.js' );


// EXPORTS //

module.exports = main;
Loading