-
-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
string/base/for-each-grapheme-cluster
- Loading branch information
Showing
10 changed files
with
793 additions
and
0 deletions.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
lib/node_modules/@stdlib/string/base/for-each-grapheme-cluster/README.md
This file contains 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,146 @@ | ||
<!-- | ||
@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. | ||
--> | ||
|
||
# forEachGraphemeCluster | ||
|
||
> Invokes a function for each grapheme cluster (i.e., user-perceived character) in a string. | ||
<!-- 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 forEachGraphemeCluster = require( '@stdlib/string/base/for-each-grapheme-cluster' ); | ||
``` | ||
|
||
#### forEachGraphemeCluster( str, clbk\[, thisArg ] ) | ||
|
||
Invokes a function for each grapheme cluster (i.e., user-perceived character) in a string. | ||
|
||
```javascript | ||
function log( value, index ) { | ||
console.log( '%d: %s', index, value ); | ||
} | ||
|
||
forEachGraphemeCluster( 'Beep!', log ); | ||
/* => | ||
0: B | ||
1: e | ||
2: e | ||
3: p | ||
4: ! | ||
*/ | ||
``` | ||
|
||
The invoked function is provided three arguments: | ||
|
||
- **value**: grapheme cluster. | ||
- **index**: starting grapheme cluster index. | ||
- **str**: input string. | ||
|
||
To set the function execution context, provide a `thisArg`. | ||
|
||
```javascript | ||
function clbk() { | ||
this.count += 1; | ||
} | ||
|
||
var str = 'Hello, world!'; | ||
|
||
var ctx = { | ||
'count': 0 | ||
}; | ||
|
||
forEachGraphemeCluster( str, clbk, ctx ); | ||
|
||
var bool = ( str.length === ctx.count ); | ||
// returns true | ||
``` | ||
|
||
</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"> | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<!-- Package usage examples. --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var forEachGraphemeCluster = require( '@stdlib/string/base/for-each-grapheme-cluster' ); | ||
|
||
function log( value, index ) { | ||
console.log( '%d: %s', index, value ); | ||
} | ||
|
||
forEachGraphemeCluster( 'presidential election', log ); | ||
forEachGraphemeCluster( 'Iñtërnâtiônàlizætiøn', log ); | ||
forEachGraphemeCluster( '🌷🍕', log ); | ||
forEachGraphemeCluster( '\uD834\uDD1E', log ); | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- 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"> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
61 changes: 61 additions & 0 deletions
61
lib/node_modules/@stdlib/string/base/for-each-grapheme-cluster/benchmark/benchmark.js
This file contains 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,61 @@ | ||
/** | ||
* @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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; | ||
var pkg = require( './../package.json' ).name; | ||
var forEach = 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', | ||
'🐶🐮🐷🐰🐸' | ||
]; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
out = forEach( values[ i%values.length ], clbk ); | ||
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(); | ||
|
||
function clbk( v ) { | ||
if ( typeof v !== 'string' ) { | ||
b.fail( 'unexpected value' ); | ||
} | ||
} | ||
}); |
38 changes: 38 additions & 0 deletions
38
lib/node_modules/@stdlib/string/base/for-each-grapheme-cluster/docs/repl.txt
This file contains 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,38 @@ | ||
|
||
{{alias}}( str, clbk[, thisArg] ) | ||
Invokes a function for each grapheme cluster (i.e., user-perceived | ||
character) in a string. | ||
|
||
When invoked, the provided function is provided three arguments: | ||
|
||
- value: grapheme cluster | ||
- index: starting grapheme cluster index | ||
- str: input string | ||
|
||
Parameters | ||
---------- | ||
str: string | ||
Input string over which to iterate. | ||
|
||
clbk: Function | ||
The function to invoke for each grapheme cluster in the input string. | ||
|
||
thisArg: any (optional) | ||
Execution context. | ||
|
||
Returns | ||
------- | ||
out: string | ||
Input string. | ||
|
||
Examples | ||
-------- | ||
> var n = 0; | ||
> function fcn() { n += 1; }; | ||
> {{alias}}( 'hello world!', fcn ); | ||
> n | ||
12 | ||
|
||
See Also | ||
-------- | ||
|
93 changes: 93 additions & 0 deletions
93
lib/node_modules/@stdlib/string/base/for-each-grapheme-cluster/docs/types/index.d.ts
This file contains 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,93 @@ | ||
/* | ||
* @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. | ||
*/ | ||
|
||
// TypeScript Version: 2.0 | ||
|
||
/** | ||
* Callback invoked for each grapheme cluster (i.e., user-perceived character) in a string. | ||
* | ||
* @returns result | ||
*/ | ||
type Nullary = () => any; | ||
|
||
/** | ||
* Callback invoked for each grapheme cluster (i.e., user-perceived character) in a string. | ||
* | ||
* @param value - grapheme cluster | ||
* @returns result | ||
*/ | ||
type Unary = ( value: string ) => any; | ||
|
||
/** | ||
* Callback invoked for each grapheme cluster (i.e., user-perceived character) in a string. | ||
* | ||
* @param value - grapheme cluster | ||
* @param index - string grapheme cluster index | ||
* @returns result | ||
*/ | ||
type Binary = ( value: string, index: number ) => any; | ||
|
||
/** | ||
* Callback invoked for each grapheme cluster (i.e., user-perceived character) in a string. | ||
* | ||
* @param value - grapheme cluster | ||
* @param index - string grapheme cluster index | ||
* @param str - input string | ||
* @returns result | ||
*/ | ||
type Ternary = ( value: string, index: number, str: string ) => any; | ||
|
||
/** | ||
* Callback invoked for each grapheme cluster (i.e., user-perceived character) in a string. | ||
* | ||
* @param value - grapheme cluster | ||
* @param index - starting grapheme cluster index | ||
* @param str - input string | ||
* @returns result | ||
*/ | ||
type Callback = Nullary | Unary | Binary | Ternary; | ||
|
||
/** | ||
* Invokes a function for each grapheme cluster (i.e., user-perceived character) in a string. | ||
* | ||
* ## Notes | ||
* | ||
* - When invoked, the provided function is provided three arguments: | ||
* | ||
* - **value**: grapheme cluster. | ||
* - **index**: starting grapheme cluster index. | ||
* - **str**: input string. | ||
* | ||
* @param str - input string | ||
* @param clbk - function to invoke | ||
* @param thisArg - execution context | ||
* @returns input string | ||
* | ||
* @example | ||
* function log( value, index ) { | ||
* console.log( '%d: %s', index, value ); | ||
* } | ||
* | ||
* forEach( 'Hello, World!', log ); | ||
*/ | ||
declare function forEach( str: string, clbk: Callback, thisArg?: any ): string; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = forEach; |
Oops, something went wrong.