Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: switch grapheme-splitter to graphemer #17160

Merged
merged 4 commits into from May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 18 additions & 11 deletions lib/shared/string-utils.js
Expand Up @@ -5,21 +5,15 @@

"use strict";

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

const GraphemeSplitter = require("grapheme-splitter");

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

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

/** @type {GraphemeSplitter | undefined} */
let splitter;
/** @type {Intl.Segmenter.segment | import("graphemer").iterateGraphemes | undefined} */
let iterateGraphemes;

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

if (!splitter) {
splitter = new GraphemeSplitter();
if (!iterateGraphemes) {
if (typeof Intl !== "undefined" && typeof Intl.Segmenter !== "undefined") {
iterateGraphemes = Intl.Segmenter.prototype.segment.bind(new Intl.Segmenter());
} else {
const Graphemer = require("graphemer").default;

iterateGraphemes = Graphemer.prototype.iterateGraphemes.bind(new Graphemer());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these two methods always give the same result? If we can't guarantee that, it would be better to switch to just graphemer now and then just Intl.Segmenter in the next major version (when we drop Node < 16), because we wouldn't want linting to produce different results in different environments.

}

let count = 0;

// eslint-disable-next-line no-unused-vars -- syntax required
for (const _ of iterateGraphemes(value)) {
count++;
}

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

module.exports = {
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