@synstack/str 1.1.2 → 1.2.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.
- package/README.md +142 -1
- package/dist/str.index.cjs +305 -44
- package/dist/str.index.cjs.map +1 -1
- package/dist/str.index.d.cts +528 -72
- package/dist/str.index.d.ts +528 -72
- package/dist/str.index.js +303 -44
- package/dist/str.index.js.map +1 -1
- package/package.json +6 -6
- package/src/str.chainable.ts +325 -44
- package/src/str.lib.ts +230 -27
package/src/str.lib.ts
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
/**
|
2
2
|
* Remove empty lines at the start of the text but leave whitespace on the first line with content
|
3
|
+
* @param text - The input string to process
|
4
|
+
* @returns The string with empty lines removed from the start
|
5
|
+
* @example
|
6
|
+
* ```typescript
|
7
|
+
* chopEmptyLinesStart("\n\n \n Hello") // " Hello"
|
8
|
+
* chopEmptyLinesStart(" Hello") // " Hello"
|
9
|
+
* ```
|
3
10
|
*/
|
4
11
|
export const chopEmptyLinesStart = (text: string) => {
|
5
12
|
return text.replace(/^(\s*\n)+/, "");
|
@@ -7,48 +14,100 @@ export const chopEmptyLinesStart = (text: string) => {
|
|
7
14
|
|
8
15
|
/**
|
9
16
|
* Remove empty lines at the end of the text but leave whitespace on the last line with content
|
17
|
+
* @param text - The input string to process
|
18
|
+
* @returns The string with empty lines removed from the end
|
19
|
+
* @example
|
20
|
+
* ```typescript
|
21
|
+
* chopEmptyLinesEnd("Hello\n\n \n") // "Hello"
|
22
|
+
* chopEmptyLinesEnd("Hello ") // "Hello "
|
23
|
+
* ```
|
10
24
|
*/
|
11
25
|
export const chopEmptyLinesEnd = (text: string) => {
|
12
26
|
return text.replace(/(\n\s*)+$/, "");
|
13
27
|
};
|
14
28
|
|
15
29
|
/**
|
16
|
-
* Remove all space
|
30
|
+
* Remove all space characters in lines that contain no content
|
31
|
+
* @param text - The input string to process
|
32
|
+
* @returns The string with whitespace removed from empty lines
|
33
|
+
* @example
|
34
|
+
* ```typescript
|
35
|
+
* trimEmptyLines("Hello\n \nWorld") // "Hello\n\nWorld"
|
36
|
+
* trimEmptyLines(" \n \n ") // "\n\n"
|
37
|
+
* ```
|
17
38
|
*/
|
18
39
|
export const trimEmptyLines = (text: string) => {
|
19
40
|
return text.replace(/(^|\n)\s+(\n|$)/g, "$1$2");
|
20
41
|
};
|
21
42
|
|
22
43
|
/**
|
23
|
-
* Remove all space
|
44
|
+
* Remove all space characters at the end of each line
|
45
|
+
* @param text - The input string to process
|
46
|
+
* @returns The string with trailing spaces removed from all lines
|
47
|
+
* @example
|
48
|
+
* ```typescript
|
49
|
+
* trimLinesTrailingSpaces("Hello \nWorld ") // "Hello\nWorld"
|
50
|
+
* trimLinesTrailingSpaces("Hello ") // "Hello"
|
51
|
+
* ```
|
24
52
|
*/
|
25
53
|
export const trimLinesTrailingSpaces = (text: string) => {
|
26
54
|
return text.replace(/ +(\n|$)/g, "$1");
|
27
55
|
};
|
28
56
|
|
29
57
|
/**
|
30
|
-
*
|
58
|
+
* Remove leading and trailing whitespace and line terminator characters
|
59
|
+
* @param text - The input string to process
|
60
|
+
* @returns The trimmed string
|
61
|
+
* @example
|
62
|
+
* ```typescript
|
63
|
+
* trim(" Hello World ") // "Hello World"
|
64
|
+
* trim("\n Hello \n") // "Hello"
|
65
|
+
* ```
|
31
66
|
*/
|
32
67
|
export const trim = (text: string) => {
|
33
68
|
return text.trim();
|
34
69
|
};
|
35
70
|
|
36
71
|
/**
|
37
|
-
*
|
72
|
+
* Remove leading whitespace and line terminator characters
|
73
|
+
* @param text - The input string to process
|
74
|
+
* @returns The string with leading whitespace removed
|
75
|
+
* @example
|
76
|
+
* ```typescript
|
77
|
+
* trimStart(" Hello World ") // "Hello World "
|
78
|
+
* trimStart("\n Hello") // "Hello"
|
79
|
+
* ```
|
38
80
|
*/
|
39
81
|
export const trimStart = (text: string) => {
|
40
82
|
return text.trimStart();
|
41
83
|
};
|
42
84
|
|
43
85
|
/**
|
44
|
-
*
|
86
|
+
* Remove trailing whitespace and line terminator characters
|
87
|
+
* @param text - The input string to process
|
88
|
+
* @returns The string with trailing whitespace removed
|
89
|
+
* @example
|
90
|
+
* ```typescript
|
91
|
+
* trimEnd(" Hello World ") // " Hello World"
|
92
|
+
* trimEnd("Hello \n") // "Hello"
|
93
|
+
* ```
|
45
94
|
*/
|
46
95
|
export const trimEnd = (text: string) => {
|
47
96
|
return text.trimEnd();
|
48
97
|
};
|
49
98
|
|
50
99
|
/**
|
51
|
-
* Split a string into substrings using the specified separator
|
100
|
+
* Split a string into substrings using the specified separator
|
101
|
+
* @param text - The input string to split
|
102
|
+
* @param separator - The string or regular expression to use for splitting
|
103
|
+
* @param limit - Optional limit on the number of splits to perform
|
104
|
+
* @returns An array of substrings
|
105
|
+
* @example
|
106
|
+
* ```typescript
|
107
|
+
* split("Hello World", " ") // ["Hello", "World"]
|
108
|
+
* split("a,b,c", ",", 2) // ["a", "b"]
|
109
|
+
* split("a.b-c", /[.-]/) // ["a", "b", "c"]
|
110
|
+
* ```
|
52
111
|
*/
|
53
112
|
export const split = (
|
54
113
|
text: string,
|
@@ -59,10 +118,15 @@ export const split = (
|
|
59
118
|
};
|
60
119
|
|
61
120
|
/**
|
62
|
-
* Add line numbers to a string
|
63
|
-
* @param text The string to add line numbers to
|
64
|
-
* @param separator The separator
|
65
|
-
*
|
121
|
+
* Add line numbers to each line in a string
|
122
|
+
* @param text - The string to add line numbers to
|
123
|
+
* @param separator - The separator between line number and content (defaults to ":")
|
124
|
+
* @returns The string with line numbers added
|
125
|
+
* @example
|
126
|
+
* ```typescript
|
127
|
+
* addLineNumbers("Hello\nWorld") // "0:Hello\n1:World"
|
128
|
+
* addLineNumbers("A\nB\nC", " -> ") // "0 -> A\n1 -> B\n2 -> C"
|
129
|
+
* ```
|
66
130
|
*/
|
67
131
|
export const addLineNumbers = (text: string, separator: string = ":") => {
|
68
132
|
return text
|
@@ -72,7 +136,15 @@ export const addLineNumbers = (text: string, separator: string = ":") => {
|
|
72
136
|
};
|
73
137
|
|
74
138
|
/**
|
75
|
-
*
|
139
|
+
* Calculate the minimum indentation level of non-empty lines in the string
|
140
|
+
* @param text - The input string to analyze
|
141
|
+
* @returns The number of spaces in the minimum indentation level, or 0 if no indentation found
|
142
|
+
* @example
|
143
|
+
* ```typescript
|
144
|
+
* indentation(" Hello\n World") // 2
|
145
|
+
* indentation("Hello\n World") // 0
|
146
|
+
* indentation(" \n Hello") // 4
|
147
|
+
* ```
|
76
148
|
*/
|
77
149
|
export const indentation = (text: string) => {
|
78
150
|
return (
|
@@ -86,9 +158,16 @@ export const indentation = (text: string) => {
|
|
86
158
|
};
|
87
159
|
|
88
160
|
/**
|
89
|
-
* Indent the string by
|
90
|
-
* @param
|
91
|
-
* @param
|
161
|
+
* Indent each line of the string by a specified number of spaces
|
162
|
+
* @param text - The input string to indent
|
163
|
+
* @param size - The number of spaces to add at the start of each line
|
164
|
+
* @param char - The character to use for indentation (defaults to space)
|
165
|
+
* @returns The indented string
|
166
|
+
* @example
|
167
|
+
* ```typescript
|
168
|
+
* indent("Hello\nWorld", 2) // " Hello\n World"
|
169
|
+
* indent("A\nB", 3, "-") // "---A\n---B"
|
170
|
+
* ```
|
92
171
|
*/
|
93
172
|
export const indent = (text: string, size: number, char: string = " ") => {
|
94
173
|
if (size === 0) return text;
|
@@ -101,9 +180,15 @@ export const indent = (text: string, size: number, char: string = " ") => {
|
|
101
180
|
};
|
102
181
|
|
103
182
|
/**
|
104
|
-
*
|
105
|
-
* @param
|
106
|
-
*
|
183
|
+
* Remove indentation from each line of the string
|
184
|
+
* @param text - The input string to dedent
|
185
|
+
* @param size - Optional number of spaces to remove. If not provided, removes the minimum common indentation
|
186
|
+
* @returns The dedented string
|
187
|
+
* @example
|
188
|
+
* ```typescript
|
189
|
+
* dedent(" Hello\n World") // "Hello\n World"
|
190
|
+
* dedent(" A\n B", 2) // " A\nB"
|
191
|
+
* ```
|
107
192
|
*/
|
108
193
|
export const dedent = (text: string, size?: number) => {
|
109
194
|
const _size = size ?? indentation(text);
|
@@ -116,7 +201,15 @@ export const dedent = (text: string, size?: number) => {
|
|
116
201
|
};
|
117
202
|
|
118
203
|
/**
|
119
|
-
*
|
204
|
+
* Remove a specified number of characters from the end of the string
|
205
|
+
* @param text - The input string to process
|
206
|
+
* @param count - The number of characters to remove
|
207
|
+
* @returns The string with characters removed from the end
|
208
|
+
* @example
|
209
|
+
* ```typescript
|
210
|
+
* chopEnd("Hello World", 6) // "Hello"
|
211
|
+
* chopEnd("Hello", 0) // "Hello"
|
212
|
+
* ```
|
120
213
|
*/
|
121
214
|
export const chopEnd = (text: string, count: number) => {
|
122
215
|
if (count === 0) return text;
|
@@ -124,7 +217,15 @@ export const chopEnd = (text: string, count: number) => {
|
|
124
217
|
};
|
125
218
|
|
126
219
|
/**
|
127
|
-
*
|
220
|
+
* Remove a specified number of characters from the start of the string
|
221
|
+
* @param text - The input string to process
|
222
|
+
* @param count - The number of characters to remove
|
223
|
+
* @returns The string with characters removed from the start
|
224
|
+
* @example
|
225
|
+
* ```typescript
|
226
|
+
* chopStart("Hello World", 6) // "World"
|
227
|
+
* chopStart("Hello", 0) // "Hello"
|
228
|
+
* ```
|
128
229
|
*/
|
129
230
|
export const chopStart = (text: string, count: number) => {
|
130
231
|
if (count === 0) return text;
|
@@ -132,8 +233,15 @@ export const chopStart = (text: string, count: number) => {
|
|
132
233
|
};
|
133
234
|
|
134
235
|
/**
|
135
|
-
*
|
136
|
-
* @param
|
236
|
+
* Limit the number of consecutive newlines in the string
|
237
|
+
* @param text - The input string to process
|
238
|
+
* @param maxRepeat - The maximum number of consecutive newlines to allow
|
239
|
+
* @returns The string with consecutive newlines limited
|
240
|
+
* @example
|
241
|
+
* ```typescript
|
242
|
+
* chopRepeatNewlines("A\n\n\n\nB", 2) // "A\n\nB"
|
243
|
+
* chopRepeatNewlines("A\n\nB", 1) // "A\nB"
|
244
|
+
* ```
|
137
245
|
*/
|
138
246
|
export const chopRepeatNewlines = (text: string, maxRepeat: number) => {
|
139
247
|
if (maxRepeat === 0) return text;
|
@@ -144,43 +252,138 @@ export const chopRepeatNewlines = (text: string, maxRepeat: number) => {
|
|
144
252
|
};
|
145
253
|
|
146
254
|
/**
|
147
|
-
*
|
255
|
+
* Extract a specified number of characters from the start of the string
|
256
|
+
* @param text - The input string to process
|
257
|
+
* @param count - The number of characters to take
|
258
|
+
* @returns The extracted substring from the start
|
259
|
+
* @example
|
260
|
+
* ```typescript
|
261
|
+
* takeStart("Hello World", 5) // "Hello"
|
262
|
+
* takeStart("Hi", 5) // "Hi"
|
263
|
+
* ```
|
148
264
|
*/
|
149
265
|
export const takeStart = (text: string, count: number) => {
|
150
266
|
return text.slice(0, count);
|
151
267
|
};
|
152
268
|
|
153
269
|
/**
|
154
|
-
*
|
270
|
+
* Extract a specified number of characters from the end of the string
|
271
|
+
* @param text - The input string to process
|
272
|
+
* @param count - The number of characters to take
|
273
|
+
* @returns The extracted substring from the end
|
274
|
+
* @example
|
275
|
+
* ```typescript
|
276
|
+
* takeEnd("Hello World", 5) // "World"
|
277
|
+
* takeEnd("Hi", 5) // "Hi"
|
278
|
+
* ```
|
155
279
|
*/
|
156
280
|
export const takeEnd = (text: string, count: number) => {
|
157
281
|
return text.slice(-count);
|
158
282
|
};
|
159
283
|
|
160
284
|
/**
|
161
|
-
*
|
285
|
+
* Get the last line of a multi-line string
|
286
|
+
* @param text - The input string to process
|
287
|
+
* @returns The last line of the string, or empty string if input is empty
|
288
|
+
* @example
|
289
|
+
* ```typescript
|
290
|
+
* lastLine("Hello\nWorld") // "World"
|
291
|
+
* lastLine("Hello") // "Hello"
|
292
|
+
* lastLine("") // ""
|
293
|
+
* ```
|
162
294
|
*/
|
163
295
|
export const lastLine = (text: string) => {
|
164
296
|
return text.split("\n").at(-1) ?? "";
|
165
297
|
};
|
166
298
|
|
167
299
|
/**
|
168
|
-
*
|
300
|
+
* Get the first line of a multi-line string
|
301
|
+
* @param text - The input string to process
|
302
|
+
* @returns The first line of the string, or empty string if input is empty
|
303
|
+
* @example
|
304
|
+
* ```typescript
|
305
|
+
* firstLine("Hello\nWorld") // "Hello"
|
306
|
+
* firstLine("Hello") // "Hello"
|
307
|
+
* firstLine("") // ""
|
308
|
+
* ```
|
169
309
|
*/
|
170
310
|
export const firstLine = (text: string) => {
|
171
311
|
return text.split("\n").at(0) ?? "";
|
172
312
|
};
|
173
313
|
|
174
314
|
/**
|
175
|
-
*
|
315
|
+
* Count the number of leading space characters in a string
|
316
|
+
* @param text - The input string to analyze
|
317
|
+
* @returns The number of leading space characters
|
318
|
+
* @example
|
319
|
+
* ```typescript
|
320
|
+
* leadingSpacesCount(" Hello") // 2
|
321
|
+
* leadingSpacesCount("Hello") // 0
|
322
|
+
* ```
|
176
323
|
*/
|
177
324
|
export const leadingSpacesCount = (text: string) => {
|
178
325
|
return text.match(/^\s+/)?.at(0)?.length ?? 0;
|
179
326
|
};
|
180
327
|
|
181
328
|
/**
|
182
|
-
*
|
329
|
+
* Check if a string is empty or contains only whitespace
|
330
|
+
* @param text - The input string to check
|
331
|
+
* @returns True if the string is empty or contains only whitespace, false otherwise
|
332
|
+
* @example
|
333
|
+
* ```typescript
|
334
|
+
* isEmpty("") // true
|
335
|
+
* isEmpty(" \n ") // true
|
336
|
+
* isEmpty("Hello") // false
|
337
|
+
* ```
|
183
338
|
*/
|
184
339
|
export const isEmpty = (text: string) => {
|
185
340
|
return text.trim() === "";
|
186
341
|
};
|
342
|
+
|
343
|
+
/**
|
344
|
+
* Replace the first occurrence of a substring or pattern in the string
|
345
|
+
* @param text - The input string to process
|
346
|
+
* @param searchValue - The string or pattern to search for
|
347
|
+
* @param replaceValue - The string to replace the match with
|
348
|
+
* @returns The string with the first match replaced
|
349
|
+
* @example
|
350
|
+
* ```typescript
|
351
|
+
* replace("Hello World", "o", "0") // "Hell0 World"
|
352
|
+
* replace("abc abc", /[a-z]/, "X") // "Xbc abc"
|
353
|
+
* ```
|
354
|
+
*/
|
355
|
+
export const replace = (
|
356
|
+
text: string,
|
357
|
+
searchValue: string | RegExp,
|
358
|
+
replaceValue: string,
|
359
|
+
) => {
|
360
|
+
return text.replace(searchValue, replaceValue);
|
361
|
+
};
|
362
|
+
|
363
|
+
/**
|
364
|
+
* Replace all occurrences of a substring or pattern in the string
|
365
|
+
* @param text - The input string to process
|
366
|
+
* @param searchValue - The string or pattern to search for
|
367
|
+
* @param replaceValue - The string to replace the matches with
|
368
|
+
* @returns The string with all matches replaced
|
369
|
+
* @example
|
370
|
+
* ```typescript
|
371
|
+
* replaceAll("Hello World", "o", "0") // "Hell0 W0rld"
|
372
|
+
* replaceAll("abc abc", /[a-z]/g, "X") // "XXX XXX"
|
373
|
+
* ```
|
374
|
+
*/
|
375
|
+
export const replaceAll = (
|
376
|
+
text: string,
|
377
|
+
searchValue: string | RegExp,
|
378
|
+
replaceValue: string,
|
379
|
+
) => {
|
380
|
+
if (typeof searchValue === "string") {
|
381
|
+
return text.replaceAll(searchValue, replaceValue);
|
382
|
+
}
|
383
|
+
// Ensure the RegExp has the global flag
|
384
|
+
const flags = searchValue.flags.includes("g")
|
385
|
+
? searchValue.flags
|
386
|
+
: searchValue.flags + "g";
|
387
|
+
const globalRegex = new RegExp(searchValue.source, flags);
|
388
|
+
return text.replace(globalRegex, replaceValue);
|
389
|
+
};
|