-
-
Notifications
You must be signed in to change notification settings - Fork 828
feat: add next-code-point-index string package #1117
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 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1b9d276
feat: add next-code-point string package
steff456 1e998d8
Apply suggestions from code review
kgryte f13f018
docs: update example in repl
steff456 3ee3c46
Apply suggestions from code review
kgryte 40c63c5
Apply suggestions from code review
kgryte 0acb4ba
Apply suggestions from code review
kgryte cf60617
Apply suggestions from code review
kgryte 6e419e3
fix: add case for unpaired high surrogate with tests
steff456 1c06df1
Apply suggestions from code review
kgryte 0f08e63
refactor: update implementation to avoid for loop
kgryte ddd8b1c
Apply suggestions from code review
kgryte 392881b
fix: address bug when surrogate pair comprises the entire string
kgryte 7857375
fix: update examples, fix tests and bug when a paired surrogate is at…
steff456 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
193 changes: 193 additions & 0 deletions
193
lib/node_modules/@stdlib/string/next-code-point-index/README.md
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,193 @@ | ||
<!-- | ||
|
||
@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. | ||
|
||
--> | ||
|
||
# nextCodePointIndex | ||
|
||
> Return the position of the next Unicode code point in a string after a specified position. | ||
|
||
<!-- 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 nextCodePointIndex = require( '@stdlib/string/next-code-point-index' ); | ||
``` | ||
|
||
#### nextCodePointIndex( string\[, fromIndex] ) | ||
|
||
Returns the position of the next Unicode code point in a string after a specified position. | ||
|
||
```javascript | ||
var out = nextCodePointIndex( 'last man standing' ); | ||
// returns 1 | ||
``` | ||
|
||
By default, the function searches for a Unicode code point starting from the first index. To specify an alternative starting search index, provide a `fromIndex` argument. | ||
|
||
```javascript | ||
var out = nextCodePointIndex( '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 a 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 nextCodePointIndex = require( '@stdlib/string/next-code-point-index' ); | ||
|
||
var out = nextCodePointIndex( 'last man standing', 4 ); | ||
// returns 5 | ||
|
||
out = nextCodePointIndex( 'presidential election', 8 ); | ||
// returns 9 | ||
|
||
out = nextCodePointIndex( '𐒻𐓟𐒻𐓟', 1 ); | ||
// returns 3 | ||
|
||
out = nextCodePointIndex( '🌷', 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-index [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-index --from=1 𐒻𐓟𐒻𐓟 | ||
3 | ||
``` | ||
|
||
To use as a [standard stream][standard-streams], | ||
|
||
```bash | ||
$ echo -n '𐒻𐓟𐒻𐓟' | next-code-point-index --from=1 | ||
3 | ||
steff456 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
</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"> | ||
|
||
</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 | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
64 changes: 64 additions & 0 deletions
64
lib/node_modules/@stdlib/string/next-code-point-index/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,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 nextCodePointIndex = 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 = nextCodePointIndex( 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
91
lib/node_modules/@stdlib/string/next-code-point-index/bin/cli
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,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 nextCodePointIndex = 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( nextCodePointIndex( 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( nextCodePointIndex( data.toString(), pos ) ); // eslint-disable-line no-console | ||
} | ||
} | ||
|
||
main(); |
31 changes: 31 additions & 0 deletions
31
lib/node_modules/@stdlib/string/next-code-point-index/docs/repl.txt
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[, fromIndex] ) | ||
Returns the position of the next Unicode 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 | ||
-------- |
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.