Skip to content

Commit 751b518

Browse files
authored
feat: replace dependency graphemer with Intl.Segmenter (#18110)
1 parent fb50077 commit 751b518

2 files changed

Lines changed: 9 additions & 12 deletions

File tree

‎lib/shared/string-utils.js‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@
55

66
"use strict";
77

8-
//------------------------------------------------------------------------------
9-
// Requirements
10-
//------------------------------------------------------------------------------
11-
12-
const Graphemer = require("graphemer").default;
13-
148
//------------------------------------------------------------------------------
159
// Helpers
1610
//------------------------------------------------------------------------------
1711

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

21-
/** @type {Graphemer | undefined} */
22-
let splitter;
15+
/** @type {Intl.Segmenter | undefined} */
16+
let segmenter;
2317

2418
//------------------------------------------------------------------------------
2519
// Public Interface
@@ -47,11 +41,15 @@ function getGraphemeCount(value) {
4741
return value.length;
4842
}
4943

50-
if (!splitter) {
51-
splitter = new Graphemer();
44+
segmenter ??= new Intl.Segmenter("en-US"); // en-US locale should be supported everywhere
45+
let graphemeCount = 0;
46+
47+
// eslint-disable-next-line no-unused-vars -- for-of needs a variable
48+
for (const unused of segmenter.segment(value)) {
49+
graphemeCount++;
5250
}
5351

54-
return splitter.countGraphemes(value);
52+
return graphemeCount;
5553
}
5654

5755
module.exports = {

‎package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
"file-entry-cache": "^8.0.0",
8989
"find-up": "^5.0.0",
9090
"glob-parent": "^6.0.2",
91-
"graphemer": "^1.4.0",
9291
"ignore": "^5.2.0",
9392
"imurmurhash": "^0.1.4",
9493
"is-glob": "^4.0.0",

0 commit comments

Comments
 (0)