9 } from './CharacterUtils'
10 import type { WordCountInfo } from './WordCountTypes'
12 const segmenter = isWordCountSupported ? new Intl.Segmenter('en', { granularity: 'grapheme' }) : undefined
14 export const createWordCountInfo = (textContent: string): WordCountInfo => {
15 let characterCount = 0
17 let nonWhitespaceCharacterCount = 0
20 return { characterCount, wordCount, nonWhitespaceCharacterCount }
23 let alreadyMatched = false
24 for (const segmentData of segmenter.segment(textContent)) {
25 const char = segmentData.segment
26 const charCodePoint = toCodePoint(char)
28 const charIsWhitespace = isWhitespace(char)
30 if (!charIsWhitespace) {
31 nonWhitespaceCharacterCount++
40 const isMatched = !charIsWhitespace && !isWordDelimiter(charCodePoint)
41 const shouldIncrement = !alreadyMatched || isCJK(charCodePoint) || isKana(charCodePoint) || isThai(charCodePoint)
43 if (shouldIncrement && isMatched) {
46 } else if (!isMatched) {
47 alreadyMatched = false
53 nonWhitespaceCharacterCount,