@visulima/string 2.0.2 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -1
- package/dist/align-text.js +32 -2
- package/dist/case/camel-case.js +55 -1
- package/dist/case/capital-case.js +47 -1
- package/dist/case/constant-case.js +7 -1
- package/dist/case/dot-case.js +7 -1
- package/dist/case/flat-case.js +7 -1
- package/dist/case/flip-case.js +76 -1
- package/dist/case/identify-case.js +65 -1
- package/dist/case/index.js +20 -1
- package/dist/case/kebab-case.js +55 -1
- package/dist/case/lower-first.js +9 -1
- package/dist/case/no-case.js +7 -1
- package/dist/case/pascal-case.js +48 -1
- package/dist/case/pascal-snake-case.js +28 -1
- package/dist/case/path-case.js +7 -1
- package/dist/case/sentence-case.js +54 -1
- package/dist/case/snake-case.js +7 -1
- package/dist/case/split-by-case.js +644 -1
- package/dist/case/title-case.js +42 -1
- package/dist/case/train-case.js +40 -1
- package/dist/case/upper-first.js +9 -1
- package/dist/closest-string.js +25 -1
- package/dist/compare-similarity.js +12 -1
- package/dist/get-string-truncated-width.js +363 -1
- package/dist/get-string-width.js +5 -1
- package/dist/index.js +36 -1
- package/dist/outdent.js +118 -1
- package/dist/packem_shared/LRUCache-3r3SovvR.js +46 -0
- package/dist/packem_shared/closest-IHuSvnSn.js +139 -0
- package/dist/packem_shared/closestN-BREy4E7C.js +19 -0
- package/dist/packem_shared/constants-CTzC6Gjg.js +95 -0
- package/dist/packem_shared/generate-cache-key-DdayDfjV.js +6 -0
- package/dist/packem_shared/joinSegments-CuU8RGpP.js +44 -0
- package/dist/packem_shared/normalize-german-eszett-DEB8-ZPT.js +7 -0
- package/dist/packem_shared/replaceString-vYvRBMfC.js +190 -0
- package/dist/packem_shared/transliterate-CUF6Ggy_.js +47776 -0
- package/dist/slice.js +413 -1
- package/dist/slugify.js +46 -1
- package/dist/test/utils.js +88 -13
- package/dist/test/vitest.js +5 -1
- package/dist/truncate.js +89 -1
- package/dist/utilities.js +54 -1
- package/dist/word-similarity-sort.js +22 -1
- package/dist/word-wrap.js +637 -8
- package/package.json +1 -1
- package/dist/packem_shared/LRUCache-Bjo6gsbC.js +0 -1
- package/dist/packem_shared/closest-uarbVSbs.js +0 -1
- package/dist/packem_shared/closestN-DjNxwK0t.js +0 -1
- package/dist/packem_shared/constants-CDcaCZKC.js +0 -1
- package/dist/packem_shared/generate-cache-key-DPByFaMH.js +0 -1
- package/dist/packem_shared/joinSegments-DTGeJBZG.js +0 -1
- package/dist/packem_shared/normalize-german-eszett-4k1qRtEP.js +0 -1
- package/dist/packem_shared/replaceString-DwIZyvoL.js +0 -1
- package/dist/packem_shared/transliterate-CxVHmZUa.js +0 -5
|
@@ -1 +1,644 @@
|
|
|
1
|
-
|
|
1
|
+
import { createRequire as __cjs_createRequire } from "node:module";
|
|
2
|
+
|
|
3
|
+
const __cjs_require = __cjs_createRequire(import.meta.url);
|
|
4
|
+
|
|
5
|
+
const __cjs_getProcess = typeof globalThis !== "undefined" && typeof globalThis.process !== "undefined" ? globalThis.process : process;
|
|
6
|
+
|
|
7
|
+
const __cjs_getBuiltinModule = (module) => {
|
|
8
|
+
// Check if we're in Node.js and version supports getBuiltinModule
|
|
9
|
+
if (typeof __cjs_getProcess !== "undefined" && __cjs_getProcess.versions && __cjs_getProcess.versions.node) {
|
|
10
|
+
const [major, minor] = __cjs_getProcess.versions.node.split(".").map(Number);
|
|
11
|
+
// Node.js 20.16.0+ and 22.3.0+
|
|
12
|
+
if (major > 22 || (major === 22 && minor >= 3) || (major === 20 && minor >= 16)) {
|
|
13
|
+
return __cjs_getProcess.getBuiltinModule(module);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
// Fallback to createRequire
|
|
17
|
+
return __cjs_require(module);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const {
|
|
21
|
+
stripVTControlCharacters
|
|
22
|
+
} = __cjs_getBuiltinModule("node:util");
|
|
23
|
+
import { j as RE_EMOJI, s as stripEmoji, k as RE_SEPARATORS, R as RE_FAST_ANSI, l as RE_CYRILLIC, m as RE_LATIN, n as RE_GREEK, o as RE_GREEK_LATIN_SPLIT, p as RE_UZBEK_LATIN_MODIFIER, q as RE_KATAKANA, r as RE_KANJI, t as RE_HIRAGANA, u as RE_HANGUL, v as RE_HEBREW, w as RE_ARABIC, x as RE_DEVANAGARI, y as RE_BENGALI, z as RE_GUJARATI, A as RE_GURMUKHI, B as RE_KANNADA, C as RE_TAMIL, D as RE_TELUGU, E as RE_MALAYALAM, F as RE_SINHALA, G as RE_THAI, H as RE_LAO, I as RE_TIBETAN, J as RE_MYANMAR, K as RE_ETHIOPIC, L as RE_KHMER, M as RE_ORIYA } from '../packem_shared/constants-CTzC6Gjg.js';
|
|
24
|
+
import LRUCache from '../packem_shared/LRUCache-3r3SovvR.js';
|
|
25
|
+
|
|
26
|
+
const regexCache = new LRUCache(1e3);
|
|
27
|
+
const getSeparatorsRegex = (separators) => {
|
|
28
|
+
const key = separators.join("");
|
|
29
|
+
if (regexCache.has(key)) {
|
|
30
|
+
return regexCache.get(key);
|
|
31
|
+
}
|
|
32
|
+
const pattern = separators.map((s) => s.replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`)).join("|");
|
|
33
|
+
const regex = new RegExp(pattern, "g");
|
|
34
|
+
regexCache.set(key, regex);
|
|
35
|
+
return regex;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const splitByEmoji = (text) => {
|
|
39
|
+
const segments = [];
|
|
40
|
+
let lastIndex = 0;
|
|
41
|
+
let match;
|
|
42
|
+
RE_EMOJI.lastIndex = 0;
|
|
43
|
+
while ((match = RE_EMOJI.exec(text)) !== null) {
|
|
44
|
+
if (match.index > lastIndex) {
|
|
45
|
+
segments.push(text.slice(lastIndex, match.index));
|
|
46
|
+
}
|
|
47
|
+
segments.push(match[0]);
|
|
48
|
+
lastIndex = RE_EMOJI.lastIndex;
|
|
49
|
+
}
|
|
50
|
+
if (lastIndex < text.length) {
|
|
51
|
+
segments.push(text.slice(lastIndex));
|
|
52
|
+
}
|
|
53
|
+
return segments.filter(Boolean);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const isUpperCode = new Uint8Array(128);
|
|
57
|
+
const isLowerCode = new Uint8Array(128);
|
|
58
|
+
const isDigitCode = new Uint8Array(128);
|
|
59
|
+
for (let index = 0; index < 128; index++) {
|
|
60
|
+
isUpperCode[index] = index >= 65 && index <= 90 ? 1 : 0;
|
|
61
|
+
isLowerCode[index] = index >= 97 && index <= 122 ? 1 : 0;
|
|
62
|
+
isDigitCode[index] = index >= 48 && index <= 57 ? 1 : 0;
|
|
63
|
+
}
|
|
64
|
+
const isUpper = (code) => isUpperCode[code];
|
|
65
|
+
const isLower = (code) => isLowerCode[code];
|
|
66
|
+
const isDigit = (code) => isDigitCode[code];
|
|
67
|
+
const handleScriptTransitions = (s, scriptDetectors, caseSensitive, locale, customSplitLogic) => {
|
|
68
|
+
if (s.length === 0) {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
let hasDetectedScript = false;
|
|
72
|
+
for (const detector of Object.values(scriptDetectors)) {
|
|
73
|
+
if (detector(s[0])) {
|
|
74
|
+
hasDetectedScript = true;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (!hasDetectedScript && !caseSensitive) {
|
|
79
|
+
return [s];
|
|
80
|
+
}
|
|
81
|
+
const chars = [...s];
|
|
82
|
+
const result = [];
|
|
83
|
+
let currentSegment = chars[0];
|
|
84
|
+
let previousType = "other";
|
|
85
|
+
for (const [type, detector] of Object.entries(scriptDetectors)) {
|
|
86
|
+
if (detector(chars[0])) {
|
|
87
|
+
previousType = type;
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
let previousIsUpper = caseSensitive && locale ? chars[0] === chars[0].toLocaleUpperCase(locale) : false;
|
|
92
|
+
for (let index = 1; index < chars.length; index++) {
|
|
93
|
+
const char = chars[index];
|
|
94
|
+
let currentType = "other";
|
|
95
|
+
for (const [type, detector] of Object.entries(scriptDetectors)) {
|
|
96
|
+
if (detector(char)) {
|
|
97
|
+
currentType = type;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const isUpperCaseChar = caseSensitive && locale ? char === char.toLocaleUpperCase(locale) : false;
|
|
102
|
+
let shouldSplit = false;
|
|
103
|
+
if (customSplitLogic) {
|
|
104
|
+
shouldSplit = customSplitLogic(previousType, currentType, previousIsUpper, isUpperCaseChar, char, index, chars);
|
|
105
|
+
} else {
|
|
106
|
+
if (previousType !== currentType && previousType !== "other" && currentType !== "other") {
|
|
107
|
+
shouldSplit = true;
|
|
108
|
+
}
|
|
109
|
+
if (caseSensitive && currentType !== "other" && !previousIsUpper && isUpperCaseChar) {
|
|
110
|
+
shouldSplit = true;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (shouldSplit) {
|
|
114
|
+
result.push(currentSegment);
|
|
115
|
+
currentSegment = char;
|
|
116
|
+
} else {
|
|
117
|
+
currentSegment += char;
|
|
118
|
+
}
|
|
119
|
+
previousType = currentType;
|
|
120
|
+
if (caseSensitive) {
|
|
121
|
+
previousIsUpper = isUpperCaseChar;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (currentSegment && currentSegment.length > 0) {
|
|
125
|
+
result.push(currentSegment);
|
|
126
|
+
}
|
|
127
|
+
return result.length > 0 ? result : [s];
|
|
128
|
+
};
|
|
129
|
+
const splitCamelCaseFast = (s, knownAcronyms = /* @__PURE__ */ new Set()) => {
|
|
130
|
+
if (s.length === 0) {
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
if (s.toUpperCase() === s) {
|
|
134
|
+
return [s];
|
|
135
|
+
}
|
|
136
|
+
let start = 0;
|
|
137
|
+
const tokens = [];
|
|
138
|
+
const width = s.length;
|
|
139
|
+
for (let index = 1; index < width; index++) {
|
|
140
|
+
const previousCode = s.codePointAt(index - 1);
|
|
141
|
+
const currentCode = s.codePointAt(index);
|
|
142
|
+
if (knownAcronyms.size > 0) {
|
|
143
|
+
for (const acronym of knownAcronyms) {
|
|
144
|
+
if (s.startsWith(acronym, start)) {
|
|
145
|
+
tokens.push(acronym);
|
|
146
|
+
start += acronym.length;
|
|
147
|
+
index = start - 1;
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (index < start) {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const previousIsUpper = previousCode && previousCode < 128 && isUpper(previousCode);
|
|
156
|
+
const currentIsUpper = currentCode && currentCode < 128 && isUpper(currentCode);
|
|
157
|
+
const previousIsLower = previousCode && previousCode < 128 && isLower(previousCode);
|
|
158
|
+
const previousIsDigit = previousCode && previousCode < 128 && isDigit(previousCode);
|
|
159
|
+
const currentIsDigit = currentCode && currentCode < 128 && isDigit(currentCode);
|
|
160
|
+
if (previousIsLower && currentIsUpper) {
|
|
161
|
+
tokens.push(s.slice(start, index));
|
|
162
|
+
start = index;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (previousIsDigit && !currentIsDigit || !previousIsDigit && currentIsDigit) {
|
|
166
|
+
tokens.push(s.slice(start, index));
|
|
167
|
+
start = index;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (currentIsDigit && !previousIsDigit) {
|
|
171
|
+
let isNextUpper = false;
|
|
172
|
+
let isNextDigit = false;
|
|
173
|
+
if (index + 1 < width) {
|
|
174
|
+
const nextCode = s.codePointAt(index + 1);
|
|
175
|
+
isNextUpper = nextCode && nextCode < 128 && isUpper(nextCode);
|
|
176
|
+
isNextDigit = nextCode && nextCode < 128 && isDigit(nextCode);
|
|
177
|
+
}
|
|
178
|
+
if (!isNextDigit && isNextUpper) {
|
|
179
|
+
tokens.push(s.slice(start, index), s.slice(index, index + 1));
|
|
180
|
+
start = index + 1;
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (index + 1 < width) {
|
|
185
|
+
const nextCode = s.codePointAt(index + 1);
|
|
186
|
+
const nextIsLower = nextCode && nextCode < 128 && isLower(nextCode);
|
|
187
|
+
if (previousIsUpper && currentIsUpper && nextIsLower) {
|
|
188
|
+
const candidate = s.slice(start, index + 1);
|
|
189
|
+
if (!knownAcronyms.has(candidate)) {
|
|
190
|
+
tokens.push(s.slice(start, index));
|
|
191
|
+
start = index;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (start < width) {
|
|
197
|
+
tokens.push(s.slice(start));
|
|
198
|
+
}
|
|
199
|
+
return tokens.filter((token) => token !== "");
|
|
200
|
+
};
|
|
201
|
+
const splitCamelCaseLocale = (s, locale, knownAcronyms) => {
|
|
202
|
+
if (s.length === 0) {
|
|
203
|
+
return [];
|
|
204
|
+
}
|
|
205
|
+
const isUpperCase = s === s.toLocaleUpperCase(locale);
|
|
206
|
+
if (locale.startsWith("de")) {
|
|
207
|
+
if (!isUpperCase && s.replaceAll("ß", "SS") === s.toLocaleUpperCase(locale)) {
|
|
208
|
+
return [s];
|
|
209
|
+
}
|
|
210
|
+
const chars2 = [...s];
|
|
211
|
+
const width_2 = chars2.length;
|
|
212
|
+
const result2 = [];
|
|
213
|
+
let currentSegment2 = chars2[0];
|
|
214
|
+
let previousIsUpper2 = chars2[0] === chars2[0].toLocaleUpperCase(locale);
|
|
215
|
+
let isInUpperSequence = previousIsUpper2;
|
|
216
|
+
let upperSequenceStart = previousIsUpper2 ? 0 : -1;
|
|
217
|
+
for (let index = 1; index < width_2; index++) {
|
|
218
|
+
const char = chars2[index];
|
|
219
|
+
const isUpperCaseChar = char === char.toLocaleUpperCase(locale);
|
|
220
|
+
if (isUpperCaseChar === previousIsUpper2) {
|
|
221
|
+
currentSegment2 += char;
|
|
222
|
+
} else if (isUpperCaseChar) {
|
|
223
|
+
if (currentSegment2 && currentSegment2.length > 0) {
|
|
224
|
+
result2.push(currentSegment2);
|
|
225
|
+
currentSegment2 = char;
|
|
226
|
+
}
|
|
227
|
+
isInUpperSequence = true;
|
|
228
|
+
upperSequenceStart = index;
|
|
229
|
+
} else {
|
|
230
|
+
if (isInUpperSequence && index - upperSequenceStart > 1) {
|
|
231
|
+
const lastUpperChar = chars2[index - 1];
|
|
232
|
+
const withoutLastUpper = currentSegment2.slice(0, -1);
|
|
233
|
+
if (withoutLastUpper && withoutLastUpper.length > 0) {
|
|
234
|
+
result2.push(withoutLastUpper);
|
|
235
|
+
}
|
|
236
|
+
currentSegment2 = lastUpperChar + char;
|
|
237
|
+
} else {
|
|
238
|
+
currentSegment2 += char;
|
|
239
|
+
}
|
|
240
|
+
isInUpperSequence = false;
|
|
241
|
+
upperSequenceStart = -1;
|
|
242
|
+
}
|
|
243
|
+
previousIsUpper2 = isUpperCaseChar;
|
|
244
|
+
}
|
|
245
|
+
if (currentSegment2 && currentSegment2.length > 0) {
|
|
246
|
+
result2.push(currentSegment2);
|
|
247
|
+
}
|
|
248
|
+
return result2;
|
|
249
|
+
}
|
|
250
|
+
if (locale.startsWith("uk") || locale.startsWith("ru") || locale.startsWith("bg") || locale.startsWith("sr") || locale.startsWith("mk") || locale.startsWith("be")) {
|
|
251
|
+
if (!RE_CYRILLIC.test(s) && !RE_LATIN.test(s)) {
|
|
252
|
+
return [s];
|
|
253
|
+
}
|
|
254
|
+
const chars2 = [...s];
|
|
255
|
+
const width_2 = chars2.length;
|
|
256
|
+
const result2 = [];
|
|
257
|
+
let currentSegment2 = chars2[0];
|
|
258
|
+
let previousType = RE_CYRILLIC.test(chars2[0]) ? 1 : RE_LATIN.test(chars2[0]) ? 2 : 0;
|
|
259
|
+
let previousIsUpper2 = chars2[0] === chars2[0].toLocaleUpperCase(locale);
|
|
260
|
+
for (let index = 1; index < width_2; index++) {
|
|
261
|
+
const char = chars2[index];
|
|
262
|
+
const currentType = RE_CYRILLIC.test(char) ? 1 : RE_LATIN.test(char) ? 2 : 0;
|
|
263
|
+
const isUpperCaseChar = char === char.toLocaleUpperCase(locale);
|
|
264
|
+
if (previousType !== currentType && (previousType === 1 || previousType === 2) && (currentType === 1 || currentType === 2) || currentType === previousType && !previousIsUpper2 && isUpperCaseChar) {
|
|
265
|
+
result2.push(currentSegment2);
|
|
266
|
+
currentSegment2 = char;
|
|
267
|
+
} else {
|
|
268
|
+
currentSegment2 += char;
|
|
269
|
+
}
|
|
270
|
+
previousType = currentType;
|
|
271
|
+
previousIsUpper2 = isUpperCaseChar;
|
|
272
|
+
}
|
|
273
|
+
if (currentSegment2 && currentSegment2.length > 0) {
|
|
274
|
+
result2.push(currentSegment2);
|
|
275
|
+
}
|
|
276
|
+
const finalResult = [];
|
|
277
|
+
for (let index = 0; index < result2.length; index++) {
|
|
278
|
+
if (index < result2.length - 1 && result2[index].length === 1 && RE_LATIN.test(result2[index]) && RE_CYRILLIC.test(result2[index + 1][0])) {
|
|
279
|
+
finalResult.push(result2[index] + result2[index + 1]);
|
|
280
|
+
index += 1;
|
|
281
|
+
} else {
|
|
282
|
+
finalResult.push(result2[index]);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return finalResult;
|
|
286
|
+
}
|
|
287
|
+
if (locale.startsWith("el")) {
|
|
288
|
+
if (!RE_GREEK.test(s) && !RE_LATIN.test(s)) {
|
|
289
|
+
return [s];
|
|
290
|
+
}
|
|
291
|
+
const parts = s.match(RE_GREEK_LATIN_SPLIT) ?? [s];
|
|
292
|
+
const result2 = [];
|
|
293
|
+
const width = parts.length;
|
|
294
|
+
if (width === 1) {
|
|
295
|
+
const part = parts[0];
|
|
296
|
+
if (!part || !RE_GREEK.test(part[0]) || part.length === 1) {
|
|
297
|
+
return [part || s];
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
for (const part of parts) {
|
|
301
|
+
if (!part) {
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
if (!RE_GREEK.test(part[0]) || part.length === 1) {
|
|
305
|
+
result2.push(part);
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
const partLength = part.length;
|
|
309
|
+
let word = part[0];
|
|
310
|
+
let previousIsUpper2 = part[0] === part[0].toLocaleUpperCase(locale);
|
|
311
|
+
for (let index = 1; index < partLength; index++) {
|
|
312
|
+
const char = part[index];
|
|
313
|
+
const isUpperCaseChar = char === char.toLocaleUpperCase(locale);
|
|
314
|
+
if (!previousIsUpper2 && isUpperCaseChar) {
|
|
315
|
+
result2.push(word);
|
|
316
|
+
word = char;
|
|
317
|
+
} else {
|
|
318
|
+
word += char;
|
|
319
|
+
}
|
|
320
|
+
previousIsUpper2 = isUpperCaseChar;
|
|
321
|
+
}
|
|
322
|
+
if (word) {
|
|
323
|
+
result2.push(word);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return result2;
|
|
327
|
+
}
|
|
328
|
+
if (locale.startsWith("ja") || locale.startsWith("ko")) {
|
|
329
|
+
const isJapanese = locale.startsWith("ja");
|
|
330
|
+
const scriptDetectors = isJapanese ? {
|
|
331
|
+
hiragana: (char) => RE_HIRAGANA.test(char),
|
|
332
|
+
kanji: (char) => RE_KANJI.test(char),
|
|
333
|
+
katakana: (char) => RE_KATAKANA.test(char),
|
|
334
|
+
latin: (char) => RE_LATIN.test(char)
|
|
335
|
+
} : {
|
|
336
|
+
hangul: (char) => RE_HANGUL.test(char),
|
|
337
|
+
latin: (char) => RE_LATIN.test(char)
|
|
338
|
+
};
|
|
339
|
+
const particles = /* @__PURE__ */ new Set(["が", "で", "と", "に", "の", "は", "へ", "も", "や", "を"]);
|
|
340
|
+
if (isJapanese) {
|
|
341
|
+
const baseSegments = handleScriptTransitions(
|
|
342
|
+
s,
|
|
343
|
+
scriptDetectors,
|
|
344
|
+
false,
|
|
345
|
+
locale,
|
|
346
|
+
(previousType, currentType) => previousType === "hiragana" && currentType === "katakana" || previousType === "katakana" && currentType === "hiragana" || previousType === "hiragana" && currentType === "latin" || previousType === "katakana" && currentType === "latin" || previousType === "kanji" && currentType === "latin" || previousType === "latin" && (currentType === "hiragana" || currentType === "katakana" || currentType === "kanji")
|
|
347
|
+
// latin -> japanese
|
|
348
|
+
);
|
|
349
|
+
const result2 = [];
|
|
350
|
+
for (const segment of baseSegments) {
|
|
351
|
+
if (segment.length === 1 && particles.has(segment) && result2.length > 0) {
|
|
352
|
+
result2[result2.length - 1] += segment;
|
|
353
|
+
} else {
|
|
354
|
+
result2.push(segment);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return result2.length > 0 ? result2 : [s];
|
|
358
|
+
}
|
|
359
|
+
return handleScriptTransitions(
|
|
360
|
+
s,
|
|
361
|
+
scriptDetectors,
|
|
362
|
+
false,
|
|
363
|
+
locale,
|
|
364
|
+
(previousType, currentType) => previousType === "hangul" && currentType === "latin" || previousType === "latin" && currentType === "hangul"
|
|
365
|
+
// latin -> hangul
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
if (locale.startsWith("sl")) {
|
|
369
|
+
const chars2 = [...s];
|
|
370
|
+
const width_2 = chars2.length;
|
|
371
|
+
const result2 = [];
|
|
372
|
+
let currentSegment2 = chars2[0];
|
|
373
|
+
let previousIsUpper2 = chars2[0] === chars2[0].toLocaleUpperCase(locale);
|
|
374
|
+
for (let index = 1; index < width_2; index++) {
|
|
375
|
+
const char = chars2[index];
|
|
376
|
+
const isUpperCaseChar = char === char.toLocaleUpperCase(locale);
|
|
377
|
+
const isSpecialChar = /[ČŠŽĐ]/i.test(char);
|
|
378
|
+
const nextIsUpper = index < width_2 - 1 && chars2[index + 1] === chars2[index + 1].toLocaleUpperCase(locale);
|
|
379
|
+
if (!previousIsUpper2 && isUpperCaseChar || isSpecialChar && nextIsUpper) {
|
|
380
|
+
result2.push(currentSegment2);
|
|
381
|
+
currentSegment2 = char;
|
|
382
|
+
if (isSpecialChar && nextIsUpper) {
|
|
383
|
+
result2.push(currentSegment2);
|
|
384
|
+
currentSegment2 = "";
|
|
385
|
+
}
|
|
386
|
+
} else {
|
|
387
|
+
currentSegment2 += char;
|
|
388
|
+
}
|
|
389
|
+
previousIsUpper2 = isUpperCaseChar;
|
|
390
|
+
}
|
|
391
|
+
if (currentSegment2 && currentSegment2.length > 0) {
|
|
392
|
+
result2.push(currentSegment2);
|
|
393
|
+
}
|
|
394
|
+
return result2;
|
|
395
|
+
}
|
|
396
|
+
if (locale.startsWith("zh")) {
|
|
397
|
+
return handleScriptTransitions(
|
|
398
|
+
s,
|
|
399
|
+
{
|
|
400
|
+
han: (char) => RE_KANJI.test(char),
|
|
401
|
+
latin: (char) => RE_LATIN.test(char)
|
|
402
|
+
},
|
|
403
|
+
false,
|
|
404
|
+
locale
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
if (["ar", "fa", "he", "ur"].includes(locale.split("-")[0])) {
|
|
408
|
+
const isRtlChar = (ch) => RE_HEBREW.test(ch) || RE_ARABIC.test(ch);
|
|
409
|
+
return handleScriptTransitions(
|
|
410
|
+
s,
|
|
411
|
+
{
|
|
412
|
+
latin: (char) => RE_LATIN.test(char),
|
|
413
|
+
rtl: (char) => isRtlChar(char)
|
|
414
|
+
},
|
|
415
|
+
false,
|
|
416
|
+
locale
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
if ([
|
|
420
|
+
"am",
|
|
421
|
+
// Amharic
|
|
422
|
+
"bn",
|
|
423
|
+
// Bengali
|
|
424
|
+
"gu",
|
|
425
|
+
// Gujarati
|
|
426
|
+
"hi",
|
|
427
|
+
// Hindi
|
|
428
|
+
"km",
|
|
429
|
+
// Khmer
|
|
430
|
+
"kn",
|
|
431
|
+
// Kannada
|
|
432
|
+
"lo",
|
|
433
|
+
// Lao
|
|
434
|
+
"ml",
|
|
435
|
+
// Malayalam
|
|
436
|
+
"mr",
|
|
437
|
+
// Marathi
|
|
438
|
+
"ne",
|
|
439
|
+
// Nepali
|
|
440
|
+
"or",
|
|
441
|
+
// Oriya
|
|
442
|
+
"pa",
|
|
443
|
+
// Punjabi
|
|
444
|
+
"si",
|
|
445
|
+
// Sinhala
|
|
446
|
+
"ta",
|
|
447
|
+
// Tamil
|
|
448
|
+
"te",
|
|
449
|
+
// Telugu
|
|
450
|
+
"th"
|
|
451
|
+
// Thai
|
|
452
|
+
].includes(locale.split("-")[0])) {
|
|
453
|
+
const isIndicChar = (ch) => RE_DEVANAGARI.test(ch) || RE_BENGALI.test(ch) || RE_GUJARATI.test(ch) || RE_GURMUKHI.test(ch) || RE_KANNADA.test(ch) || RE_TAMIL.test(ch) || RE_TELUGU.test(ch) || RE_MALAYALAM.test(ch) || RE_SINHALA.test(ch) || RE_THAI.test(ch) || RE_LAO.test(ch) || RE_TIBETAN.test(ch) || RE_MYANMAR.test(ch) || RE_ETHIOPIC.test(ch) || RE_KHMER.test(ch) || RE_ORIYA.test(ch);
|
|
454
|
+
return handleScriptTransitions(
|
|
455
|
+
s,
|
|
456
|
+
{
|
|
457
|
+
indic: (char) => isIndicChar(char),
|
|
458
|
+
latin: (char) => RE_LATIN.test(char)
|
|
459
|
+
},
|
|
460
|
+
false,
|
|
461
|
+
locale
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
if (["be", "bg", "ru", "sr", "uk"].includes(locale)) {
|
|
465
|
+
return handleScriptTransitions(
|
|
466
|
+
s,
|
|
467
|
+
{
|
|
468
|
+
cyrillic: (char) => RE_CYRILLIC.test(char),
|
|
469
|
+
latin: (char) => RE_LATIN.test(char)
|
|
470
|
+
},
|
|
471
|
+
true,
|
|
472
|
+
// Enable case-sensitive splitting
|
|
473
|
+
locale
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
if (["ar", "fa", "he"].includes(locale)) {
|
|
477
|
+
return handleScriptTransitions(
|
|
478
|
+
s,
|
|
479
|
+
{
|
|
480
|
+
latin: (char) => RE_LATIN.test(char),
|
|
481
|
+
rtl: (char) => RE_HEBREW.test(char) || RE_ARABIC.test(char)
|
|
482
|
+
},
|
|
483
|
+
false,
|
|
484
|
+
locale
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
if (locale.startsWith("ko")) {
|
|
488
|
+
return handleScriptTransitions(
|
|
489
|
+
s,
|
|
490
|
+
{
|
|
491
|
+
hangul: (char) => RE_HANGUL.test(char),
|
|
492
|
+
latin: (char) => RE_LATIN.test(char)
|
|
493
|
+
},
|
|
494
|
+
false,
|
|
495
|
+
locale
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
if (locale.startsWith("uz")) {
|
|
499
|
+
if (!RE_CYRILLIC.test(s) && !RE_LATIN.test(s)) {
|
|
500
|
+
return [s];
|
|
501
|
+
}
|
|
502
|
+
const chars2 = [...s];
|
|
503
|
+
const width_2 = chars2.length;
|
|
504
|
+
const result2 = [];
|
|
505
|
+
let currentSegment2 = chars2[0];
|
|
506
|
+
let previousIsUpper2 = chars2[0] === chars2[0].toLocaleUpperCase(locale);
|
|
507
|
+
for (let index = 1; index < width_2; index++) {
|
|
508
|
+
const char = chars2[index];
|
|
509
|
+
const isUpperCaseChar = char === char.toLocaleUpperCase(locale);
|
|
510
|
+
if (RE_UZBEK_LATIN_MODIFIER.test(char) || RE_UZBEK_LATIN_MODIFIER.test(chars2[index - 1])) {
|
|
511
|
+
currentSegment2 += char;
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
if (!previousIsUpper2 && isUpperCaseChar) {
|
|
515
|
+
result2.push(currentSegment2);
|
|
516
|
+
currentSegment2 = char;
|
|
517
|
+
} else {
|
|
518
|
+
currentSegment2 += char;
|
|
519
|
+
}
|
|
520
|
+
previousIsUpper2 = isUpperCaseChar;
|
|
521
|
+
}
|
|
522
|
+
if (currentSegment2 && currentSegment2.length > 0) {
|
|
523
|
+
result2.push(currentSegment2);
|
|
524
|
+
}
|
|
525
|
+
return result2;
|
|
526
|
+
}
|
|
527
|
+
const chars = [...s];
|
|
528
|
+
const width_ = chars.length;
|
|
529
|
+
const result = [];
|
|
530
|
+
let currentSegment = chars[0];
|
|
531
|
+
let previousIsUpper = chars[0] === chars[0].toLocaleUpperCase(locale);
|
|
532
|
+
for (const acronym of knownAcronyms) {
|
|
533
|
+
if (s.startsWith(acronym)) {
|
|
534
|
+
result.push(acronym);
|
|
535
|
+
currentSegment = chars[acronym.length];
|
|
536
|
+
previousIsUpper = currentSegment === currentSegment.toLocaleUpperCase(locale);
|
|
537
|
+
break;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
for (let index = 1; index < width_; index++) {
|
|
541
|
+
const char = chars[index];
|
|
542
|
+
const isUpperCaseChar = char === char.toLocaleUpperCase(locale);
|
|
543
|
+
let isAcronym = false;
|
|
544
|
+
for (const acronym of knownAcronyms) {
|
|
545
|
+
if (s.startsWith(acronym, index)) {
|
|
546
|
+
result.push(currentSegment, acronym);
|
|
547
|
+
index += acronym.length - 1;
|
|
548
|
+
currentSegment = "";
|
|
549
|
+
isAcronym = true;
|
|
550
|
+
break;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
if (isAcronym) {
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
if (!previousIsUpper && isUpperCaseChar) {
|
|
557
|
+
result.push(currentSegment);
|
|
558
|
+
currentSegment = char;
|
|
559
|
+
} else {
|
|
560
|
+
currentSegment += char;
|
|
561
|
+
}
|
|
562
|
+
previousIsUpper = isUpperCaseChar;
|
|
563
|
+
}
|
|
564
|
+
if (currentSegment) {
|
|
565
|
+
result.push(currentSegment);
|
|
566
|
+
}
|
|
567
|
+
return result;
|
|
568
|
+
};
|
|
569
|
+
const processTextWithAnsiEmoji = (text, locale, knownAcronyms) => {
|
|
570
|
+
const result = [];
|
|
571
|
+
const segments = RE_FAST_ANSI.test(text) ? text.split(RE_FAST_ANSI).filter(Boolean) : [text];
|
|
572
|
+
for (const seg of segments) {
|
|
573
|
+
if (RE_FAST_ANSI.test(seg)) {
|
|
574
|
+
result.push(seg);
|
|
575
|
+
} else {
|
|
576
|
+
const subs = RE_EMOJI.test(seg) ? splitByEmoji(seg).filter(Boolean) : [seg];
|
|
577
|
+
for (const sub of subs) {
|
|
578
|
+
if (RE_EMOJI.test(sub)) {
|
|
579
|
+
result.push(sub);
|
|
580
|
+
} else {
|
|
581
|
+
if (locale) {
|
|
582
|
+
const normalizedLocale = locale.toLowerCase().split("-")[0];
|
|
583
|
+
result.push(...splitCamelCaseLocale(sub, normalizedLocale, knownAcronyms));
|
|
584
|
+
} else {
|
|
585
|
+
result.push(...splitCamelCaseFast(sub, knownAcronyms));
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return result;
|
|
592
|
+
};
|
|
593
|
+
const splitByCase = (input, options = {}) => {
|
|
594
|
+
if (!input || typeof input !== "string") {
|
|
595
|
+
return [];
|
|
596
|
+
}
|
|
597
|
+
const {
|
|
598
|
+
handleAnsi = false,
|
|
599
|
+
handleEmoji = false,
|
|
600
|
+
knownAcronyms = [],
|
|
601
|
+
locale,
|
|
602
|
+
normalize = false,
|
|
603
|
+
separators,
|
|
604
|
+
stripAnsi: stripAnsiOption = false,
|
|
605
|
+
stripEmoji: stripEmojiOption = false
|
|
606
|
+
} = options;
|
|
607
|
+
const acronymSet = new Set([...knownAcronyms].sort((a, b) => b.length - a.length));
|
|
608
|
+
let cleanedInput = input;
|
|
609
|
+
if (stripAnsiOption) {
|
|
610
|
+
cleanedInput = stripVTControlCharacters(cleanedInput);
|
|
611
|
+
}
|
|
612
|
+
if (stripEmojiOption) {
|
|
613
|
+
cleanedInput = stripEmoji(cleanedInput);
|
|
614
|
+
}
|
|
615
|
+
const separatorRegex = Array.isArray(separators) ? getSeparatorsRegex(separators) : separators instanceof RegExp ? separators : RE_SEPARATORS;
|
|
616
|
+
const parts = cleanedInput.split(separatorRegex).filter(Boolean);
|
|
617
|
+
let tokens = [];
|
|
618
|
+
for (const part of parts) {
|
|
619
|
+
if (handleAnsi || handleEmoji) {
|
|
620
|
+
tokens.push(...processTextWithAnsiEmoji(part, locale, acronymSet));
|
|
621
|
+
} else if (locale) {
|
|
622
|
+
tokens.push(...splitCamelCaseLocale(part, locale, acronymSet));
|
|
623
|
+
} else {
|
|
624
|
+
tokens.push(...splitCamelCaseFast(part, acronymSet));
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
if (normalize) {
|
|
628
|
+
tokens = tokens.map((token) => {
|
|
629
|
+
if (acronymSet.has(token)) {
|
|
630
|
+
return token;
|
|
631
|
+
}
|
|
632
|
+
if (locale && token === token.toLocaleUpperCase(locale)) {
|
|
633
|
+
return token[0] + token.slice(1).toLocaleLowerCase(locale);
|
|
634
|
+
}
|
|
635
|
+
if (token.toUpperCase() === token && !acronymSet.has(token)) {
|
|
636
|
+
return token.slice(0, 1) + token.slice(1).toLowerCase();
|
|
637
|
+
}
|
|
638
|
+
return token;
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
return tokens;
|
|
642
|
+
};
|
|
643
|
+
|
|
644
|
+
export { splitByCase };
|