Skip to content

graphemes ​

ts
function String.graphemes(target: string): Generator<string>

Returns a generator that yields individual grapheme clusters from target string.

A grapheme cluster represents a single user-perceived character, which may consist of multiple Unicode code points (e.g., emojis with modifiers, base characters with combining diacriticals).

Example ​

ts
import { String } from "@monstermann/string";

[...String.graphemes("hello")]; // ["h", "e", "l", "l", "o"]
[...String.graphemes("πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦")]; // ["πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦"]
[...String.graphemes("cafΓ©")]; // ["c", "a", "f", "Γ©"]
[...String.graphemes("πŸ‘‹πŸ½")]; // ["πŸ‘‹πŸ½"]

// Using in a loop
for (const grapheme of String.graphemes("helloπŸ‘‹")) {
    console.log(grapheme); // "h", "e", "l", "l", "o", "πŸ‘‹"
}
ts
import { String } from "@monstermann/string";

[...pipe("hello", String.graphemes())]; // ["h", "e", "l", "l", "o"]
[...pipe("πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦", String.graphemes())]; // ["πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦"]
[...pipe("cafΓ©", String.graphemes())]; // ["c", "a", "f", "Γ©"]
[...pipe("πŸ‘‹πŸ½", String.graphemes())]; // ["πŸ‘‹πŸ½"]

// Using in a loop
for (const grapheme of pipe("helloπŸ‘‹", String.graphemes())) {
    console.log(grapheme); // "h", "e", "l", "l", "o", "πŸ‘‹"
}