Skip to content

Commit

Permalink
feat: replace dependency graphemer with Intl.Segmenter (#18110)
Browse files Browse the repository at this point in the history
  • Loading branch information
fasttime committed Apr 19, 2024
1 parent fb50077 commit 751b518
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
20 changes: 9 additions & 11 deletions lib/shared/string-utils.js
Expand Up @@ -5,21 +5,15 @@

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const Graphemer = require("graphemer").default;

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

// eslint-disable-next-line no-control-regex -- intentionally including control characters
const ASCII_REGEX = /^[\u0000-\u007f]*$/u;

/** @type {Graphemer | undefined} */
let splitter;
/** @type {Intl.Segmenter | undefined} */
let segmenter;

//------------------------------------------------------------------------------
// Public Interface
Expand Down Expand Up @@ -47,11 +41,15 @@ function getGraphemeCount(value) {
return value.length;
}

if (!splitter) {
splitter = new Graphemer();
segmenter ??= new Intl.Segmenter("en-US"); // en-US locale should be supported everywhere
let graphemeCount = 0;

// eslint-disable-next-line no-unused-vars -- for-of needs a variable
for (const unused of segmenter.segment(value)) {
graphemeCount++;
}

return splitter.countGraphemes(value);
return graphemeCount;
}

module.exports = {
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -88,7 +88,6 @@
"file-entry-cache": "^8.0.0",
"find-up": "^5.0.0",
"glob-parent": "^6.0.2",
"graphemer": "^1.4.0",
"ignore": "^5.2.0",
"imurmurhash": "^0.1.4",
"is-glob": "^4.0.0",
Expand Down

0 comments on commit 751b518

Please sign in to comment.