@visulima/string 3.0.0-alpha.8 → 3.0.0

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.
Files changed (121) hide show
  1. package/CHANGELOG.md +326 -0
  2. package/LICENSE.md +119 -0
  3. package/README.md +88 -4
  4. package/dist/align-text.d.ts +35 -0
  5. package/dist/align-text.js +2 -32
  6. package/dist/case/camel-case.d.ts +17 -3
  7. package/dist/case/camel-case.js +1 -55
  8. package/dist/case/capital-case.d.ts +14 -3
  9. package/dist/case/capital-case.js +1 -47
  10. package/dist/case/constant-case.d.ts +14 -3
  11. package/dist/case/constant-case.js +1 -7
  12. package/dist/case/dot-case.d.ts +14 -3
  13. package/dist/case/dot-case.js +1 -7
  14. package/dist/case/flat-case.d.ts +12 -3
  15. package/dist/case/flat-case.js +1 -7
  16. package/dist/case/flip-case.d.ts +16 -3
  17. package/dist/case/flip-case.js +1 -82
  18. package/dist/case/identify-case.d.ts +15 -3
  19. package/dist/case/identify-case.js +1 -65
  20. package/dist/case/index.d.ts +9 -4
  21. package/dist/case/index.js +1 -20
  22. package/dist/case/kebab-case.d.ts +25 -3
  23. package/dist/case/kebab-case.js +1 -55
  24. package/dist/case/lower-first.d.ts +13 -3
  25. package/dist/case/lower-first.js +1 -9
  26. package/dist/case/no-case.d.ts +14 -3
  27. package/dist/case/no-case.js +1 -7
  28. package/dist/case/pascal-case.d.ts +17 -3
  29. package/dist/case/pascal-case.js +1 -48
  30. package/dist/case/pascal-snake-case.d.ts +14 -3
  31. package/dist/case/pascal-snake-case.js +1 -28
  32. package/dist/case/path-case.d.ts +14 -3
  33. package/dist/case/path-case.js +1 -7
  34. package/dist/case/sentence-case.d.ts +14 -3
  35. package/dist/case/sentence-case.js +1 -54
  36. package/dist/case/snake-case.d.ts +14 -3
  37. package/dist/case/snake-case.js +1 -7
  38. package/dist/case/split-by-case.d.ts +29 -3
  39. package/dist/case/split-by-case.js +1 -747
  40. package/dist/case/title-case.d.ts +12 -3
  41. package/dist/case/title-case.js +1 -42
  42. package/dist/case/train-case.d.ts +14 -3
  43. package/dist/case/train-case.js +1 -40
  44. package/dist/case/upper-first.d.ts +13 -3
  45. package/dist/case/upper-first.js +1 -9
  46. package/dist/closest-string.d.ts +32 -0
  47. package/dist/closest-string.js +1 -23
  48. package/dist/compare-similarity.d.ts +37 -0
  49. package/dist/compare-similarity.js +1 -12
  50. package/dist/count-occurrences.d.ts +9 -0
  51. package/dist/count-occurrences.js +1 -0
  52. package/dist/direction.d.ts +7 -0
  53. package/dist/direction.js +1 -0
  54. package/dist/east-asian-width.d.ts +52 -0
  55. package/dist/east-asian-width.js +1 -0
  56. package/dist/excerpt.d.ts +25 -0
  57. package/dist/excerpt.js +53 -0
  58. package/dist/get-string-truncated-width.d.ts +151 -0
  59. package/dist/get-string-truncated-width.js +1 -444
  60. package/dist/get-string-width.d.ts +58 -0
  61. package/dist/get-string-width.js +1 -5
  62. package/dist/indent.d.ts +87 -0
  63. package/dist/indent.js +1 -0
  64. package/dist/index.d.ts +12 -15
  65. package/dist/index.js +1 -39
  66. package/dist/is-fullwidth-code-point.d.ts +7 -0
  67. package/dist/is-fullwidth-code-point.js +1 -0
  68. package/dist/levenshtein.d.ts +25 -0
  69. package/dist/levenshtein.js +1 -0
  70. package/dist/native-string-types.d.ts +239 -0
  71. package/dist/outdent.d.ts +74 -0
  72. package/dist/outdent.js +1 -117
  73. package/dist/packem_shared/_isFullWidth-Dqc3MWgl.js +1 -0
  74. package/dist/packem_shared/closest-C6YDQs4m.js +1 -0
  75. package/dist/packem_shared/constants-aPzGW29R.js +1 -0
  76. package/dist/packem_shared/dedent-CPjAYUXh.js +1 -0
  77. package/dist/packem_shared/eastAsianWidth-DJV8Kqd-.js +1 -0
  78. package/dist/packem_shared/generate-cache-key-BqU1Cqwm.js +1 -0
  79. package/dist/packem_shared/indent-cz8WMI0e.js +1 -0
  80. package/dist/packem_shared/joinSegments-CaHT-45s.js +1 -0
  81. package/dist/packem_shared/lru-cache-BtMSrYQJ.js +1 -0
  82. package/dist/packem_shared/normalize-german-eszett-hNMwtGjd.js +1 -0
  83. package/dist/packem_shared/redent-ByOoJ6fN.js +1 -0
  84. package/dist/packem_shared/types.d-CmNvyl4l.d.ts +360 -0
  85. package/dist/packem_shared/types.d-fhe030jX.d.ts +402 -0
  86. package/dist/replace-string.d.ts +10 -0
  87. package/dist/replace-string.js +1 -0
  88. package/dist/slice.d.ts +42 -0
  89. package/dist/slice.js +1 -413
  90. package/dist/slugify.d.ts +10 -1
  91. package/dist/slugify.js +1 -46
  92. package/dist/test/utils.d.ts +17 -0
  93. package/dist/test/utils.js +13 -88
  94. package/dist/test/vitest.d.ts +3 -0
  95. package/dist/test/vitest.js +1 -5
  96. package/dist/transliterate.d.ts +10 -0
  97. package/dist/transliterate.js +5 -0
  98. package/dist/truncate.d.ts +43 -0
  99. package/dist/truncate.js +1 -114
  100. package/dist/utilities.d.ts +28 -2
  101. package/dist/utilities.js +1 -54
  102. package/dist/word-similarity-sort.d.ts +34 -0
  103. package/dist/word-similarity-sort.js +1 -22
  104. package/dist/word-wrap.d.ts +44 -0
  105. package/dist/word-wrap.js +8 -637
  106. package/package.json +70 -39
  107. package/dist/packem_shared/LRUCache-3r3SovvR.js +0 -46
  108. package/dist/packem_shared/closest-IHuSvnSn.js +0 -139
  109. package/dist/packem_shared/closestN-Cvsw0Fue.js +0 -24
  110. package/dist/packem_shared/constants-D5qaproB.js +0 -95
  111. package/dist/packem_shared/countOccurrences-CS9XprQN.js +0 -22
  112. package/dist/packem_shared/direction-BVhR8MOQ.js +0 -10
  113. package/dist/packem_shared/excerpt-COsVNttN.js +0 -1502
  114. package/dist/packem_shared/generate-cache-key-DdayDfjV.js +0 -6
  115. package/dist/packem_shared/joinSegments-BStLas0c.js +0 -44
  116. package/dist/packem_shared/lru-cache.d-BsP6kbVJ.d.ts +0 -13
  117. package/dist/packem_shared/normalize-german-eszett-DEB8-ZPT.js +0 -7
  118. package/dist/packem_shared/replaceString-BVlxk27d.js +0 -190
  119. package/dist/packem_shared/transliterate-DGlx-woj.js +0 -47756
  120. package/dist/packem_shared/types.d-BVNv675q.d.ts +0 -61
  121. package/dist/packem_shared/types.d-DfgbcNKh.d.ts +0 -57
package/dist/word-wrap.js CHANGED
@@ -1,637 +1,8 @@
1
- import { O as ESCAPES, P as ANSI_ESCAPE_LINK, Q as ANSI_SGR_TERMINATOR, S as ANSI_ESCAPE_BELL, T as ANSI_CSI, U as RE_ESCAPE_PATTERN, V as ANSI_RESET_CODES, W as END_CODE, X as RE_ZERO_WIDTH } from './packem_shared/constants-D5qaproB.js';
2
- import { getStringWidth } from './get-string-width.js';
3
-
4
- class AnsiStateTracker {
5
- activeForeground = void 0;
6
- activeBackground = void 0;
7
- // Track other formatting (bold, italic, etc.)
8
- activeFormatting = [];
9
- /**
10
- * Processes an escape sequence and updates the internal state
11
- * @param sequence The escape sequence to process
12
- */
13
- processEscape(sequence) {
14
- const match = /\x1B\[(\d+)m/.exec(sequence);
15
- if (!match) {
16
- return;
17
- }
18
- const code = Number.parseInt(match[1], 10);
19
- switch (code) {
20
- case 0: {
21
- this.activeForeground = void 0;
22
- this.activeBackground = void 0;
23
- this.activeFormatting = [];
24
- break;
25
- }
26
- case 39: {
27
- this.activeForeground = void 0;
28
- break;
29
- }
30
- case 49: {
31
- this.activeBackground = void 0;
32
- break;
33
- }
34
- default: {
35
- if (code >= 30 && code <= 37 || code >= 90 && code <= 97) {
36
- this.activeForeground = sequence;
37
- } else if (code >= 40 && code <= 47 || code >= 100 && code <= 107) {
38
- this.activeBackground = sequence;
39
- } else if ([1, 2, 3, 4, 7, 8, 9].includes(code)) {
40
- this.activeFormatting.push(sequence);
41
- } else if ([22, 23, 24, 27, 28, 29].includes(code)) {
42
- const formatResetMap = {
43
- 22: "[1m",
44
- // Reset bold
45
- 23: "[3m",
46
- // Reset italic
47
- 24: "[4m",
48
- // Reset underline
49
- 27: "[7m",
50
- // Reset inverse
51
- 28: "[8m",
52
- // Reset hidden
53
- 29: "[9m"
54
- // Reset strikethrough
55
- };
56
- const formatToRemove = formatResetMap[code];
57
- if (formatToRemove) {
58
- this.activeFormatting = this.activeFormatting.filter((fmt) => !fmt.includes(formatToRemove));
59
- }
60
- }
61
- }
62
- }
63
- }
64
- /**
65
- * Gets all active escape sequences to apply
66
- * @returns String with all active escapes
67
- */
68
- getStartEscapesForAllActiveAttributes() {
69
- return [this.activeBackground, this.activeForeground, ...this.activeFormatting].filter(Boolean).join("");
70
- }
71
- /**
72
- * Gets all closing escape sequences for the currently active attributes.
73
- * The order is generally reverse of application: formatting, foreground, background.
74
- * @returns String with all necessary closing escapes.
75
- */
76
- getEndEscapesForAllActiveAttributes() {
77
- const closingEscapes = [];
78
- if (this.activeFormatting.length > 0) {
79
- const formatResetMap = {
80
- "\x1B[1m": "\x1B[22m",
81
- // Bold
82
- "\x1B[2m": "\x1B[22m",
83
- // Faint/Dim (also reset by 22)
84
- "\x1B[3m": "\x1B[23m",
85
- // Italic
86
- "\x1B[4m": "\x1B[24m",
87
- // Underline
88
- "\x1B[7m": "\x1B[27m",
89
- // Inverse
90
- "\x1B[8m": "\x1B[28m",
91
- // Hidden/Conceal
92
- "\x1B[9m": "\x1B[29m"
93
- // Strikethrough
94
- };
95
- [...this.activeFormatting].toReversed().forEach((formatCode) => {
96
- const resetCode = formatResetMap[formatCode];
97
- if (resetCode) {
98
- closingEscapes.push(resetCode);
99
- }
100
- });
101
- }
102
- if (this.activeForeground) {
103
- closingEscapes.push("\x1B[39m");
104
- }
105
- if (this.activeBackground) {
106
- closingEscapes.push("\x1B[49m");
107
- }
108
- return closingEscapes.join("");
109
- }
110
- }
111
-
112
- const checkEscapeSequence = (chars, index) => {
113
- if (!ESCAPES.has(chars[index])) {
114
- return { isInsideEscape: false, isInsideLinkEscape: false };
115
- }
116
- const isInsideEscape = true;
117
- const possibleLink = chars.slice(index + 1, index + 1 + ANSI_ESCAPE_LINK.length).join("");
118
- const isInsideLinkEscape = possibleLink === ANSI_ESCAPE_LINK;
119
- return { isInsideEscape, isInsideLinkEscape };
120
- };
121
- const processAnsiString = (string, options = {}) => {
122
- const stateTracker = new AnsiStateTracker();
123
- let currentText = "";
124
- let isInsideEscape = false;
125
- let isInsideLinkEscape = false;
126
- let escapeBuffer = "";
127
- let currentUrl = "";
128
- let isInHyperlink = false;
129
- const chars = [...string];
130
- for (let index = 0; index < chars.length; index++) {
131
- const character = chars[index];
132
- if (character && ESCAPES.has(character)) {
133
- if (currentText) {
134
- const width2 = options.getWidth?.(currentText) ?? 0;
135
- const segment2 = {
136
- isEscapeSequence: false,
137
- isGrapheme: true,
138
- text: currentText,
139
- width: width2
140
- };
141
- if (isInHyperlink) {
142
- segment2.isHyperlink = true;
143
- segment2.hyperlinkUrl = currentUrl;
144
- }
145
- if (options.onSegment?.(segment2, stateTracker) === false) {
146
- return;
147
- }
148
- currentText = "";
149
- }
150
- isInsideEscape = true;
151
- escapeBuffer = character;
152
- const escapeInfo = checkEscapeSequence(chars, index);
153
- isInsideLinkEscape = escapeInfo.isInsideLinkEscape;
154
- if (isInsideLinkEscape) {
155
- let urlEnd = index + 1;
156
- currentUrl = "";
157
- while (urlEnd < chars.length) {
158
- const nextChar = chars[urlEnd];
159
- if (nextChar === ANSI_ESCAPE_BELL) {
160
- break;
161
- }
162
- currentUrl += nextChar;
163
- urlEnd += 1;
164
- }
165
- currentUrl = currentUrl.slice(4);
166
- const segment2 = {
167
- hyperlinkUrl: currentUrl,
168
- isEscapeSequence: true,
169
- isGrapheme: false,
170
- isHyperlink: true,
171
- isHyperlinkStart: true,
172
- width: 0
173
- };
174
- if (options.onSegment?.(segment2, stateTracker) === false) {
175
- return;
176
- }
177
- index = urlEnd;
178
- isInHyperlink = true;
179
- isInsideEscape = false;
180
- isInsideLinkEscape = false;
181
- escapeBuffer = "";
182
- continue;
183
- }
184
- if (index + 1 < chars.length && chars[index + 1] === "\\" && isInHyperlink) {
185
- const segment2 = {
186
- isEscapeSequence: true,
187
- isGrapheme: false,
188
- isHyperlink: true,
189
- isHyperlinkEnd: true,
190
- width: 0
191
- };
192
- if (options.onSegment?.(segment2, stateTracker) === false) {
193
- return;
194
- }
195
- isInHyperlink = false;
196
- currentUrl = "";
197
- index += 1;
198
- isInsideEscape = false;
199
- escapeBuffer = "";
200
- continue;
201
- }
202
- }
203
- if (isInsideEscape) {
204
- if (escapeBuffer !== character) {
205
- escapeBuffer += character;
206
- }
207
- if (character === ANSI_SGR_TERMINATOR) {
208
- isInsideEscape = false;
209
- stateTracker.processEscape(escapeBuffer);
210
- const segment2 = {
211
- isEscapeSequence: true,
212
- isGrapheme: false,
213
- text: escapeBuffer,
214
- width: 0
215
- };
216
- if (options.onSegment?.(segment2, stateTracker) === false) {
217
- return;
218
- }
219
- escapeBuffer = "";
220
- }
221
- continue;
222
- }
223
- currentText += character;
224
- const width = options.getWidth?.(currentText) ?? 0;
225
- const segment = {
226
- isEscapeSequence: false,
227
- isGrapheme: true,
228
- text: currentText,
229
- width
230
- };
231
- if (isInHyperlink) {
232
- segment.isHyperlink = true;
233
- segment.hyperlinkUrl = currentUrl;
234
- }
235
- if (options.onSegment?.(segment, stateTracker) === false) {
236
- return;
237
- }
238
- currentText = "";
239
- }
240
- if (currentText) {
241
- const width = options.getWidth?.(currentText) ?? 0;
242
- const segment = {
243
- isEscapeSequence: false,
244
- isGrapheme: true,
245
- text: currentText,
246
- width
247
- };
248
- if (isInHyperlink) {
249
- segment.isHyperlink = true;
250
- segment.hyperlinkUrl = currentUrl;
251
- }
252
- options.onSegment?.(segment, stateTracker);
253
- }
254
- if (escapeBuffer) {
255
- const segment = {
256
- isEscapeSequence: true,
257
- isGrapheme: false,
258
- text: escapeBuffer,
259
- width: 0
260
- };
261
- options.onSegment?.(segment, stateTracker);
262
- }
263
- };
264
-
265
- const wrapAnsiCode = (code) => {
266
- const escapeChar = ESCAPES.values().next().value;
267
- return `${escapeChar}${ANSI_CSI}${code}${ANSI_SGR_TERMINATOR}`;
268
- };
269
-
270
- const wrapAnsiHyperlink = (url) => {
271
- const escapeChar = ESCAPES.values().next().value;
272
- return `${escapeChar}${ANSI_ESCAPE_LINK}${url}${ANSI_ESCAPE_BELL}`;
273
- };
274
-
275
- const preserveAnsi = (rawLines) => {
276
- if (rawLines.length === 0) {
277
- return "";
278
- }
279
- if (rawLines.length === 1) {
280
- return rawLines[0];
281
- }
282
- let returnValue = "";
283
- let escapeCode;
284
- let escapeUrl;
285
- const preString = rawLines.join("\n");
286
- const pre = [...preString];
287
- let preStringIndex = 0;
288
- for (const [index, character] of pre.entries()) {
289
- returnValue += character;
290
- if (ESCAPES.has(character)) {
291
- const match = RE_ESCAPE_PATTERN.exec(preString.slice(preStringIndex));
292
- const groups = match?.groups ?? {};
293
- if (groups.code !== void 0) {
294
- const code2 = Number.parseFloat(groups.code);
295
- escapeCode = code2 === END_CODE ? void 0 : code2;
296
- } else if (groups.uri !== void 0) {
297
- escapeUrl = groups.uri.length === 0 ? void 0 : groups.uri;
298
- }
299
- }
300
- const code = ANSI_RESET_CODES.get(Number(escapeCode));
301
- if (pre[index + 1] === "\n") {
302
- if (escapeUrl) {
303
- returnValue += wrapAnsiHyperlink("");
304
- }
305
- if (escapeCode && code) {
306
- returnValue += wrapAnsiCode(code);
307
- }
308
- } else if (character === "\n") {
309
- if (escapeCode && code) {
310
- returnValue += wrapAnsiCode(escapeCode);
311
- }
312
- if (escapeUrl) {
313
- returnValue += wrapAnsiHyperlink(escapeUrl);
314
- }
315
- }
316
- preStringIndex += character.length;
317
- }
318
- return returnValue;
319
- };
320
-
321
- const resetAnsiAtLineBreak = (currentLine) => {
322
- if (!currentLine.includes("\x1B")) {
323
- return currentLine;
324
- }
325
- let result = currentLine;
326
- if (currentLine.includes("\x1B[30m")) {
327
- result += "\x1B[39m";
328
- }
329
- if (currentLine.includes("\x1B[42m")) {
330
- result += "\x1B[49m";
331
- }
332
- return result;
333
- };
334
- const stringVisibleTrimSpacesRight = (string) => {
335
- const words = string.split(" ");
336
- let last = words.length;
337
- while (last > 0 && getStringWidth(words[last - 1]) === 0) {
338
- last--;
339
- }
340
- if (last === words.length) {
341
- return string;
342
- }
343
- return words.slice(0, last).join(" ") + words.slice(last).join("");
344
- };
345
- const wrapWithBreakAtWidth = (string, width, trim) => {
346
- if (string.length === 0) {
347
- return [""];
348
- }
349
- if (width <= 0) {
350
- return [string];
351
- }
352
- const rows = [];
353
- const ansiTracker = new AnsiStateTracker();
354
- let currentLine = "";
355
- let currentWidth = 0;
356
- let isInsideEscape = false;
357
- let isInsideLinkEscape = false;
358
- let escapeBuffer = "";
359
- let index = 0;
360
- while (index < string.length) {
361
- const char = string[index];
362
- if (ESCAPES.has(char)) {
363
- isInsideEscape = true;
364
- escapeBuffer = char;
365
- currentLine += char;
366
- const escapeInfo = checkEscapeSequence([...string], index);
367
- isInsideLinkEscape = escapeInfo.isInsideLinkEscape;
368
- index += 1;
369
- continue;
370
- }
371
- if (isInsideEscape) {
372
- escapeBuffer += char;
373
- currentLine += char;
374
- if (isInsideLinkEscape) {
375
- if (char === ANSI_ESCAPE_BELL) {
376
- isInsideEscape = isInsideLinkEscape = false;
377
- }
378
- } else if (char === ANSI_SGR_TERMINATOR) {
379
- isInsideEscape = false;
380
- ansiTracker.processEscape(escapeBuffer);
381
- }
382
- index += 1;
383
- continue;
384
- }
385
- const charWidth = getStringWidth(char);
386
- const isSpace = char === " ";
387
- if (charWidth === 0) {
388
- currentLine += char;
389
- index += 1;
390
- continue;
391
- }
392
- if (currentWidth + charWidth > width) {
393
- if (currentLine) {
394
- rows.push(currentLine + ansiTracker.getEndEscapesForAllActiveAttributes());
395
- }
396
- currentLine = ansiTracker.getStartEscapesForAllActiveAttributes();
397
- currentWidth = getStringWidth(currentLine);
398
- if (isSpace && trim) {
399
- while (index < string.length && string[index] === " ") {
400
- index += 1;
401
- }
402
- continue;
403
- }
404
- }
405
- currentLine += char;
406
- currentWidth += charWidth;
407
- if (currentWidth === width && index < string.length - 1) {
408
- rows.push(currentLine + ansiTracker.getEndEscapesForAllActiveAttributes());
409
- currentLine = ansiTracker.getStartEscapesForAllActiveAttributes();
410
- currentWidth = getStringWidth(currentLine);
411
- if (index + 1 < string.length && string[index + 1] === " " && trim) {
412
- index += 1;
413
- while (index < string.length && string[index] === " ") {
414
- index += 1;
415
- }
416
- continue;
417
- }
418
- }
419
- index += 1;
420
- }
421
- if (currentLine) {
422
- rows.push(currentLine + ansiTracker.getEndEscapesForAllActiveAttributes());
423
- }
424
- return trim ? rows.map((element) => stringVisibleTrimSpacesRight(element)) : rows;
425
- };
426
- const wrapCharByChar = (string, width, trim) => {
427
- if (string.length === 0) {
428
- return [];
429
- }
430
- const inputToProcess = trim ? string.trim() : string;
431
- if (inputToProcess.length === 0) {
432
- return [];
433
- }
434
- const rows = [];
435
- let currentLine = "";
436
- let currentWidth = 0;
437
- processAnsiString(inputToProcess, {
438
- getWidth: getStringWidth,
439
- // eslint-disable-next-line sonarjs/cognitive-complexity,sonarjs/no-invariant-returns
440
- onSegment: (segment, stateTracker) => {
441
- if (segment.isEscapeSequence) {
442
- currentLine += segment.text;
443
- } else {
444
- const isSpace = segment.text === " ";
445
- if (segment.width === 0) {
446
- currentLine += segment.text;
447
- return true;
448
- }
449
- if (currentWidth + segment.width > width) {
450
- if (currentLine) {
451
- rows.push(currentLine);
452
- }
453
- currentLine = stateTracker.getStartEscapesForAllActiveAttributes();
454
- currentWidth = 0;
455
- if (isSpace) {
456
- if (trim) {
457
- return true;
458
- }
459
- rows.push(stateTracker.getStartEscapesForAllActiveAttributes() + segment.text);
460
- return true;
461
- }
462
- }
463
- currentLine += segment.text;
464
- currentWidth += segment.width;
465
- }
466
- return true;
467
- }
468
- });
469
- if (currentLine) {
470
- rows.push(currentLine);
471
- }
472
- return trim ? rows.map((row) => stringVisibleTrimSpacesRight(row)) : rows;
473
- };
474
- const wrapWithWordBoundaries = (string, width, trim) => {
475
- if (string.length === 0) {
476
- return [];
477
- }
478
- const inputToProcess = trim ? string.trim() : string;
479
- if (inputToProcess.length === 0) {
480
- return [];
481
- }
482
- const tokens = inputToProcess.split(/(?=\s)|(?<=\s)/);
483
- const rows = [];
484
- let currentLine = "";
485
- let currentWidth = 0;
486
- let index = 0;
487
- while (index < tokens.length) {
488
- const token = tokens[index];
489
- const isSpace = /^\s+$/.test(token);
490
- const tokenVisibleWidth = getStringWidth(token);
491
- if (token.length === 0) {
492
- index += 1;
493
- continue;
494
- }
495
- if (trim && isSpace && currentWidth === 0) {
496
- index += 1;
497
- continue;
498
- }
499
- if (currentWidth + tokenVisibleWidth > width && currentWidth > 0) {
500
- if (trim) {
501
- rows.push(stringVisibleTrimSpacesRight(currentLine));
502
- } else {
503
- rows.push(currentLine);
504
- }
505
- currentLine = "";
506
- currentWidth = 0;
507
- continue;
508
- }
509
- currentLine += token;
510
- currentWidth += tokenVisibleWidth;
511
- index += 1;
512
- }
513
- if (currentLine) {
514
- if (trim) {
515
- rows.push(stringVisibleTrimSpacesRight(currentLine));
516
- } else {
517
- rows.push(currentLine);
518
- }
519
- }
520
- return rows;
521
- };
522
- const wrapAndBreakWords = (string, width, trim) => {
523
- if (string.length === 0) {
524
- return [];
525
- }
526
- const inputToProcess = trim ? string.trim() : string;
527
- if (inputToProcess.length === 0) {
528
- return [];
529
- }
530
- const tokens = inputToProcess.split(/(?=\s)|(?<=\s)/);
531
- const rows = [];
532
- let currentLine = "";
533
- let currentWidth = 0;
534
- let index = 0;
535
- while (index < tokens.length) {
536
- const token = tokens[index];
537
- const isSpace = /^\s+$/.test(token);
538
- const tokenVisibleWidth = getStringWidth(token);
539
- if (token.length === 0) {
540
- index += 1;
541
- continue;
542
- }
543
- if (trim && isSpace && currentWidth === 0) {
544
- index += 1;
545
- continue;
546
- }
547
- if (tokenVisibleWidth > width) {
548
- if (currentLine) {
549
- rows.push(resetAnsiAtLineBreak(trim ? stringVisibleTrimSpacesRight(currentLine) : currentLine));
550
- }
551
- const brokenLines = wrapWithBreakAtWidth(token, width, trim);
552
- if (brokenLines.length > 0) {
553
- for (let brokenLineIndex = 0; brokenLineIndex < brokenLines.length - 1; brokenLineIndex += 1) {
554
- rows.push(brokenLines[brokenLineIndex]);
555
- }
556
- currentLine = brokenLines[brokenLines.length - 1];
557
- currentWidth = getStringWidth(currentLine);
558
- } else {
559
- currentLine = "";
560
- currentWidth = 0;
561
- }
562
- index += 1;
563
- continue;
564
- }
565
- if (currentWidth + tokenVisibleWidth > width && currentWidth > 0) {
566
- rows.push(resetAnsiAtLineBreak(trim ? stringVisibleTrimSpacesRight(currentLine) : currentLine));
567
- currentLine = "";
568
- currentWidth = 0;
569
- if (trim && isSpace) {
570
- index += 1;
571
- continue;
572
- }
573
- }
574
- currentLine += token;
575
- currentWidth += tokenVisibleWidth;
576
- index += 1;
577
- }
578
- if (currentLine) {
579
- rows.push(resetAnsiAtLineBreak(trim ? stringVisibleTrimSpacesRight(currentLine) : currentLine));
580
- }
581
- return rows;
582
- };
583
- const WrapMode = {
584
- /**
585
- * Breaks words at character boundaries to fit the width
586
- */
587
- BREAK_AT_CHARACTERS: "BREAK_AT_CHARACTERS",
588
- /**
589
- * Breaks lines at word boundaries. If a word is longer than the width, it will be broken.
590
- */
591
- BREAK_WORDS: "BREAK_WORDS",
592
- /**
593
- * Preserves word boundaries, words are kept intact even if they exceed width
594
- */
595
- PRESERVE_WORDS: "PRESERVE_WORDS",
596
- /**
597
- * Enforces strict adherence to the width limit by breaking at exact width
598
- */
599
- STRICT_WIDTH: "STRICT_WIDTH"
600
- };
601
- const wordWrap = (string, options = {}) => {
602
- const { removeZeroWidthCharacters = true, trim = true, width = 80, wrapMode = WrapMode.PRESERVE_WORDS } = options;
603
- if (trim && string.trim() === "") {
604
- return "";
605
- }
606
- let normalizedString = String(string).normalize("NFC").replaceAll("\r\n", "\n");
607
- if (removeZeroWidthCharacters) {
608
- normalizedString = normalizedString.replaceAll(RE_ZERO_WIDTH, "");
609
- }
610
- const result = normalizedString.split("\n").map((line) => {
611
- if (trim && line.trim() === "") {
612
- return "";
613
- }
614
- let wrappedLines;
615
- switch (wrapMode) {
616
- case WrapMode.BREAK_AT_CHARACTERS: {
617
- wrappedLines = wrapCharByChar(line, width, trim);
618
- break;
619
- }
620
- case WrapMode.BREAK_WORDS: {
621
- wrappedLines = wrapAndBreakWords(line, width, trim);
622
- break;
623
- }
624
- case WrapMode.STRICT_WIDTH: {
625
- wrappedLines = wrapWithBreakAtWidth(line, width, trim);
626
- break;
627
- }
628
- default: {
629
- wrappedLines = wrapWithWordBoundaries(line, width, trim);
630
- }
631
- }
632
- return preserveAnsi(wrappedLines);
633
- });
634
- return result.join("\n");
635
- };
636
-
637
- export { WrapMode, wordWrap };
1
+ import{O as m,P as B,Q as v,S as x,T as H,U as $,V as D,W as N,X as y}from"./packem_shared/constants-aPzGW29R.js";import{getStringWidth as E}from"./get-string-width.js";const O=/\x1B\[(\d+)m/;class T{activeForeground=void 0;activeBackground=void 0;activeFormatting=[];processEscape(t){const i=O.exec(t);if(!i)return;const s=Number.parseInt(i[1],10);switch(s){case 0:{this.activeForeground=void 0,this.activeBackground=void 0,this.activeFormatting=[];break}case 39:{this.activeForeground=void 0;break}case 49:{this.activeBackground=void 0;break}default:if(s>=30&&s<=37||s>=90&&s<=97)this.activeForeground=t;else if(s>=40&&s<=47||s>=100&&s<=107)this.activeBackground=t;else if([1,2,3,4,7,8,9].includes(s))this.activeFormatting.push(t);else if([22,23,24,27,28,29].includes(s)){const u={22:"[1m",23:"[3m",24:"[4m",27:"[7m",28:"[8m",29:"[9m"}[s];u&&(this.activeFormatting=this.activeFormatting.filter(n=>!n.includes(u)))}}}getStartEscapesForAllActiveAttributes(){return[this.activeBackground,this.activeForeground,...this.activeFormatting].filter(Boolean).join("")}getEndEscapesForAllActiveAttributes(){const t=[];if(this.activeFormatting.length>0){const i={"\x1B[1m":"\x1B[22m","\x1B[2m":"\x1B[22m","\x1B[3m":"\x1B[23m","\x1B[4m":"\x1B[24m","\x1B[7m":"\x1B[27m","\x1B[8m":"\x1B[28m","\x1B[9m":"\x1B[29m"};[...this.activeFormatting].toReversed().forEach(s=>{const u=i[s];u&&t.push(u)})}return this.activeForeground&&t.push("\x1B[39m"),this.activeBackground&&t.push("\x1B[49m"),t.join("")}}const P=(e,t)=>{if(!m.has(e[t]))return{isInsideEscape:!1,isInsideLinkEscape:!1};const i=!0,s=e.slice(t+1,t+1+v.length).join("")===v;return{isInsideEscape:i,isInsideLinkEscape:s}},q=(e,t={})=>{const i=new T;let s="",u=!1,n="",r="",o=!1;const a=[...e];for(let l=0;l<a.length;l++){const c=a[l];if(c&&m.has(c)){if(s){const g=t.getWidth?.(s)??0,S={isEscapeSequence:!1,isGrapheme:!0,text:s,width:g};if(o&&(S.isHyperlink=!0,S.hyperlinkUrl=r),t.onSegment?.(S,i)===!1)return;s=""}u=!0,n=c;const p=P(a,l),{isInsideLinkEscape:C}=p;if(C){let g=l+1;for(r="";g<a.length;){const _=a[g];if(_===x)break;r+=_,g+=1}r=r.slice(4);const S={hyperlinkUrl:r,isEscapeSequence:!0,isGrapheme:!1,isHyperlink:!0,isHyperlinkStart:!0,width:0};if(t.onSegment?.(S,i)===!1)return;l=g,o=!0,u=!1,n="";continue}if(l+1<a.length&&a[l+1]==="\\"&&o){const g={isEscapeSequence:!0,isGrapheme:!1,isHyperlink:!0,isHyperlinkEnd:!0,width:0};if(t.onSegment?.(g,i)===!1)return;o=!1,r="",l+=1,u=!1,n="";continue}}if(u){if(n!==c&&(n+=c),c===B){u=!1,i.processEscape(n);const p={isEscapeSequence:!0,isGrapheme:!1,text:n,width:0};if(t.onSegment?.(p,i)===!1)return;n=""}continue}s+=c;const h=t.getWidth?.(s)??0,f={isEscapeSequence:!1,isGrapheme:!0,text:s,width:h};if(o&&(f.isHyperlink=!0,f.hyperlinkUrl=r),t.onSegment?.(f,i)===!1)return;s=""}if(s){const l=t.getWidth?.(s)??0,c={isEscapeSequence:!1,isGrapheme:!0,text:s,width:l};o&&(c.isHyperlink=!0,c.hyperlinkUrl=r),t.onSegment?.(c,i)}if(n){const l={isEscapeSequence:!0,isGrapheme:!1,text:n,width:0};t.onSegment?.(l,i)}},k=e=>`${m.values().next().value}${H}${String(e)}${B}`,F=e=>`${m.values().next().value}${v}${e}${x}`,G=e=>{if(e.length===0)return"";if(e.length===1)return e[0];let t="",i,s;const u=e.join(`
2
+ `),n=[...u];let r=0;for(const[o,a]of n.entries()){if(t+=a,m.has(a)){const c=$.exec(u.slice(r))?.groups??{};if(c.code!==void 0){const h=Number.parseFloat(c.code);i=h===N?void 0:h}else c.uri!==void 0&&(s=c.uri.length===0?void 0:c.uri)}const l=D.get(Number(i));n[o+1]===`
3
+ `?(s&&(t+=F("")),i&&l&&(t+=k(l))):a===`
4
+ `&&(i&&l&&(t+=k(i)),s&&(t+=F(s))),r+=a.length}return t},W=/(?=\s)|(?<=\s)/,b=/^\s+$/,I=new Map,j=e=>{const t=I.get(e);if(t!==void 0)return t;const i=E(e);return I.set(e,i),i},R=e=>{if(!e.includes("\x1B"))return e;let t=e;return e.includes("\x1B[30m")&&(t+="\x1B[39m"),e.includes("\x1B[42m")&&(t+="\x1B[49m"),t},d=e=>{const t=e.split(" ");let i=t.length;for(;i>0&&E(t[i-1])===0;)i--;return i===t.length?e:t.slice(0,i).join(" ")+t.slice(i).join("")},w=(e,t,i)=>{if(e.length===0)return[""];if(t<=0)return[e];const s=[],u=new T;let n="",r=0,o=!1,a=!1,l="",c=0;for(;c<e.length;){const h=e[c];if(m.has(h)){o=!0,l=h,n+=h,a=e.startsWith(v,c+1),c+=1;continue}if(o){l+=h,n+=h,a?h===x&&(o=a=!1):h===B&&(o=!1,u.processEscape(l)),c+=1;continue}const f=j(h),p=h===" ";if(f===0){n+=h,c+=1;continue}if(r+f>t&&(n&&s.push(n+u.getEndEscapesForAllActiveAttributes()),n=u.getStartEscapesForAllActiveAttributes(),r=E(n),p&&i)){for(;c<e.length&&e[c]===" ";)c+=1;continue}if(n+=h,r+=f,r===t&&c<e.length-1&&(s.push(n+u.getEndEscapesForAllActiveAttributes()),n=u.getStartEscapesForAllActiveAttributes(),r=E(n),c+1<e.length&&e[c+1]===" "&&i)){for(c+=1;c<e.length&&e[c]===" ";)c+=1;continue}c+=1}return n&&s.push(n+u.getEndEscapesForAllActiveAttributes()),i?s.map(h=>d(h)):s},K=(e,t,i)=>{if(e.length===0)return[];const s=i?e.trim():e;if(s.length===0)return[];const u=[];let n="",r=0;return q(s,{getWidth:E,onSegment:(o,a)=>{const l=o.text??"";if(o.isEscapeSequence)n+=l;else{const c=l===" ";if(o.width===0)return n+=l,!0;if(r+o.width>t&&(n&&u.push(n),n=a.getStartEscapesForAllActiveAttributes(),r=0,c))return i||u.push(a.getStartEscapesForAllActiveAttributes()+l),!0;n+=l,r+=o.width}return!0}}),n&&u.push(n),i?u.map(o=>d(o)):u},L=(e,t,i)=>{if(e.length===0)return[];const s=i?e.trim():e;if(s.length===0)return[];const u=s.split(W),n=[];let r="",o=0,a=0;for(;a<u.length;){const l=u[a],c=b.test(l),h=E(l);if(l.length===0){a+=1;continue}if(i&&c&&o===0){a+=1;continue}if(o+h>t&&o>0){i?n.push(d(r)):n.push(r),r="",o=0;continue}r+=l,o+=h,a+=1}return r&&(i?n.push(d(r)):n.push(r)),n},U=(e,t,i)=>{if(e.length===0)return[];const s=i?e.trim():e;if(s.length===0)return[];const u=s.split(W),n=[];let r="",o=0,a=0;for(;a<u.length;){const l=u[a],c=b.test(l),h=E(l);if(l.length===0){a+=1;continue}if(i&&c&&o===0){a+=1;continue}if(h>t){r&&n.push(R(i?d(r):r));const f=w(l,t,i);if(f.length>0){for(let p=0;p<f.length-1;p+=1)n.push(f[p]);r=f.at(-1),o=E(r)}else r="",o=0;a+=1;continue}if(o+h>t&&o>0&&(n.push(R(i?d(r):r)),r="",o=0,i&&c)){a+=1;continue}r+=l,o+=h,a+=1}return r&&n.push(R(i?d(r):r)),n},A={BREAK_AT_CHARACTERS:"BREAK_AT_CHARACTERS",BREAK_WORDS:"BREAK_WORDS",PRESERVE_WORDS:"PRESERVE_WORDS",STRICT_WIDTH:"STRICT_WIDTH"},Z=(e,t={})=>{const{removeZeroWidthCharacters:i=!0,trim:s=!0,width:u=80,wrapMode:n=A.PRESERVE_WORDS}=t;if(s&&e.trim()==="")return"";let r=e.normalize("NFC").replaceAll(`\r
5
+ `,`
6
+ `);return i&&(r=r.replaceAll(y,"")),r.split(`
7
+ `).map(o=>{if(s&&o.trim()==="")return"";let a;switch(n){case A.BREAK_AT_CHARACTERS:{a=K(o,u,s);break}case A.BREAK_WORDS:{a=U(o,u,s);break}case A.STRICT_WIDTH:{a=w(o,u,s);break}default:a=L(o,u,s)}return G(a)}).join(`
8
+ `)};export{A as WrapMode,Z as wordWrap};