Invokes a function for each grapheme cluster (i.e., user-perceived character) in a string.
var forEachGraphemeCluster = require( '@stdlib/string/base/for-each-grapheme-cluster' );
Invokes a function for each grapheme cluster (i.e., user-perceived character) in a string.
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
.
function clbk() {
this.count += 1;
}
var str = '👉🏿';
var ctx = {
'count': 0
};
forEachGraphemeCluster( str, clbk, ctx );
var cnt = ctx.count;
// returns 1
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 );
@stdlib/string/base/for-each
: invoke a function for each UTF-16 code unit in a string.@stdlib/string/base/for-each-code-point
: invoke a function for each Unicode code point in a string.@stdlib/string/for-each
: invoke a function for each character in a string.