Skip to content

Back to Snippets

wordsUnique()

For the given string, return the set of unique words.

/*
For the given `text` in the language `lang`,
return the set of unique words.
*/

function wordsUnique(text, lang = 'en') {
return new Set(
Array.from(
new Intl.Segmenter(lang, { granularity: 'word' }).segment(text)
)
.filter(i => i.isWordLike)
.map(i => i.segment)
);
}