Skip to content

Commit

Permalink
fix: switch grapheme-splitter to graphemer (#17160)
Browse files Browse the repository at this point in the history
* fix: improve `getGraphemeCount`

* Fix

* Fix

* Use `Graphemer` only
  • Loading branch information
fisker committed May 8, 2023
1 parent 918b0fd commit 9682d66
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/shared/string-utils.js
Expand Up @@ -9,7 +9,7 @@
// Requirements
//------------------------------------------------------------------------------

const GraphemeSplitter = require("grapheme-splitter");
const Graphemer = require("graphemer").default;

//------------------------------------------------------------------------------
// Helpers
Expand All @@ -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;

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -48,7 +48,7 @@ function getGraphemeCount(value) {
}

if (!splitter) {
splitter = new GraphemeSplitter();
splitter = new Graphemer();
}

return splitter.countGraphemes(value);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/shared/string-utils.js
Expand Up @@ -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 */

Expand Down

0 comments on commit 9682d66

Please sign in to comment.