Skip to content

Commit 955bcbe

Browse files
feat: add string/num-code-points
PR-URL: #5456 Ref: #1062 Ref: #1097 Co-authored-by: Stephannie Jiménez Gacha <steff456@hotmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Muhammad Haris <harriskhan047@outlook.com>
1 parent 38b39db commit 955bcbe

File tree

15 files changed

+1091
-0
lines changed

15 files changed

+1091
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# numCodePoints
22+
23+
> Return the number of Unicode code points in a string.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var numCodePoints = require( '@stdlib/string/num-code-points' );
31+
```
32+
33+
#### numCodePoints( str )
34+
35+
Returns the number of code points in a string.
36+
37+
```javascript
38+
var out = numCodePoints( 'last man standing' );
39+
// returns 17
40+
41+
out = numCodePoints( 'Hidden Treasures' );
42+
// returns 16
43+
44+
out = numCodePoints( '👋👋👋' );
45+
// returns 3
46+
```
47+
48+
</section>
49+
50+
<!-- /.usage -->
51+
52+
<section class="examples">
53+
54+
## Examples
55+
56+
<!-- eslint no-undef: "error" -->
57+
58+
```javascript
59+
var numCodePoints = require( '@stdlib/string/num-code-points' );
60+
61+
console.log( numCodePoints( 'last man standing' ) );
62+
// => 17
63+
64+
console.log( numCodePoints( '六书/六書' ) );
65+
// => 5
66+
67+
console.log( numCodePoints( '🐶🐮🐷🐰🐸' ) );
68+
// => 5
69+
70+
console.log( numCodePoints( 'Hello 👋 World' ) );
71+
// => 13
72+
```
73+
74+
</section>
75+
76+
<!-- /.examples -->
77+
78+
* * *
79+
80+
<section class="cli">
81+
82+
## CLI
83+
84+
<section class="usage">
85+
86+
### Usage
87+
88+
```text
89+
Usage: num-code-points [options] [<string>]
90+
91+
Options:
92+
93+
-h, --help Print this message.
94+
-V, --version Print the package version.
95+
-l, --lines Analyze individual lines.
96+
```
97+
98+
</section>
99+
100+
<!-- /.usage -->
101+
102+
<section class="examples">
103+
104+
### Examples
105+
106+
```bash
107+
$ num-code-points beep
108+
4
109+
```
110+
111+
To use as a [standard stream][standard-streams],
112+
113+
```bash
114+
$ echo -n 'beep\nboop書' | num-code-points
115+
10
116+
```
117+
118+
```bash
119+
$ echo -n 'beep\nboop書' | num-code-points -l
120+
4
121+
5
122+
```
123+
124+
</section>
125+
126+
<!-- /.examples -->
127+
128+
</section>
129+
130+
<!-- /.cli -->
131+
132+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
133+
134+
<section class="related">
135+
136+
</section>
137+
138+
<!-- /.related -->
139+
140+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
141+
142+
<section class="links">
143+
144+
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
145+
146+
<!-- <related-links> -->
147+
148+
<!-- </related-links> -->
149+
150+
</section>
151+
152+
<!-- /.links -->
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isInteger = require( '@stdlib/assert/is-integer' );
25+
var pkg = require( './../package.json' ).name;
26+
var numCodePoints = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var strings;
33+
var out;
34+
var i;
35+
36+
strings = [
37+
'last man standing',
38+
'presidential election',
39+
'अनुच्छेद',
40+
'🌷',
41+
'书/六書',
42+
'เ❄︎நி',
43+
'กิิก้้ก็็ก็็กิิก้้ก็็กิิก้้กิิก้้ก็็ก็็กิิก้้ก็็กิิก้้',
44+
'書六/书六',
45+
'ܶƔλʃݖͱšɕ҆ʧѸؐҜҦɳΏ',
46+
'âݝΝ‚ҳӌݾҀƳ۵ۧ޳ǁǸΓ'
47+
];
48+
49+
b.tic();
50+
for ( i = 0; i < b.iterations; i++ ) {
51+
out = numCodePoints( strings[ i%strings.length ] );
52+
if ( out < 0 ) {
53+
b.fail( 'should never be a negative integer' );
54+
}
55+
}
56+
b.toc();
57+
if ( !isInteger( out ) ) {
58+
b.fail( 'should return an integer' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* @license Apache-2.0
5+
*
6+
* Copyright (c) 2025 The Stdlib Authors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var resolve = require( 'path' ).resolve;
26+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
27+
var CLI = require( '@stdlib/cli/ctor' );
28+
var stdin = require( '@stdlib/process/read-stdin' );
29+
var stdinStream = require( '@stdlib/streams/node/stdin' );
30+
var RE_EOL = require( '@stdlib/regexp/eol' ).REGEXP;
31+
var numCodePoints = require( './../lib' );
32+
33+
34+
// MAIN //
35+
36+
/**
37+
* Main execution sequence.
38+
*
39+
* @private
40+
* @returns {void}
41+
*/
42+
function main() {
43+
var flags;
44+
var lines;
45+
var args;
46+
var cli;
47+
var i;
48+
49+
// Create a command-line interface:
50+
cli = new CLI({
51+
'pkg': require( './../package.json' ),
52+
'options': require( './../etc/cli_opts.json' ),
53+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
54+
'encoding': 'utf8'
55+
})
56+
});
57+
58+
// Get any provided command-line options:
59+
flags = cli.flags();
60+
if ( flags.help || flags.version ) {
61+
return;
62+
}
63+
64+
// Get any provided command-line arguments:
65+
args = cli.args();
66+
67+
// Check if we are receiving data from `stdin`...
68+
if ( !stdinStream.isTTY ) {
69+
return stdin( onRead );
70+
}
71+
if ( flags.lines ) {
72+
lines = args[ 0 ].split( RE_EOL );
73+
for ( i = 0; i < lines.length; i++ ) {
74+
console.log( numCodePoints( lines[ i ] ) ); // eslint-disable-line no-console
75+
}
76+
} else {
77+
console.log( numCodePoints( args[ 0 ] ) ); // eslint-disable-line no-console
78+
}
79+
80+
/**
81+
* Callback invoked upon reading from `stdin`.
82+
*
83+
* @private
84+
* @param {(Error|null)} error - error object
85+
* @param {Buffer} data - data
86+
* @returns {void}
87+
*/
88+
function onRead( error, data ) {
89+
var lines;
90+
var i;
91+
if ( error ) {
92+
return cli.error( error );
93+
}
94+
data = data.toString();
95+
if ( flags.lines ) {
96+
lines = data.split( RE_EOL );
97+
for ( i = 0; i < lines.length; i++ ) {
98+
console.log( numCodePoints( lines[ i ] ) ); // eslint-disable-line no-console
99+
}
100+
} else {
101+
console.log( numCodePoints( data ) ); // eslint-disable-line no-console
102+
}
103+
}
104+
}
105+
106+
main();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
{{alias}}( str )
3+
Returns the number of code points in a string.
4+
5+
Parameters
6+
----------
7+
str: string
8+
Input string.
9+
10+
Returns
11+
-------
12+
out: string
13+
Number of code points.
14+
15+
Examples
16+
--------
17+
> var out = {{alias}}( 'beep' )
18+
4
19+
> out = {{alias}}( '六' )
20+
1
21+
22+
See Also
23+
--------
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
/**
22+
* Returns the number of code points in a string.
23+
*
24+
* @param str - input string
25+
* @returns number of code points
26+
*
27+
* @example
28+
* var out = numCodePoints( 'last man standing' );
29+
* // returns 17
30+
*
31+
* @example
32+
* var out = numCodePoints( 'presidential election' );
33+
* // returns 21
34+
*
35+
* @example
36+
* var out = numCodePoints( '六' );
37+
* // returns 1
38+
*
39+
* @example
40+
* var out = numCodePoints( 'अनुच्छेद' );
41+
* // returns 5
42+
*/
43+
declare function numCodePoints( str: string ): number;
44+
45+
46+
// EXPORTS //
47+
48+
export = numCodePoints;

0 commit comments

Comments
 (0)