-
-
Notifications
You must be signed in to change notification settings - Fork 837
feat: add string/num-code-points
#5456
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
4 commits
Select commit
Hold shift + click to select a range
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
152 changes: 152 additions & 0 deletions
152
lib/node_modules/@stdlib/string/num-code-points/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,152 @@ | ||
<!-- | ||
|
||
@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. | ||
|
||
--> | ||
|
||
# numCodePoints | ||
|
||
> Return the number of Unicode code points in a string. | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var numCodePoints = require( '@stdlib/string/num-code-points' ); | ||
``` | ||
|
||
#### numCodePoints( str ) | ||
|
||
Returns the number of code points in a string. | ||
|
||
```javascript | ||
var out = numCodePoints( 'last man standing' ); | ||
// returns 17 | ||
|
||
out = numCodePoints( 'Hidden Treasures' ); | ||
// returns 16 | ||
|
||
out = numCodePoints( '👋👋👋' ); | ||
// returns 3 | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var numCodePoints = require( '@stdlib/string/num-code-points' ); | ||
|
||
console.log( numCodePoints( 'last man standing' ) ); | ||
// => 17 | ||
|
||
console.log( numCodePoints( '六书/六書' ) ); | ||
// => 5 | ||
|
||
console.log( numCodePoints( '🐶🐮🐷🐰🐸' ) ); | ||
// => 5 | ||
|
||
console.log( numCodePoints( 'Hello 👋 World' ) ); | ||
// => 13 | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
* * * | ||
|
||
<section class="cli"> | ||
|
||
## CLI | ||
|
||
<section class="usage"> | ||
|
||
### Usage | ||
|
||
```text | ||
Usage: num-code-points [options] [<string>] | ||
|
||
Options: | ||
|
||
-h, --help Print this message. | ||
-V, --version Print the package version. | ||
-l, --lines Analyze individual lines. | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="examples"> | ||
|
||
### Examples | ||
|
||
```bash | ||
$ num-code-points beep | ||
4 | ||
``` | ||
|
||
To use as a [standard stream][standard-streams], | ||
|
||
```bash | ||
$ echo -n 'beep\nboop書' | num-code-points | ||
10 | ||
``` | ||
|
||
```bash | ||
$ echo -n 'beep\nboop書' | num-code-points -l | ||
4 | ||
5 | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
</section> | ||
|
||
<!-- /.cli --> | ||
|
||
<!-- 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 | ||
|
||
<!-- <related-links> --> | ||
|
||
<!-- </related-links> --> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
62 changes: 62 additions & 0 deletions
62
lib/node_modules/@stdlib/string/num-code-points/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,62 @@ | ||
/** | ||
* @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 isInteger = require( '@stdlib/assert/is-integer' ); | ||
var pkg = require( './../package.json' ).name; | ||
var numCodePoints = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var strings; | ||
var out; | ||
var i; | ||
|
||
strings = [ | ||
'last man standing', | ||
'presidential election', | ||
'अनुच्छेद', | ||
'🌷', | ||
'书/六書', | ||
'เ❄︎நி', | ||
'กิิก้้ก็็ก็็กิิก้้ก็็กิิก้้กิิก้้ก็็ก็็กิิก้้ก็็กิิก้้', | ||
'書六/书六', | ||
'ܶƔλʃݖͱšɕ҆ʧѸؐҜҦɳΏ', | ||
'âݝΝҳӌݾҀƳ۵ۧǁǸΓ' | ||
]; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
out = numCodePoints( strings[ i%strings.length ] ); | ||
if ( out < 0 ) { | ||
b.fail( 'should never be a negative integer' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isInteger( out ) ) { | ||
b.fail( 'should return an integer' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
106 changes: 106 additions & 0 deletions
106
lib/node_modules/@stdlib/string/num-code-points/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,106 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* @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 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 RE_EOL = require( '@stdlib/regexp/eol' ).REGEXP; | ||
var numCodePoints = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Main execution sequence. | ||
* | ||
* @private | ||
* @returns {void} | ||
*/ | ||
function main() { | ||
var flags; | ||
var lines; | ||
var args; | ||
var cli; | ||
var i; | ||
|
||
// 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; | ||
} | ||
|
||
// Get any provided command-line arguments: | ||
args = cli.args(); | ||
|
||
// Check if we are receiving data from `stdin`... | ||
if ( !stdinStream.isTTY ) { | ||
return stdin( onRead ); | ||
} | ||
if ( flags.lines ) { | ||
lines = args[ 0 ].split( RE_EOL ); | ||
for ( i = 0; i < lines.length; i++ ) { | ||
console.log( numCodePoints( lines[ i ] ) ); // eslint-disable-line no-console | ||
} | ||
} else { | ||
console.log( numCodePoints( args[ 0 ] ) ); // eslint-disable-line no-console | ||
} | ||
|
||
/** | ||
* Callback invoked upon reading from `stdin`. | ||
* | ||
* @private | ||
* @param {(Error|null)} error - error object | ||
* @param {Buffer} data - data | ||
* @returns {void} | ||
*/ | ||
function onRead( error, data ) { | ||
var lines; | ||
var i; | ||
if ( error ) { | ||
return cli.error( error ); | ||
} | ||
data = data.toString(); | ||
if ( flags.lines ) { | ||
lines = data.split( RE_EOL ); | ||
for ( i = 0; i < lines.length; i++ ) { | ||
console.log( numCodePoints( lines[ i ] ) ); // eslint-disable-line no-console | ||
} | ||
} else { | ||
console.log( numCodePoints( data ) ); // eslint-disable-line no-console | ||
} | ||
} | ||
} | ||
|
||
main(); |
23 changes: 23 additions & 0 deletions
23
lib/node_modules/@stdlib/string/num-code-points/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,23 @@ | ||
|
||
{{alias}}( str ) | ||
Returns the number of code points in a string. | ||
|
||
Parameters | ||
---------- | ||
str: string | ||
Input string. | ||
|
||
Returns | ||
------- | ||
out: string | ||
Number of code points. | ||
|
||
Examples | ||
-------- | ||
> var out = {{alias}}( 'beep' ) | ||
4 | ||
> out = {{alias}}( '六' ) | ||
1 | ||
|
||
See Also | ||
-------- |
48 changes: 48 additions & 0 deletions
48
lib/node_modules/@stdlib/string/num-code-points/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,48 @@ | ||
/* | ||
* @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: 2.0 | ||
|
||
/** | ||
* Returns the number of code points in a string. | ||
* | ||
* @param str - input string | ||
* @returns number of code points | ||
* | ||
* @example | ||
* var out = numCodePoints( 'last man standing' ); | ||
* // returns 17 | ||
* | ||
* @example | ||
* var out = numCodePoints( 'presidential election' ); | ||
* // returns 21 | ||
* | ||
* @example | ||
* var out = numCodePoints( '六' ); | ||
* // returns 1 | ||
* | ||
* @example | ||
* var out = numCodePoints( 'अनुच्छेद' ); | ||
* // returns 5 | ||
*/ | ||
declare function numCodePoints( str: string ): number; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = numCodePoints; |
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.