0.1.1 β€’ Published 2 years ago

teinei-text-counter v0.1.1

Weekly downloads
-
License
MIT OR Apache-2.0
Repository
github
Last release
2 years ago

teinei-text-counter

teinei-text-counter is a minimal WebAssembly wrapper of the unicode-segmentation crate just for counting grapheme clusters. "丁寧"(teinei) means "careful" or "deliberate."

API

countGraphemeClusters(string)

Returns the length of a string with an extended grapheme cluster as a minimum unit.

import { countGraphemeClusters } from 'teinei-text-counter'

const text = '🌞HelloπŸ‘©β€πŸ‘©β€πŸ‘§'
console.log(text.length)
//-> πŸ™ 15
console.log(countGraphemeClusters(text))
//-> πŸ™‚ 7

splitIntoGraphemeClusters(string)

Splits a string into extended grapheme clusters and returns as an array.

import { splitIntoGraphemeClusters } from 'teinei-text-counter'

const text = 'π©Έ½γŠγ„γ—γ„πŸ˜‹'
console.log(text.split(''))
//-> πŸ™ ['\uD867', '\uDE3D', 'お', 'い', 'し', 'い', '\uD83D', '\uDE0B']
console.log(splitIntoGraphemeClusters(text))
//-> πŸ™‚ ['π©Έ½', 'お', 'い', 'し', 'い', 'πŸ˜‹']