@stdlib/string
Version:
String manipulation functions.
228 lines (143 loc) • 5.76 kB
Markdown
<!--
@license Apache-2.0
Copyright (c) 2022 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.
-->
# graphemeClusters2iterator
> Create an iterator which iterates over [grapheme clusters][unicode-text-segmentation].
<!-- 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 graphemeClusters2iterator = require( '@stdlib/string/to-grapheme-cluster-iterator' );
```
#### graphemeClusters2iterator( src\[, mapFcn\[, thisArg]] )
Returns an iterator which iterates over each [grapheme cluster][unicode-text-segmentation] in a `string`.
```javascript
var iter = graphemeClusters2iterator( '🌷🍕' );
var v = iter.next().value;
// returns '🌷'
v = iter.next().value;
// returns '🍕'
var bool = iter.next().done;
// returns true
```
The returned iterator protocol-compliant object has the following properties:
- **next**: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the iterator is finished.
- **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.
To invoke a function for each `src` value, provide a callback function.
```javascript
function fcn( v ) {
return v + v;
}
var it = graphemeClusters2iterator( 'beep', fcn );
// returns <Object>
var v = it.next().value;
// returns 'bb'
v = it.next().value;
// returns 'ee'
v = it.next().value;
// returns 'ee'
// ...
```
The invoked function is provided three arguments:
- **value**: [grapheme cluster][unicode-text-segmentation].
- **index**: iterated value index.
- **src**: source string.
```javascript
function fcn( v, i ) {
return v + i;
}
var it = graphemeClusters2iterator( 'bar', fcn );
// returns <Object>
var v = it.next().value;
// returns 'b0'
v = it.next().value;
// returns 'a1'
v = it.next().value;
// returns 'r2'
// ...
```
To set the callback function execution context, provide a `thisArg`.
```javascript
function fcn( v ) {
this.count += 1;
return v;
}
var ctx = {
'count': 0
};
var it = graphemeClusters2iterator( '🌷🍕', fcn, ctx );
// returns <Object>
var v = it.next().value;
// returns '🌷'
v = it.next().value;
// returns '🍕'
var count = ctx.count;
// returns 2
```
</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">
## Notes
- If an environment supports `Symbol.iterator`, the returned iterator is iterable.
- In environments supporting `Symbol.iterator`, the function **explicitly** does **not** invoke a string's `@@iterator` method, regardless of whether this method is defined. To convert a string to an implementation defined iterator, invoke this method directly.
</section>
<!-- /.notes -->
<!-- Package usage examples. -->
<section class="examples">
## Examples
<!-- eslint no-undef: "error" -->
```javascript
var graphemeClusters2iterator = require( '@stdlib/string/to-grapheme-cluster-iterator' );
function repeat( str ) {
return str + str;
}
// Create an iterator which iterates over grapheme clusters:
var it = graphemeClusters2iterator( 'Iñtërnâtiônàlizætiøn', repeat );
// Perform manual iteration...
var v;
while ( true ) {
v = it.next();
if ( v.done ) {
break;
}
console.log( v.value );
}
```
</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">
* * *
## See Also
- <span class="package-name">[`@stdlib/array/to-iterator`][@stdlib/array/to-iterator]</span><span class="delimiter">: </span><span class="description">create an iterator from an array-like object.</span>
- <span class="package-name">[`@stdlib/string/to-grapheme-cluster-iterator-right`][@stdlib/string/to-grapheme-cluster-iterator-right]</span><span class="delimiter">: </span><span class="description">create an iterator which iterates from right to left over grapheme clusters.</span>
</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">
[unicode-text-segmentation]: http://www.unicode.org/reports/tr29/
<!-- <related-links> -->
[@stdlib/array/to-iterator]: https://www.npmjs.com/package/@stdlib/array-to-iterator
[@stdlib/string/to-grapheme-cluster-iterator-right]: https://github.com/stdlib-js/string/tree/main/to-grapheme-cluster-iterator-right
<!-- </related-links> -->
</section>
<!-- /.links -->