🏛️grapheme-splitter

libraries ⟩ grapheme-splitter

  • install

npm install grapheme-splitter
  • import

import GraphemeSplitter from 'grapheme-splitter';       // ES module
const GraphemeSplitter = require('grapheme-splitter');  // CommonJS
  • test

const splitter = new GraphemeSplitter();
const string = "🌷🎁💩😜👍🏳️‍🌈";

// string -> iterable iterator of grapheme clusters
const graphemesIt = splitter.iterateGraphemes(string);
[...graphemesIt],    // [ '🌷', '🎁', '💩', '😜', '👍', '🏳️‍🌈' ]
[...graphemesIt],    // []     (exhausted iterator)

splitter.splitGraphemes(string),        // array of grapheme clusters
// [ '🌷', '🎁', '💩', '😜', '👍', '🏳️‍🌈' ]

splitter.countGraphemes(string),        // 6    (count grapheme clusters)

Last updated