@visulima/string
Version:
Functions for manipulating strings.
389 lines (382 loc) • 21.3 kB
JavaScript
'use strict';
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
const constants = require('./packem_shared/constants-nihvIvk5.cjs');
var __defProp$2 = Object.defineProperty;
var __name$2 = (target, value) => __defProp$2(target, "name", { value, configurable: true });
function isAmbiguous(x) {
return x === 161 || x === 164 || x === 167 || x === 168 || x === 170 || x === 173 || x === 174 || x >= 176 && x <= 180 || x >= 182 && x <= 186 || x >= 188 && x <= 191 || x === 198 || x === 208 || x === 215 || x === 216 || x >= 222 && x <= 225 || x === 230 || x >= 232 && x <= 234 || x === 236 || x === 237 || x === 240 || x === 242 || x === 243 || x >= 247 && x <= 250 || x === 252 || x === 254 || x === 257 || x === 273 || x === 275 || x === 283 || x === 294 || x === 295 || x === 299 || x >= 305 && x <= 307 || x === 312 || x >= 319 && x <= 322 || x === 324 || x >= 328 && x <= 331 || x === 333 || x === 338 || x === 339 || x === 358 || x === 359 || x === 363 || x === 462 || x === 464 || x === 466 || x === 468 || x === 470 || x === 472 || x === 474 || x === 476 || x === 593 || x === 609 || x === 708 || x === 711 || x >= 713 && x <= 715 || x === 717 || x === 720 || x >= 728 && x <= 731 || x === 733 || x === 735 || x >= 768 && x <= 879 || x >= 913 && x <= 929 || x >= 931 && x <= 937 || x >= 945 && x <= 961 || x >= 963 && x <= 969 || x === 1025 || x >= 1040 && x <= 1103 || x === 1105 || x === 8208 || x >= 8211 && x <= 8214 || x === 8216 || x === 8217 || x === 8220 || x === 8221 || x >= 8224 && x <= 8226 || x >= 8228 && x <= 8231 || x === 8240 || x === 8242 || x === 8243 || x === 8245 || x === 8251 || x === 8254 || x === 8308 || x === 8319 || x >= 8321 && x <= 8324 || x === 8364 || x === 8451 || x === 8453 || x === 8457 || x === 8467 || x === 8470 || x === 8481 || x === 8482 || x === 8486 || x === 8491 || x === 8531 || x === 8532 || x >= 8539 && x <= 8542 || x >= 8544 && x <= 8555 || x >= 8560 && x <= 8569 || x === 8585 || x >= 8592 && x <= 8601 || x === 8632 || x === 8633 || x === 8658 || x === 8660 || x === 8679 || x === 8704 || x === 8706 || x === 8707 || x === 8711 || x === 8712 || x === 8715 || x === 8719 || x === 8721 || x === 8725 || x === 8730 || x >= 8733 && x <= 8736 || x === 8739 || x === 8741 || x >= 8743 && x <= 8748 || x === 8750 || x >= 8756 && x <= 8759 || x === 8764 || x === 8765 || x === 8776 || x === 8780 || x === 8786 || x === 8800 || x === 8801 || x >= 8804 && x <= 8807 || x === 8810 || x === 8811 || x === 8814 || x === 8815 || x === 8834 || x === 8835 || x === 8838 || x === 8839 || x === 8853 || x === 8857 || x === 8869 || x === 8895 || x === 8978 || x >= 9312 && x <= 9449 || x >= 9451 && x <= 9547 || x >= 9552 && x <= 9587 || x >= 9600 && x <= 9615 || x >= 9618 && x <= 9621 || x === 9632 || x === 9633 || x >= 9635 && x <= 9641 || x === 9650 || x === 9651 || x === 9654 || x === 9655 || x === 9660 || x === 9661 || x === 9664 || x === 9665 || x >= 9670 && x <= 9672 || x === 9675 || x >= 9678 && x <= 9681 || x >= 9698 && x <= 9701 || x === 9711 || x === 9733 || x === 9734 || x === 9737 || x === 9742 || x === 9743 || x === 9756 || x === 9758 || x === 9792 || x === 9794 || x === 9824 || x === 9825 || x >= 9827 && x <= 9829 || x >= 9831 && x <= 9834 || x === 9836 || x === 9837 || x === 9839 || x === 9886 || x === 9887 || x === 9919 || x >= 9926 && x <= 9933 || x >= 9935 && x <= 9939 || x >= 9941 && x <= 9953 || x === 9955 || x === 9960 || x === 9961 || x >= 9963 && x <= 9969 || x === 9972 || x >= 9974 && x <= 9977 || x === 9979 || x === 9980 || x === 9982 || x === 9983 || x === 10045 || x >= 10102 && x <= 10111 || x >= 11094 && x <= 11097 || x >= 12872 && x <= 12879 || x >= 57344 && x <= 63743 || x >= 65024 && x <= 65039 || x === 65533 || x >= 127232 && x <= 127242 || x >= 127248 && x <= 127277 || x >= 127280 && x <= 127337 || x >= 127344 && x <= 127373 || x === 127375 || x === 127376 || x >= 127387 && x <= 127404 || x >= 917760 && x <= 917999 || x >= 983040 && x <= 1048573 || x >= 1048576 && x <= 1114109;
}
__name$2(isAmbiguous, "isAmbiguous");
function isFullWidth(x) {
return x === 12288 || x >= 65281 && x <= 65376 || x >= 65504 && x <= 65510;
}
__name$2(isFullWidth, "isFullWidth");
function isWide(x) {
return x >= 4352 && x <= 4447 || x === 8986 || x === 8987 || x === 9001 || x === 9002 || x >= 9193 && x <= 9196 || x === 9200 || x === 9203 || x === 9725 || x === 9726 || x === 9748 || x === 9749 || x >= 9776 && x <= 9783 || x >= 9800 && x <= 9811 || x === 9855 || x >= 9866 && x <= 9871 || x === 9875 || x === 9889 || x === 9898 || x === 9899 || x === 9917 || x === 9918 || x === 9924 || x === 9925 || x === 9934 || x === 9940 || x === 9962 || x === 9970 || x === 9971 || x === 9973 || x === 9978 || x === 9981 || x === 9989 || x === 9994 || x === 9995 || x === 10024 || x === 10060 || x === 10062 || x >= 10067 && x <= 10069 || x === 10071 || x >= 10133 && x <= 10135 || x === 10160 || x === 10175 || x === 11035 || x === 11036 || x === 11088 || x === 11093 || x >= 11904 && x <= 11929 || x >= 11931 && x <= 12019 || x >= 12032 && x <= 12245 || x >= 12272 && x <= 12287 || x >= 12289 && x <= 12350 || x >= 12353 && x <= 12438 || x >= 12441 && x <= 12543 || x >= 12549 && x <= 12591 || x >= 12593 && x <= 12686 || x >= 12688 && x <= 12773 || x >= 12783 && x <= 12830 || x >= 12832 && x <= 12871 || x >= 12880 && x <= 42124 || x >= 42128 && x <= 42182 || x >= 43360 && x <= 43388 || x >= 44032 && x <= 55203 || x >= 63744 && x <= 64255 || x >= 65040 && x <= 65049 || x >= 65072 && x <= 65106 || x >= 65108 && x <= 65126 || x >= 65128 && x <= 65131 || x >= 94176 && x <= 94180 || x === 94192 || x === 94193 || x >= 94208 && x <= 100343 || x >= 100352 && x <= 101589 || x >= 101631 && x <= 101640 || x >= 110576 && x <= 110579 || x >= 110581 && x <= 110587 || x === 110589 || x === 110590 || x >= 110592 && x <= 110882 || x === 110898 || x >= 110928 && x <= 110930 || x === 110933 || x >= 110948 && x <= 110951 || x >= 110960 && x <= 111355 || x >= 119552 && x <= 119638 || x >= 119648 && x <= 119670 || x === 126980 || x === 127183 || x === 127374 || x >= 127377 && x <= 127386 || x >= 127488 && x <= 127490 || x >= 127504 && x <= 127547 || x >= 127552 && x <= 127560 || x === 127568 || x === 127569 || x >= 127584 && x <= 127589 || x >= 127744 && x <= 127776 || x >= 127789 && x <= 127797 || x >= 127799 && x <= 127868 || x >= 127870 && x <= 127891 || x >= 127904 && x <= 127946 || x >= 127951 && x <= 127955 || x >= 127968 && x <= 127984 || x === 127988 || x >= 127992 && x <= 128062 || x === 128064 || x >= 128066 && x <= 128252 || x >= 128255 && x <= 128317 || x >= 128331 && x <= 128334 || x >= 128336 && x <= 128359 || x === 128378 || x === 128405 || x === 128406 || x === 128420 || x >= 128507 && x <= 128591 || x >= 128640 && x <= 128709 || x === 128716 || x >= 128720 && x <= 128722 || x >= 128725 && x <= 128727 || x >= 128732 && x <= 128735 || x === 128747 || x === 128748 || x >= 128756 && x <= 128764 || x >= 128992 && x <= 129003 || x === 129008 || x >= 129292 && x <= 129338 || x >= 129340 && x <= 129349 || x >= 129351 && x <= 129535 || x >= 129648 && x <= 129660 || x >= 129664 && x <= 129673 || x >= 129679 && x <= 129734 || x >= 129742 && x <= 129756 || x >= 129759 && x <= 129769 || x >= 129776 && x <= 129784 || x >= 131072 && x <= 196605 || x >= 196608 && x <= 262141;
}
__name$2(isWide, "isWide");
function getCategory(x) {
if (isAmbiguous(x)) return "ambiguous";
if (isFullWidth(x)) return "fullwidth";
if (x === 8361 || x >= 65377 && x <= 65470 || x >= 65474 && x <= 65479 || x >= 65482 && x <= 65487 || x >= 65490 && x <= 65495 || x >= 65498 && x <= 65500 || x >= 65512 && x <= 65518) {
return "halfwidth";
}
if (x >= 32 && x <= 126 || x === 162 || x === 163 || x === 165 || x === 166 || x === 172 || x === 175 || x >= 10214 && x <= 10221 || x === 10629 || x === 10630) {
return "narrow";
}
if (isWide(x)) return "wide";
return "neutral";
}
__name$2(getCategory, "getCategory");
var __defProp$1 = Object.defineProperty;
var __name$1 = (target, value) => __defProp$1(target, "name", { value, configurable: true });
function validate(codePoint) {
if (!Number.isSafeInteger(codePoint)) {
throw new TypeError(`Expected a code point, got \`${typeof codePoint}\`.`);
}
}
__name$1(validate, "validate");
function eastAsianWidthType(codePoint) {
validate(codePoint);
return getCategory(codePoint);
}
__name$1(eastAsianWidthType, "eastAsianWidthType");
function eastAsianWidth(codePoint, { ambiguousAsWide = false } = {}) {
validate(codePoint);
if (isFullWidth(codePoint) || isWide(codePoint) || ambiguousAsWide && isAmbiguous(codePoint)) {
return 2;
}
return 1;
}
__name$1(eastAsianWidth, "eastAsianWidth");
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const charWidthCache = /* @__PURE__ */ new Map();
const RE_LATIN_CHARS = /(?:[\u0020-\u007E\u00A0-\u00FF](?!\uFE0F)){1,1000}/y;
const getCharType = /* @__PURE__ */ __name((codePoint) => {
if (codePoint >= 32 && codePoint <= 126) {
return "latin";
}
if (codePoint === 8203 || codePoint === 8204 || codePoint === 8205 || codePoint === 8288) {
return "zero";
}
if (codePoint <= 31 || codePoint >= 127 && codePoint <= 159) {
return "control";
}
if (codePoint >= 160 && codePoint <= 255) {
return "latin";
}
if (codePoint >= 4352 && codePoint <= 4607 || // Hangul Jamo
codePoint >= 11904 && codePoint <= 40959 || // CJK & radicals
codePoint >= 44032 && codePoint <= 55215 || // Hangul Syllables
codePoint >= 63744 && codePoint <= 64255 || // CJK Compatibility Ideographs
codePoint >= 65280 && codePoint <= 65519 && !(codePoint >= 65377 && codePoint <= 65439) || // Full-width Forms (excluding half-width katakana)
codePoint >= 12352 && codePoint <= 12543) {
return "wide";
}
return "other";
}, "getCharType");
const getCachedCharWidth = /* @__PURE__ */ __name((codePoint, config) => {
const highBits = Math.floor(codePoint / 65536);
const lowBits = codePoint % 65536;
let lowMap = charWidthCache.get(highBits);
if (!lowMap) {
lowMap = /* @__PURE__ */ new Map();
charWidthCache.set(highBits, lowMap);
}
if (lowMap.has(lowBits)) {
return lowMap.get(lowBits);
}
let width;
if (getCharType(codePoint) === "latin") {
width = config.width.regular;
} else if (getCharType(codePoint) === "control") {
width = config.width.control;
} else if (getCharType(codePoint) === "wide") {
width = config.width.wide;
} else {
const eaw = eastAsianWidthType(codePoint);
switch (eaw) {
case "fullwidth": {
width = config.width.fullWidth;
break;
}
case "wide": {
width = config.width.wide;
break;
}
case "ambiguous": {
width = config.width.ambiguousIsNarrow ? config.width.regular : config.width.wide;
break;
}
default: {
width = config.width.regular;
}
}
}
lowMap.set(lowBits, width);
return width;
}, "getCachedCharWidth");
const isCombiningCharacter = /* @__PURE__ */ __name((codePoint) => {
if (codePoint >= 768 && codePoint <= 879 || // Combining Diacritical Marks
codePoint >= 6832 && codePoint <= 6911 || // Combining Diacritical Marks Extended
codePoint >= 7616 && codePoint <= 7679 || // Combining Diacritical Marks Supplement
codePoint >= 8400 && codePoint <= 8447 || // Combining Diacritical Marks for Symbols
codePoint >= 65056 && codePoint <= 65071) {
return true;
}
if (codePoint >= 917760 && codePoint <= 917999 || // Variation Selectors Supplement
codePoint >= 65024 && codePoint <= 65039) {
return true;
}
if (codePoint >= 3633 && codePoint <= 3642 || // Thai vowel marks and tone marks
codePoint >= 3655 && codePoint <= 3662 || // Thai diacritics
codePoint >= 3761 && codePoint <= 3769 || // Lao vowel marks
codePoint >= 3771 && codePoint <= 3772 || // Lao vowel signs
codePoint >= 3784 && codePoint <= 3789) {
return true;
}
if (codePoint >= 2304 && codePoint <= 2307 || // Devanagari signs
codePoint >= 2362 && codePoint <= 2383 || // Devanagari vowel signs and virama
codePoint >= 2385 && codePoint <= 2391 || // Devanagari stress marks
codePoint >= 2402 && codePoint <= 2403 || // Devanagari vowel signs
codePoint >= 2433 && codePoint <= 2435 || // Bengali signs
codePoint >= 2492 && codePoint <= 2500 || // Bengali vowel signs
codePoint >= 2509 && codePoint <= 2509 || // Bengali virama
codePoint >= 2561 && codePoint <= 2563 || // Gurmukhi signs
codePoint >= 2620 && codePoint <= 2637) {
return true;
}
if (codePoint >= 1611 && codePoint <= 1631 || // Arabic diacritics
codePoint >= 1648 && codePoint <= 1648 || // Arabic letter superscript alef
codePoint >= 1750 && codePoint <= 1773 || // Arabic small high signs
codePoint >= 2276 && codePoint <= 2302) {
return true;
}
if (codePoint >= 1425 && codePoint <= 1469 || // Hebrew points
codePoint >= 1471 && codePoint <= 1471 || // Hebrew point rafe
codePoint >= 1473 && codePoint <= 1474 || // Hebrew points
codePoint >= 1476 && codePoint <= 1477 || // Hebrew marks
codePoint >= 1479 && codePoint <= 1479) {
return true;
}
if (codePoint >= 3893 && codePoint <= 3893 || // Tibetan mark nada
codePoint >= 3895 && codePoint <= 3895 || // Tibetan mark nada
codePoint >= 3897 && codePoint <= 3897 || // Tibetan mark tsa phru
codePoint >= 3953 && codePoint <= 3966 || // Tibetan vowel signs
codePoint >= 3968 && codePoint <= 3972 || // Tibetan vowel signs and virama
codePoint >= 3974 && codePoint <= 3975) {
return true;
}
return codePoint >= 768 && codePoint <= 777 || // Combining diacritical marks used in Vietnamese
codePoint >= 803 && codePoint <= 803;
}, "isCombiningCharacter");
const getStringTruncatedWidth = /* @__PURE__ */ __name((input, options = {}) => {
if (!input || input.length === 0) {
return { ellipsed: false, index: 0, truncated: false, width: 0 };
}
const config = {
truncation: {
ellipsis: options.ellipsis ?? "",
ellipsisWidth: options.ellipsisWidth ?? (options.ellipsis ? getStringTruncatedWidth(options.ellipsis, {
...options,
ellipsis: "",
ellipsisWidth: 0,
limit: Number.POSITIVE_INFINITY
}).width : 0),
limit: options.limit ?? Number.POSITIVE_INFINITY
},
width: {
ambiguous: options.ambiguousWidth ?? 1,
ambiguousIsNarrow: options.ambiguousIsNarrow ?? false,
ansi: options.ansiWidth ?? 0,
control: options.controlWidth ?? 0,
emoji: options.emojiWidth ?? 2,
fullWidth: options.fullWidth ?? 2,
halfWidth: options.halfWidth ?? 1,
regular: options.regularWidth ?? 1,
tab: options.tabWidth ?? 8,
wide: options.wideWidth ?? 2
}
};
const truncationLimit = Math.max(0, config.truncation.limit - config.truncation.ellipsisWidth);
const { length } = input;
const useCaching = length > 1e4;
let index = 0;
let width = 0;
let truncationIndex = length;
let truncationEnabled = false;
const hasAnsi = input.includes("\x1B") || input.includes("");
while (index < length) {
if (hasAnsi && (input[index] === "\x1B" || input[index] === "")) {
constants.RE_ANSI.lastIndex = index;
constants.RE_VALID_ANSI_PAIRS.lastIndex = index;
constants.RE_VALID_HYPERLINKS.lastIndex = index;
if (constants.RE_ANSI.test(input) && (constants.RE_VALID_ANSI_PAIRS.test(input) || constants.RE_VALID_HYPERLINKS.test(input))) {
const ansiLength = constants.RE_ANSI.lastIndex - index;
const ansiWidth = options.countAnsiEscapeCodes ? ansiLength : config.width.ansi;
if (index + 3 < length && input.slice(index, index + 4) === "\x1B]8;") {
const startPos = constants.RE_ANSI.lastIndex;
constants.RE_ANSI_LINK_END.lastIndex = startPos;
if (constants.RE_ANSI_LINK_END.test(input)) {
const endPos = constants.RE_ANSI_LINK_END.lastIndex;
const textContent = input.slice(startPos, endPos - 5);
const textWidth = textContent.length;
if (width + textWidth > truncationLimit) {
truncationIndex = Math.min(truncationIndex, startPos);
if (width + textWidth > config.truncation.limit) {
truncationEnabled = true;
break;
}
}
width += textWidth;
index = endPos;
continue;
}
}
if (width + ansiWidth > truncationLimit) {
truncationIndex = Math.min(truncationIndex, index);
if (width + ansiWidth > config.truncation.limit) {
truncationEnabled = true;
break;
}
}
width += ansiWidth;
index = constants.RE_ANSI.lastIndex;
continue;
}
}
const charCode = input.codePointAt(index);
if (charCode === 8203 || charCode === 65279 || charCode >= 8288 && charCode <= 8292) {
index++;
continue;
}
if (charCode === 9) {
if (width + config.width.tab > truncationLimit) {
truncationIndex = Math.min(truncationIndex, index);
if (width + config.width.tab > config.truncation.limit) {
truncationEnabled = true;
break;
}
}
width += config.width.tab;
index++;
continue;
}
RE_LATIN_CHARS.lastIndex = index;
if (RE_LATIN_CHARS.test(input)) {
const latinLength = RE_LATIN_CHARS.lastIndex - index;
const latinWidth = latinLength * config.width.regular;
if (width + latinWidth > truncationLimit) {
const charsToLimit = Math.floor((truncationLimit - width) / config.width.regular);
truncationIndex = Math.min(truncationIndex, index + charsToLimit);
if (width + latinWidth > config.truncation.limit) {
truncationEnabled = true;
break;
}
}
width += latinWidth;
index = RE_LATIN_CHARS.lastIndex;
continue;
}
if (charCode <= 31 || charCode >= 127 && charCode <= 159) {
constants.RE_CONTROL.lastIndex = index;
if (constants.RE_CONTROL.test(input)) {
const controlLength = constants.RE_CONTROL.lastIndex - index;
const controlWidth = controlLength * config.width.control;
if (width + controlWidth > truncationLimit) {
truncationIndex = Math.min(truncationIndex, index + Math.floor((truncationLimit - width) / config.width.control));
if (width + controlWidth > config.truncation.limit) {
truncationEnabled = true;
break;
}
}
width += controlWidth;
index = constants.RE_CONTROL.lastIndex;
continue;
}
}
constants.RE_EMOJI.lastIndex = index;
if (constants.RE_EMOJI.test(input)) {
if (width + config.width.emoji > truncationLimit) {
truncationIndex = Math.min(truncationIndex, index);
if (width + config.width.emoji > config.truncation.limit) {
truncationEnabled = true;
break;
}
}
width += config.width.emoji;
index = constants.RE_EMOJI.lastIndex;
continue;
}
const codePoint = input.codePointAt(index) ?? 0;
if (isCombiningCharacter(codePoint)) {
index += codePoint > 65535 ? 2 : 1;
continue;
}
let charWidth;
if (useCaching) {
charWidth = getCachedCharWidth(codePoint, config);
} else {
const charType = getCharType(codePoint);
switch (charType) {
case "latin": {
charWidth = config.width.regular;
break;
}
case "control": {
charWidth = config.width.control;
break;
}
case "wide": {
charWidth = config.width.wide;
break;
}
case "zero": {
charWidth = 0;
break;
}
default: {
const eaw = eastAsianWidthType(codePoint);
switch (eaw) {
case "fullwidth": {
charWidth = config.width.fullWidth;
break;
}
case "wide": {
charWidth = config.width.wide;
break;
}
case "ambiguous": {
charWidth = config.width.ambiguousIsNarrow ? config.width.regular : config.width.wide;
break;
}
default: {
charWidth = config.width.regular;
}
}
}
}
}
if (width + charWidth > truncationLimit) {
truncationIndex = Math.min(truncationIndex, index);
if (width + charWidth > config.truncation.limit) {
truncationEnabled = true;
break;
}
}
width += charWidth;
index += codePoint > 65535 ? 2 : 1;
}
let finalWidth = width;
if (truncationEnabled && config.truncation.limit >= config.truncation.ellipsisWidth) {
finalWidth = Math.min(truncationLimit + config.truncation.ellipsisWidth, config.truncation.limit);
}
return {
ellipsed: truncationEnabled && config.truncation.limit >= config.truncation.ellipsisWidth,
index: truncationEnabled ? truncationIndex : length,
truncated: truncationEnabled,
width: finalWidth
};
}, "getStringTruncatedWidth");
exports.getStringTruncatedWidth = getStringTruncatedWidth;