Extract getStringWidth (#12173)

This commit is contained in:
Sosuke Suzuki authored and GitHub committed 2022-01-27 19:24:11 +09:00
1 parent b0e87946e9
commit bb130d2d6e
4 files changed
+28 -21

No files matched your search

+1 -20
View File
@@ -1,12 +1,10 @@
"use strict";
const stringWidth = require("string-width");
const escapeStringRegexp = require("escape-string-regexp");
const getLast = require("../utils/get-last.js");
const { getSupportInfo } = require("../main/support.js");
const isNonEmptyArray = require("../utils/is-non-empty-array.js");
const notAsciiRegex = /[^\x20-\x7F]/;
const getStringWidth = require("../utils/get-string-width.js");
const getPenultimate = (arr) => arr[arr.length - 2];
@@ -518,23 +516,6 @@ function getMinNotPresentContinuousCount(str, target) {
return max + 1;
}
/**
* @param {string} text
* @returns {number}
*/
function getStringWidth(text) {
if (!text) {
return 0;
}
// shortcut to avoid needless string `RegExp`s, replacements, and allocations within `string-width`
if (!notAsciiRegex.test(text)) {
return text.length;
}
return stringWidth(text);
}
function addCommentHelper(node, comment) {
const comments = node.comments || (node.comments = []);
comments.push(comment);
+2 -1
View File
@@ -1,7 +1,8 @@
"use strict";
const { getStringWidth, getLast } = require("../common/util.js");
const { convertEndOfLineToChars } = require("../common/end-of-line.js");
const getLast = require("../utils/get-last.js");
const getStringWidth = require("../utils/get-string-width.js");
const { fill, cursor, indent } = require("./doc-builders.js");
const { isConcat, getDocParts } = require("./doc-utils.js");
+1
View File
@@ -1,4 +1,5 @@
"use strict";
const getLast = require("../utils/get-last.js");
const { literalline, join } = require("./doc-builders.js");
+24
View File
@@ -0,0 +1,24 @@
"use strict";
const stringWidth = require("string-width");
const notAsciiRegex = /[^\x20-\x7F]/;
/**
* @param {string} text
* @returns {number}
*/
function getStringWidth(text) {
if (!text) {
return 0;
}
// shortcut to avoid needless string `RegExp`s, replacements, and allocations within `string-width`
if (!notAsciiRegex.test(text)) {
return text.length;
}
return stringWidth(text);
}
module.exports = getStringWidth;