Skip to content
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

feat: add next-code-point-index string package #1117

Merged
merged 13 commits into from
Nov 10, 2023
Merged
205 changes: 205 additions & 0 deletions lib/node_modules/@stdlib/string/next-code-point/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<!--

@license Apache-2.0

Copyright (c) 2023 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.

-->

# nextCodePoint

> Return the next code point in a string after a specified position.
kgryte marked this conversation as resolved.
Show resolved Hide resolved
steff456 marked this conversation as resolved.
Show resolved Hide resolved

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var nextCodePoint = require( '@stdlib/string/next-code-point' );
```

#### nextCodePoint( string\[, fromIndex] )

Returns the next code point in a string after a specified position.
kgryte marked this conversation as resolved.
Show resolved Hide resolved

```javascript
var out = nextCodePoint( 'last man standing' );
// returns 1
```

By default, the function searches for a grapheme cluster starting from the first index. To specify an alternative starting search index, provide a `fromIndex` argument.
kgryte marked this conversation as resolved.
Show resolved Hide resolved

```javascript
var out = nextCodePoint( 'last man standing', 4 );
// returns 5
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- If `string` is an empty string, the function returns `-1` irrespective of `fromIndex`.
- If an code point does not exist after `fromIndex`, the function returns `-1`.
- Note that `fromIndex` does **not** refer to a visual character position, but to an index in the ordered sequence of [UTF-16][utf-16] code units.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var nextCodePoint = require( '@stdlib/string/next-code-point' );

var out = nextCodePoint( 'last man standing', 4 );
// returns 5

out = nextCodePoint( 'presidential election', 8 );
// returns 9

out = nextCodePoint( 'अनुच्छेद', 1 );
// returns 2

out = nextCodePoint( '🌷', 0 );
// returns -1
```

</section>

<!-- /.examples -->

<!-- Section for describing a command-line interface. -->

* * *

<section class="cli">

## CLI

<!-- CLI usage documentation. -->

<section class="usage">

### Usage

```text
Usage: next-code-point [options] [<string>]

Options:

-h, --help Print this message.
-V, --version Print the package version.
--from index Starting search position in string. Default: 0.
```

</section>

<!-- /.usage -->

<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- CLI usage examples. -->

<section class="examples">

### Examples

```bash
$ next-code-point --from=1 अनुच्छेद
3
```

To use as a [standard stream][standard-streams],

```bash
$ echo -n 'अनुच्छेद' | next-code-point --from=1
3
```

</section>

<!-- /.examples -->

</section>

<!-- /.cli -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

* * *

## See Also

- <span class="package-name">[`@stdlib/string/num-grapheme-clusters`][@stdlib/string/num-grapheme-clusters]</span><span class="delimiter">: </span><span class="description">return the number of grapheme clusters in a string.</span>

</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">

[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams

[utf-16]: https://en.wikipedia.org/wiki/UTF-16

<!-- <related-links> -->

[@stdlib/string/num-grapheme-clusters]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/string/num-grapheme-clusters

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var nextCodePoint = require( './../lib' );


// MAIN //

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

strings = [
'last man standing',
'presidential election',
'अनुच्छेद',
'🌷',
'书/六書',
'เ❄︎நி',
'กิิก้้ก็็ก็็กิิก้้ก็็กิิก้้กิิก้้ก็็ก็็กิิก้้ก็็กิิก้้',
'書六/书六',
'ܶƔλʃݖͱšɕ҆ʧѸؐҜҦɳΏ',
'âݝΝ‚ҳӌݾҀƳ۵ۧ޳ǁǸΓ'
];
len = strings.length;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = nextCodePoint( strings[ i%len ], 1 );
if ( out !== out ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( !isInteger( out ) ) {
b.fail( 'should return an integer' );
}
b.pass( 'benchmark finished' );
b.end();
});
91 changes: 91 additions & 0 deletions lib/node_modules/@stdlib/string/next-code-point/bin/cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env node

/**
* @license Apache-2.0
*
* Copyright (c) 2023 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 resolve = require( 'path' ).resolve;
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
var CLI = require( '@stdlib/cli/ctor' );
var stdin = require( '@stdlib/process/read-stdin' );
var stdinStream = require( '@stdlib/streams/node/stdin' );
var nextCodePoint = require( './../lib' );


// MAIN //

/**
* Main execution sequence.
*
* @private
* @returns {void}
*/
function main() {
var flags;
var args;
var cli;
var pos;

// Create a command-line interface:
cli = new CLI({
'pkg': require( './../package.json' ),
'options': require( './../etc/cli_opts.json' ),
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
'encoding': 'utf8'
})
});

// Get any provided command-line options:
flags = cli.flags();
if ( flags.help || flags.version ) {
return;
}
if ( flags.from ) {
pos = parseInt( flags.from, 10 );
} else {
pos = 0;
}
// Get any provided command-line arguments:
args = cli.args();

// Check if we are receiving data from `stdin`...
if ( stdinStream.isTTY ) {
return console.log( nextCodePoint( args[ 0 ], pos ) ); // eslint-disable-line no-console
}
return stdin( onRead );

/**
* Callback invoked upon reading from `stdin`.
*
* @private
* @param {(Error|null)} error - error object
* @param {Buffer} data - data
* @returns {void}
*/
function onRead( error, data ) {
if ( error ) {
return cli.error( error );
}
console.log( nextCodePoint( data.toString(), pos ) ); // eslint-disable-line no-console
}
}

main();
30 changes: 30 additions & 0 deletions lib/node_modules/@stdlib/string/next-code-point/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

{{alias}}( str[, fromIndex] )
Returns the next code point in a string after a specified position.

Parameters
----------
str: string
Input string.

fromIndex: integer (optional)
Position. Default: 0.

Returns
-------
out: integer
Next code point position.

Examples
--------
> var out = {{alias}}( 'last man standing', 4 )
5
> out = {{alias}}( 'presidential election', 8 )
9
> out = {{alias}}( 'अनुच्छेद', 1 )
3
> out = {{alias}}( '🌷' )
-1

See Also
--------
Loading
Loading