0.2.1 • Published 2 months ago

@stdlib/string-base-for-each-code-point v0.2.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 months ago

forEachCodePoint

NPM version Build Status Coverage Status

Invokes a function for each Unicode code point in a string.

Installation

npm install @stdlib/string-base-for-each-code-point

Usage

var forEachCodePoint = require( '@stdlib/string-base-for-each-code-point' );

forEachCodePoint( str, clbk[, thisArg ] )

Invokes a function for each Unicode code point in a string.

function log( value, index ) {
    console.log( '%d: %s', index, value );
}

forEachCodePoint( 'Beep!', log );
/* =>
    0: B
    1: e
    2: e
    3: p
    4: !
*/

The invoked function is provided three arguments:

  • value: Unicode code point.
  • index: starting Unicode code point index.
  • str: input string.

To set the function execution context, provide a thisArg.

function clbk() {
    this.count += 1;
}

var str = '👉🏿';

var ctx = {
    'count': 0
};

forEachCodePoint( str, clbk, ctx );

var cnt = ctx.count;
// returns 2

Examples

var forEachCodePoint = require( '@stdlib/string-base-for-each-code-point' );

function log( value, index ) {
    console.log( '%d: %s', index, value );
}

forEachCodePoint( 'presidential election', log );
forEachCodePoint( 'Iñtërnâtiônàlizætiøn', log );
forEachCodePoint( '🌷🍕', log );
forEachCodePoint( '\uD834\uDD1E', log );

See Also


Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.