@visulima/string 1.2.3 → 1.4.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 (102) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +46 -0
  3. package/dist/case/camel-case.cjs +1 -59
  4. package/dist/case/camel-case.mjs +1 -57
  5. package/dist/case/capital-case.cjs +1 -51
  6. package/dist/case/capital-case.mjs +1 -49
  7. package/dist/case/constant-case.cjs +1 -11
  8. package/dist/case/constant-case.mjs +1 -9
  9. package/dist/case/dot-case.cjs +1 -11
  10. package/dist/case/dot-case.mjs +1 -9
  11. package/dist/case/flat-case.cjs +1 -11
  12. package/dist/case/flat-case.mjs +1 -9
  13. package/dist/case/flip-case.cjs +1 -61
  14. package/dist/case/flip-case.mjs +1 -57
  15. package/dist/case/identify-case.cjs +1 -69
  16. package/dist/case/identify-case.mjs +1 -67
  17. package/dist/case/index.cjs +1 -47
  18. package/dist/case/index.mjs +1 -20
  19. package/dist/case/kebab-case.cjs +1 -61
  20. package/dist/case/kebab-case.mjs +1 -57
  21. package/dist/case/lower-first.cjs +1 -13
  22. package/dist/case/lower-first.mjs +1 -11
  23. package/dist/case/no-case.cjs +1 -11
  24. package/dist/case/no-case.mjs +1 -9
  25. package/dist/case/pascal-case.cjs +1 -52
  26. package/dist/case/pascal-case.mjs +1 -50
  27. package/dist/case/pascal-snake-case.cjs +1 -32
  28. package/dist/case/pascal-snake-case.mjs +1 -30
  29. package/dist/case/path-case.cjs +1 -11
  30. package/dist/case/path-case.mjs +1 -9
  31. package/dist/case/sentence-case.cjs +1 -58
  32. package/dist/case/sentence-case.mjs +1 -56
  33. package/dist/case/snake-case.cjs +1 -11
  34. package/dist/case/snake-case.mjs +1 -9
  35. package/dist/case/split-by-case.cjs +1 -641
  36. package/dist/case/split-by-case.mjs +1 -637
  37. package/dist/case/title-case.cjs +1 -47
  38. package/dist/case/title-case.mjs +1 -45
  39. package/dist/case/train-case.cjs +1 -44
  40. package/dist/case/train-case.mjs +1 -42
  41. package/dist/case/upper-first.cjs +1 -13
  42. package/dist/case/upper-first.mjs +1 -11
  43. package/dist/get-string-truncated-width.cjs +1 -422
  44. package/dist/get-string-truncated-width.mjs +1 -418
  45. package/dist/get-string-width.cjs +1 -11
  46. package/dist/get-string-width.mjs +1 -7
  47. package/dist/index.cjs +1 -66
  48. package/dist/index.d.cts +13 -3
  49. package/dist/index.d.mts +13 -3
  50. package/dist/index.d.ts +13 -3
  51. package/dist/index.mjs +1 -29
  52. package/dist/outdent.cjs +1 -125
  53. package/dist/outdent.mjs +1 -121
  54. package/dist/packem_shared/LRUCache-Bjo6gsbC.mjs +1 -0
  55. package/dist/packem_shared/LRUCache-TYqfUYE5.cjs +1 -0
  56. package/dist/packem_shared/WrapMode-BZ3jkEf1.mjs +8 -0
  57. package/dist/packem_shared/WrapMode-DR7vv3px.cjs +8 -0
  58. package/dist/packem_shared/alignText-BCKbnqhC.mjs +2 -0
  59. package/dist/packem_shared/alignText-CLZJuPP6.cjs +2 -0
  60. package/dist/packem_shared/constants-Cd17VNEv.mjs +1 -0
  61. package/dist/packem_shared/constants-D3K8u3G8.cjs +1 -0
  62. package/dist/packem_shared/generate-cache-key-BZf0GiMs.cjs +1 -0
  63. package/dist/packem_shared/generate-cache-key-DPByFaMH.mjs +1 -0
  64. package/dist/packem_shared/joinSegments-CDHNMXsF.cjs +1 -0
  65. package/dist/packem_shared/joinSegments-H7QJ7rwJ.mjs +1 -0
  66. package/dist/packem_shared/normalize-german-eszett-4k1qRtEP.mjs +1 -0
  67. package/dist/packem_shared/normalize-german-eszett-Bop4wOp9.cjs +1 -0
  68. package/dist/packem_shared/replaceString-Ce8TI_-L.cjs +1 -0
  69. package/dist/packem_shared/replaceString-D7R-5VFX.mjs +1 -0
  70. package/dist/packem_shared/slugify-B1vlQQO1.mjs +1 -0
  71. package/dist/packem_shared/slugify-CH3GI7E5.cjs +1 -0
  72. package/dist/packem_shared/transliterate-BXaP38Sh.mjs +5 -0
  73. package/dist/packem_shared/transliterate-CToQ-aeM.cjs +5 -0
  74. package/dist/slice.cjs +1 -419
  75. package/dist/slice.mjs +1 -415
  76. package/dist/test/utils.cjs +13 -74
  77. package/dist/test/utils.mjs +14 -69
  78. package/dist/test/vitest.cjs +1 -11
  79. package/dist/test/vitest.mjs +1 -7
  80. package/dist/truncate.cjs +1 -101
  81. package/dist/truncate.mjs +1 -97
  82. package/dist/utils.cjs +1 -65
  83. package/dist/utils.mjs +1 -56
  84. package/package.json +8 -1
  85. package/dist/packem_shared/LRUCache-Crb-m7cw.cjs +0 -53
  86. package/dist/packem_shared/LRUCache-udNErhWw.mjs +0 -51
  87. package/dist/packem_shared/WrapMode-BCVbMqYK.mjs +0 -545
  88. package/dist/packem_shared/WrapMode-DebG_Nll.cjs +0 -550
  89. package/dist/packem_shared/constants-6-GJ6zA-.cjs +0 -150
  90. package/dist/packem_shared/constants-CKdlkHVU.mjs +0 -99
  91. package/dist/packem_shared/generate-cache-key-D8f1auDB.cjs +0 -10
  92. package/dist/packem_shared/generate-cache-key-YSju5pj2.mjs +0 -8
  93. package/dist/packem_shared/joinSegments-BfSDBmPF.mjs +0 -46
  94. package/dist/packem_shared/joinSegments-HYXurfoq.cjs +0 -48
  95. package/dist/packem_shared/normalize-german-eszett-DJK0S2qj.cjs +0 -11
  96. package/dist/packem_shared/normalize-german-eszett-fpJ4lZL6.mjs +0 -9
  97. package/dist/packem_shared/replaceString-C7Dp5vAg.mjs +0 -200
  98. package/dist/packem_shared/replaceString-iYo1cDfZ.cjs +0 -202
  99. package/dist/packem_shared/slugify-SKjCL8DF.cjs +0 -50
  100. package/dist/packem_shared/slugify-pbxTlzeQ.mjs +0 -48
  101. package/dist/packem_shared/transliterate-4P32b49d.mjs +0 -47780
  102. package/dist/packem_shared/transliterate-CtOTNpLs.cjs +0 -47782
@@ -1,545 +0,0 @@
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 './constants-CKdlkHVU.mjs';
2
- import { getStringWidth } from '../get-string-width.mjs';
3
-
4
- var __defProp$5 = Object.defineProperty;
5
- var __name$5 = (target, value) => __defProp$5(target, "name", { value, configurable: true });
6
- class AnsiStateTracker {
7
- static {
8
- __name$5(this, "AnsiStateTracker");
9
- }
10
- activeForeground = null;
11
- activeBackground = null;
12
- // Track other formatting (bold, italic, etc.)
13
- activeFormatting = [];
14
- /**
15
- * Processes an escape sequence and updates the internal state
16
- * @param sequence - The escape sequence to process
17
- */
18
- // eslint-disable-next-line sonarjs/cognitive-complexity
19
- processEscape(sequence) {
20
- const match = /\x1B\[(\d+)m/.exec(sequence);
21
- if (!match) {
22
- return;
23
- }
24
- const code = Number.parseInt(match[1], 10);
25
- switch (code) {
26
- case 0: {
27
- this.activeForeground = null;
28
- this.activeBackground = null;
29
- this.activeFormatting = [];
30
- break;
31
- }
32
- case 39: {
33
- this.activeForeground = null;
34
- break;
35
- }
36
- case 49: {
37
- this.activeBackground = null;
38
- break;
39
- }
40
- default: {
41
- if (code >= 30 && code <= 37 || code >= 90 && code <= 97) {
42
- this.activeForeground = sequence;
43
- } else if (code >= 40 && code <= 47 || code >= 100 && code <= 107) {
44
- this.activeBackground = sequence;
45
- } else if ([1, 2, 3, 4, 7, 8, 9].includes(code)) {
46
- this.activeFormatting.push(sequence);
47
- } else if ([22, 23, 24, 27, 28, 29].includes(code)) {
48
- const formatResetMap = {
49
- 22: "[1m",
50
- // Reset bold
51
- 23: "[3m",
52
- // Reset italic
53
- 24: "[4m",
54
- // Reset underline
55
- 27: "[7m",
56
- // Reset inverse
57
- 28: "[8m",
58
- // Reset hidden
59
- 29: "[9m"
60
- // Reset strikethrough
61
- };
62
- const formatToRemove = formatResetMap[code];
63
- if (formatToRemove) {
64
- this.activeFormatting = this.activeFormatting.filter((fmt) => !fmt.includes(formatToRemove));
65
- }
66
- }
67
- }
68
- }
69
- }
70
- /**
71
- * Gets all active escape sequences to apply
72
- * @returns String with all active escapes
73
- */
74
- getActiveEscapes() {
75
- return [this.activeBackground, this.activeForeground, ...this.activeFormatting].filter(Boolean).join("");
76
- }
77
- }
78
-
79
- var __defProp$4 = Object.defineProperty;
80
- var __name$4 = (target, value) => __defProp$4(target, "name", { value, configurable: true });
81
- const checkEscapeSequence = /* @__PURE__ */ __name$4((chars, index) => {
82
- if (!ESCAPES.has(chars[index])) {
83
- return { isInsideEscape: false, isInsideLinkEscape: false };
84
- }
85
- const isInsideEscape = true;
86
- const possibleLink = chars.slice(index + 1, index + 1 + ANSI_ESCAPE_LINK.length).join("");
87
- const isInsideLinkEscape = possibleLink === ANSI_ESCAPE_LINK;
88
- return { isInsideEscape, isInsideLinkEscape };
89
- }, "checkEscapeSequence");
90
- const processAnsiString = /* @__PURE__ */ __name$4((string, options = {}) => {
91
- const stateTracker = new AnsiStateTracker();
92
- let currentText = "";
93
- let isInsideEscape = false;
94
- let isInsideLinkEscape = false;
95
- let escapeBuffer = "";
96
- let currentUrl = "";
97
- let isInHyperlink = false;
98
- const chars = [...string];
99
- for (let index = 0; index < chars.length; index++) {
100
- const character = chars[index];
101
- if (character && ESCAPES.has(character)) {
102
- if (currentText) {
103
- const width2 = options.getWidth?.(currentText) ?? 0;
104
- const segment2 = {
105
- isEscapeSequence: false,
106
- isGrapheme: true,
107
- text: currentText,
108
- width: width2
109
- };
110
- if (isInHyperlink) {
111
- segment2.isHyperlink = true;
112
- segment2.hyperlinkUrl = currentUrl;
113
- }
114
- if (options.onSegment?.(segment2, stateTracker) === false) {
115
- return;
116
- }
117
- currentText = "";
118
- }
119
- isInsideEscape = true;
120
- escapeBuffer = character;
121
- const escapeInfo = checkEscapeSequence(chars, index);
122
- isInsideLinkEscape = escapeInfo.isInsideLinkEscape;
123
- if (isInsideLinkEscape) {
124
- let urlEnd = index + 1;
125
- currentUrl = "";
126
- while (urlEnd < chars.length) {
127
- const nextChar = chars[urlEnd];
128
- if (nextChar === ANSI_ESCAPE_BELL) {
129
- break;
130
- }
131
- currentUrl += nextChar;
132
- urlEnd++;
133
- }
134
- currentUrl = currentUrl.slice(4);
135
- const segment2 = {
136
- hyperlinkUrl: currentUrl,
137
- isEscapeSequence: true,
138
- isGrapheme: false,
139
- isHyperlink: true,
140
- isHyperlinkStart: true,
141
- width: 0
142
- };
143
- if (options.onSegment?.(segment2, stateTracker) === false) {
144
- return;
145
- }
146
- index = urlEnd;
147
- isInHyperlink = true;
148
- isInsideEscape = false;
149
- isInsideLinkEscape = false;
150
- escapeBuffer = "";
151
- continue;
152
- }
153
- if (index + 1 < chars.length && chars[index + 1] === "\\" && isInHyperlink) {
154
- const segment2 = {
155
- isEscapeSequence: true,
156
- isGrapheme: false,
157
- isHyperlink: true,
158
- isHyperlinkEnd: true,
159
- width: 0
160
- };
161
- if (options.onSegment?.(segment2, stateTracker) === false) {
162
- return;
163
- }
164
- isInHyperlink = false;
165
- currentUrl = "";
166
- index++;
167
- isInsideEscape = false;
168
- escapeBuffer = "";
169
- continue;
170
- }
171
- }
172
- if (isInsideEscape) {
173
- if (escapeBuffer !== character) {
174
- escapeBuffer += character;
175
- }
176
- if (character === ANSI_SGR_TERMINATOR) {
177
- isInsideEscape = false;
178
- stateTracker.processEscape(escapeBuffer);
179
- const segment2 = {
180
- isEscapeSequence: true,
181
- isGrapheme: false,
182
- text: escapeBuffer,
183
- width: 0
184
- };
185
- if (options.onSegment?.(segment2, stateTracker) === false) {
186
- return;
187
- }
188
- escapeBuffer = "";
189
- }
190
- continue;
191
- }
192
- currentText += character;
193
- const width = options.getWidth?.(currentText) ?? 0;
194
- const segment = {
195
- isEscapeSequence: false,
196
- isGrapheme: true,
197
- text: currentText,
198
- width
199
- };
200
- if (isInHyperlink) {
201
- segment.isHyperlink = true;
202
- segment.hyperlinkUrl = currentUrl;
203
- }
204
- if (options.onSegment?.(segment, stateTracker) === false) {
205
- return;
206
- }
207
- currentText = "";
208
- }
209
- if (currentText) {
210
- const width = options.getWidth?.(currentText) ?? 0;
211
- const segment = {
212
- isEscapeSequence: false,
213
- isGrapheme: true,
214
- text: currentText,
215
- width
216
- };
217
- if (isInHyperlink) {
218
- segment.isHyperlink = true;
219
- segment.hyperlinkUrl = currentUrl;
220
- }
221
- options.onSegment?.(segment, stateTracker);
222
- }
223
- if (escapeBuffer) {
224
- const segment = {
225
- isEscapeSequence: true,
226
- isGrapheme: false,
227
- text: escapeBuffer,
228
- width: 0
229
- };
230
- options.onSegment?.(segment, stateTracker);
231
- }
232
- }, "processAnsiString");
233
-
234
- var __defProp$3 = Object.defineProperty;
235
- var __name$3 = (target, value) => __defProp$3(target, "name", { value, configurable: true });
236
- const wrapAnsiCode = /* @__PURE__ */ __name$3((code) => {
237
- const escapeChar = ESCAPES.values().next().value;
238
- return `${escapeChar}${ANSI_CSI}${code}${ANSI_SGR_TERMINATOR}`;
239
- }, "wrapAnsiCode");
240
-
241
- var __defProp$2 = Object.defineProperty;
242
- var __name$2 = (target, value) => __defProp$2(target, "name", { value, configurable: true });
243
- const wrapAnsiHyperlink = /* @__PURE__ */ __name$2((url) => {
244
- const escapeChar = ESCAPES.values().next().value;
245
- return `${escapeChar}${ANSI_ESCAPE_LINK}${url}${ANSI_ESCAPE_BELL}`;
246
- }, "wrapAnsiHyperlink");
247
-
248
- var __defProp$1 = Object.defineProperty;
249
- var __name$1 = (target, value) => __defProp$1(target, "name", { value, configurable: true });
250
- const preserveAnsi = /* @__PURE__ */ __name$1((rawLines) => {
251
- if (rawLines.length === 0) {
252
- return "";
253
- }
254
- if (rawLines.length === 1) {
255
- return rawLines[0];
256
- }
257
- let returnValue = "";
258
- let escapeCode;
259
- let escapeUrl;
260
- const preString = rawLines.join("\n");
261
- const pre = [...preString];
262
- let preStringIndex = 0;
263
- for (const [index, character] of pre.entries()) {
264
- returnValue += character;
265
- if (ESCAPES.has(character)) {
266
- const match = RE_ESCAPE_PATTERN.exec(preString.slice(preStringIndex));
267
- const groups = match?.groups ?? {};
268
- if (groups.code !== void 0) {
269
- const code2 = Number.parseFloat(groups.code);
270
- escapeCode = code2 === END_CODE ? void 0 : code2;
271
- } else if (groups.uri !== void 0) {
272
- escapeUrl = groups.uri.length === 0 ? void 0 : groups.uri;
273
- }
274
- }
275
- const code = ANSI_RESET_CODES.get(Number(escapeCode));
276
- if (pre[index + 1] === "\n") {
277
- if (escapeUrl) {
278
- returnValue += wrapAnsiHyperlink("");
279
- }
280
- if (escapeCode && code) {
281
- returnValue += wrapAnsiCode(code);
282
- }
283
- } else if (character === "\n") {
284
- if (escapeCode && code) {
285
- returnValue += wrapAnsiCode(escapeCode);
286
- }
287
- if (escapeUrl) {
288
- returnValue += wrapAnsiHyperlink(escapeUrl);
289
- }
290
- }
291
- preStringIndex += character.length;
292
- }
293
- return returnValue;
294
- }, "preserveAnsi");
295
-
296
- var __defProp = Object.defineProperty;
297
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
298
- const resetAnsiAtLineBreak = /* @__PURE__ */ __name((currentLine) => {
299
- if (!currentLine.includes("\x1B")) {
300
- return currentLine;
301
- }
302
- let result = currentLine;
303
- if (currentLine.includes("\x1B[30m")) {
304
- result += "\x1B[39m";
305
- }
306
- if (currentLine.includes("\x1B[42m")) {
307
- result += "\x1B[49m";
308
- }
309
- return result;
310
- }, "resetAnsiAtLineBreak");
311
- const stringVisibleTrimSpacesRight = /* @__PURE__ */ __name((string) => {
312
- const words = string.split(" ");
313
- let last = words.length;
314
- while (last > 0 && getStringWidth(words[last - 1]) === 0) {
315
- last--;
316
- }
317
- if (last === words.length) {
318
- return string;
319
- }
320
- return words.slice(0, last).join(" ") + words.slice(last).join("");
321
- }, "stringVisibleTrimSpacesRight");
322
- const wrapWithBreakAtWidth = /* @__PURE__ */ __name((string, width, trim) => {
323
- if (string.length === 0) {
324
- return [""];
325
- }
326
- if (width <= 0) {
327
- return [string];
328
- }
329
- const rows = [];
330
- let currentLine = "";
331
- let currentWidth = 0;
332
- const ansiTracker = new AnsiStateTracker();
333
- let isInsideEscape = false;
334
- let isInsideLinkEscape = false;
335
- let escapeBuffer = "";
336
- for (let index = 0; index < string.length; index++) {
337
- const char = string[index];
338
- if (ESCAPES.has(char)) {
339
- isInsideEscape = true;
340
- escapeBuffer = char;
341
- currentLine += char;
342
- const escapeInfo = checkEscapeSequence([...string], index);
343
- isInsideLinkEscape = escapeInfo.isInsideLinkEscape;
344
- continue;
345
- }
346
- if (isInsideEscape) {
347
- escapeBuffer += char;
348
- currentLine += char;
349
- if (isInsideLinkEscape) {
350
- if (char === ANSI_ESCAPE_BELL) {
351
- isInsideEscape = isInsideLinkEscape = false;
352
- }
353
- } else if (char === ANSI_SGR_TERMINATOR) {
354
- isInsideEscape = false;
355
- ansiTracker.processEscape(escapeBuffer);
356
- }
357
- continue;
358
- }
359
- const charWidth = getStringWidth(char);
360
- const isSpace = char === " ";
361
- if (charWidth === 0) {
362
- currentLine += char;
363
- continue;
364
- }
365
- if (currentWidth + charWidth > width) {
366
- if (currentLine) {
367
- rows.push(resetAnsiAtLineBreak(currentLine));
368
- }
369
- currentLine = ansiTracker.getActiveEscapes();
370
- currentWidth = 0;
371
- if (isSpace && trim) {
372
- while (index < string.length && string[index] === " ") {
373
- index++;
374
- if (index >= string.length) {
375
- break;
376
- }
377
- }
378
- if (index < string.length) {
379
- index--;
380
- }
381
- continue;
382
- }
383
- }
384
- currentLine += char;
385
- currentWidth += charWidth;
386
- if (currentWidth === width && index < string.length - 1) {
387
- rows.push(resetAnsiAtLineBreak(currentLine));
388
- currentLine = ansiTracker.getActiveEscapes();
389
- currentWidth = 0;
390
- if (index + 1 < string.length && string[index + 1] === " " && trim) {
391
- index++;
392
- while (index < string.length && string[index] === " ") {
393
- index++;
394
- }
395
- index--;
396
- }
397
- }
398
- }
399
- if (currentLine) {
400
- rows.push(currentLine);
401
- }
402
- return trim ? rows.map((element) => stringVisibleTrimSpacesRight(element)) : rows;
403
- }, "wrapWithBreakAtWidth");
404
- const wrapCharByChar = /* @__PURE__ */ __name((string, width, trim) => {
405
- if (string.length === 0) {
406
- return [];
407
- }
408
- const inputToProcess = trim ? string.trim() : string;
409
- if (inputToProcess.length === 0) {
410
- return [];
411
- }
412
- const rows = [];
413
- let currentLine = "";
414
- let currentWidth = 0;
415
- processAnsiString(inputToProcess, {
416
- getWidth: getStringWidth,
417
- onSegment: /* @__PURE__ */ __name((segment, stateTracker) => {
418
- if (segment.isEscapeSequence) {
419
- currentLine += segment.text;
420
- } else {
421
- const isSpace = segment.text === " ";
422
- if (segment.width === 0) {
423
- currentLine += segment.text;
424
- return true;
425
- }
426
- if (currentWidth + segment.width > width) {
427
- if (currentLine) {
428
- rows.push(resetAnsiAtLineBreak(currentLine));
429
- }
430
- currentLine = stateTracker.getActiveEscapes();
431
- currentWidth = 0;
432
- if (isSpace) {
433
- if (trim) {
434
- return true;
435
- }
436
- rows.push(stateTracker.getActiveEscapes() + segment.text);
437
- return true;
438
- }
439
- }
440
- currentLine += segment.text;
441
- currentWidth += segment.width;
442
- }
443
- return true;
444
- }, "onSegment")
445
- });
446
- if (currentLine) {
447
- rows.push(currentLine);
448
- }
449
- return trim ? rows.map((row) => stringVisibleTrimSpacesRight(row)) : rows;
450
- }, "wrapCharByChar");
451
- const wrapWithWordBoundaries = /* @__PURE__ */ __name((string, width, trim) => {
452
- if (string.length === 0) {
453
- return [];
454
- }
455
- const inputToProcess = trim ? string.trim() : string;
456
- if (inputToProcess.length === 0) {
457
- return [];
458
- }
459
- const tokens = inputToProcess.split(/(?=\s)|(?<=\s)/);
460
- const rows = [];
461
- let currentLine = "";
462
- let currentWidth = 0;
463
- let index = 0;
464
- while (index < tokens.length) {
465
- const token = tokens[index];
466
- const isSpace = /^\s+$/.test(token);
467
- const tokenVisibleWidth = getStringWidth(token);
468
- if (token.length === 0) {
469
- index++;
470
- continue;
471
- }
472
- if (trim && isSpace && currentWidth === 0) {
473
- index++;
474
- continue;
475
- }
476
- if (currentWidth + tokenVisibleWidth > width && currentWidth > 0) {
477
- if (trim) {
478
- rows.push(stringVisibleTrimSpacesRight(currentLine));
479
- } else {
480
- rows.push(currentLine);
481
- }
482
- currentLine = "";
483
- currentWidth = 0;
484
- continue;
485
- }
486
- currentLine += token;
487
- currentWidth += tokenVisibleWidth;
488
- index++;
489
- }
490
- if (currentLine) {
491
- if (trim) {
492
- rows.push(stringVisibleTrimSpacesRight(currentLine));
493
- } else {
494
- rows.push(currentLine);
495
- }
496
- }
497
- return rows;
498
- }, "wrapWithWordBoundaries");
499
- const WrapMode = {
500
- /**
501
- * Breaks words at character boundaries to fit the width
502
- */
503
- BREAK_AT_CHARACTERS: "BREAK_AT_CHARACTERS",
504
- /**
505
- * Preserves word boundaries, words are kept intact even if they exceed width
506
- */
507
- PRESERVE_WORDS: "PRESERVE_WORDS",
508
- /**
509
- * Enforces strict adherence to the width limit by breaking at exact width
510
- */
511
- STRICT_WIDTH: "STRICT_WIDTH"
512
- };
513
- const wordWrap = /* @__PURE__ */ __name((string, options = {}) => {
514
- const { removeZeroWidthCharacters = true, trim = true, width = 80, wrapMode = WrapMode.PRESERVE_WORDS } = options;
515
- if (trim && string.trim() === "") {
516
- return "";
517
- }
518
- let normalizedString = String(string).normalize("NFC").replaceAll("\r\n", "\n");
519
- if (removeZeroWidthCharacters) {
520
- normalizedString = normalizedString.replaceAll(RE_ZERO_WIDTH, "");
521
- }
522
- const result = normalizedString.split("\n").map((line) => {
523
- if (trim && line.trim() === "") {
524
- return "";
525
- }
526
- let wrappedLines;
527
- switch (wrapMode) {
528
- case WrapMode.STRICT_WIDTH: {
529
- wrappedLines = wrapWithBreakAtWidth(line, width, trim);
530
- break;
531
- }
532
- case WrapMode.BREAK_AT_CHARACTERS: {
533
- wrappedLines = wrapCharByChar(line, width, trim);
534
- break;
535
- }
536
- default: {
537
- wrappedLines = wrapWithWordBoundaries(line, width, trim);
538
- }
539
- }
540
- return preserveAnsi(wrappedLines);
541
- });
542
- return result.join("\n");
543
- }, "wordWrap");
544
-
545
- export { WrapMode, wordWrap };