From 9682d669e4ee8641293914e21679f40fee8bc354 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 9 May 2023 04:44:19 +0800 Subject: [PATCH] fix: switch `grapheme-splitter` to `graphemer` (#17160) * fix: improve `getGraphemeCount` * Fix * Fix * Use `Graphemer` only --- lib/shared/string-utils.js | 6 +++--- package.json | 2 +- tests/lib/shared/string-utils.js | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/shared/string-utils.js b/lib/shared/string-utils.js index 84c88a55d43..ed0781e578c 100644 --- a/lib/shared/string-utils.js +++ b/lib/shared/string-utils.js @@ -9,7 +9,7 @@ // Requirements //------------------------------------------------------------------------------ -const GraphemeSplitter = require("grapheme-splitter"); +const Graphemer = require("graphemer").default; //------------------------------------------------------------------------------ // Helpers @@ -18,7 +18,7 @@ const GraphemeSplitter = require("grapheme-splitter"); // eslint-disable-next-line no-control-regex -- intentionally including control characters const ASCII_REGEX = /^[\u0000-\u007f]*$/u; -/** @type {GraphemeSplitter | undefined} */ +/** @type {Graphemer | undefined} */ let splitter; //------------------------------------------------------------------------------ @@ -48,7 +48,7 @@ function getGraphemeCount(value) { } if (!splitter) { - splitter = new GraphemeSplitter(); + splitter = new Graphemer(); } return splitter.countGraphemes(value); diff --git a/package.json b/package.json index 1697beb446f..8534ac35f79 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,7 @@ "find-up": "^5.0.0", "glob-parent": "^6.0.2", "globals": "^13.19.0", - "grapheme-splitter": "^1.0.4", + "graphemer": "^1.4.0", "ignore": "^5.2.0", "import-fresh": "^3.0.0", "imurmurhash": "^0.1.4", diff --git a/tests/lib/shared/string-utils.js b/tests/lib/shared/string-utils.js index deacb54a575..6682f140f48 100644 --- a/tests/lib/shared/string-utils.js +++ b/tests/lib/shared/string-utils.js @@ -76,7 +76,8 @@ describe("getGraphemeCount", () => { "πŸ‘¨β€πŸ‘©β€πŸ‘¦aπŸ‘¨β€πŸ‘©β€πŸ‘¦": 3, "aπŸ‘¨β€πŸ‘©β€πŸ‘¦bπŸ‘¨β€πŸ‘©β€πŸ‘¦c": 5, "πŸ‘¨β€πŸ‘©β€πŸ‘¦πŸ‘": 2, - "πŸ‘ΆπŸ½πŸ‘¨β€πŸ‘©β€πŸ‘¦": 2 + "πŸ‘ΆπŸ½πŸ‘¨β€πŸ‘©β€πŸ‘¦": 2, + "πŸ‘©β€πŸ¦°πŸ‘©β€πŸ‘©β€πŸ‘¦β€πŸ‘¦πŸ³οΈβ€πŸŒˆ": 3 // 3 grapheme, 14 code points, 22 code units }; /* eslint-enable quote-props -- Make consistent here for readability */