@synstack/str 1.0.1-alpha.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +1 -0
- package/dist/str.index.cjs +441 -0
- package/dist/str.index.cjs.map +1 -0
- package/dist/str.index.d.cts +261 -0
- package/dist/str.index.d.ts +261 -0
- package/dist/str.index.js +384 -0
- package/dist/str.index.js.map +1 -0
- package/package.json +66 -0
- package/src/str.bundle.ts +16 -0
- package/src/str.chainable.ts +291 -0
- package/src/str.index.ts +2 -0
- package/src/str.lib.ts +186 -0
@@ -0,0 +1,384 @@
|
|
1
|
+
// src/str.bundle.ts
|
2
|
+
import {
|
3
|
+
camelCase as camelCase2,
|
4
|
+
capitalCase as capitalCase2,
|
5
|
+
constantCase as constantCase2,
|
6
|
+
dotCase as dotCase2,
|
7
|
+
kebabCase as kebabCase2,
|
8
|
+
noCase as noCase2,
|
9
|
+
pascalCase as pascalCase2,
|
10
|
+
pascalSnakeCase as pascalSnakeCase2,
|
11
|
+
pathCase as pathCase2,
|
12
|
+
sentenceCase as sentenceCase2,
|
13
|
+
snakeCase as snakeCase2,
|
14
|
+
trainCase as trainCase2
|
15
|
+
} from "change-case";
|
16
|
+
|
17
|
+
// src/str.chainable.ts
|
18
|
+
import { Pipeable } from "@synstack/pipe";
|
19
|
+
import * as changeCase from "change-case";
|
20
|
+
|
21
|
+
// src/str.lib.ts
|
22
|
+
var chopEmptyLinesStart = (text) => {
|
23
|
+
return text.replace(/^(\s*\n)+/, "");
|
24
|
+
};
|
25
|
+
var chopEmptyLinesEnd = (text) => {
|
26
|
+
return text.replace(/(\n\s*)+$/, "");
|
27
|
+
};
|
28
|
+
var trimEmptyLines = (text) => {
|
29
|
+
return text.replace(/(^|\n)\s+(\n|$)/g, "$1$2");
|
30
|
+
};
|
31
|
+
var trimLinesTrailingSpaces = (text) => {
|
32
|
+
return text.replace(/ +(\n|$)/g, "$1");
|
33
|
+
};
|
34
|
+
var trim = (text) => {
|
35
|
+
return text.trim();
|
36
|
+
};
|
37
|
+
var trimStart = (text) => {
|
38
|
+
return text.trimStart();
|
39
|
+
};
|
40
|
+
var trimEnd = (text) => {
|
41
|
+
return text.trimEnd();
|
42
|
+
};
|
43
|
+
var split = (text, separator, limit) => {
|
44
|
+
return text.split(separator, limit);
|
45
|
+
};
|
46
|
+
var addLineNumbers = (text, separator = ":") => {
|
47
|
+
return text.split("\n").map((line, index) => `${index}${separator}${line}`).join("\n");
|
48
|
+
};
|
49
|
+
var indentation = (text) => {
|
50
|
+
return text.split("\n").reduce((acc, line) => {
|
51
|
+
if (line.trim() === "") return acc;
|
52
|
+
const indentation2 = leadingSpacesCount(line);
|
53
|
+
if (acc === null) return indentation2;
|
54
|
+
return Math.min(acc, indentation2);
|
55
|
+
}, null) ?? 0;
|
56
|
+
};
|
57
|
+
var indent = (text, size, char = " ") => {
|
58
|
+
if (size === 0) return text;
|
59
|
+
const indentStr = char.repeat(size);
|
60
|
+
return text.split("\n").map((line) => indentStr + line).join("\n");
|
61
|
+
};
|
62
|
+
var dedent = (text, size) => {
|
63
|
+
const _size = size ?? indentation(text);
|
64
|
+
if (_size === 0) return text;
|
65
|
+
const regex = new RegExp(`^\\s{1,${_size}}`);
|
66
|
+
return text.split("\n").map((line) => line.replace(regex, "")).join("\n");
|
67
|
+
};
|
68
|
+
var chopEnd = (text, count) => {
|
69
|
+
if (count === 0) return text;
|
70
|
+
return text.slice(0, -count);
|
71
|
+
};
|
72
|
+
var chopStart = (text, count) => {
|
73
|
+
if (count === 0) return text;
|
74
|
+
return text.slice(count);
|
75
|
+
};
|
76
|
+
var chopRepeatNewlines = (text, maxRepeat) => {
|
77
|
+
if (maxRepeat === 0) return text;
|
78
|
+
return text.replace(
|
79
|
+
new RegExp(`
|
80
|
+
{${maxRepeat + 1},}`, "g"),
|
81
|
+
"\n".repeat(maxRepeat)
|
82
|
+
);
|
83
|
+
};
|
84
|
+
var takeStart = (text, count) => {
|
85
|
+
return text.slice(0, count);
|
86
|
+
};
|
87
|
+
var takeEnd = (text, count) => {
|
88
|
+
return text.slice(-count);
|
89
|
+
};
|
90
|
+
var lastLine = (text) => {
|
91
|
+
return text.split("\n").at(-1) ?? "";
|
92
|
+
};
|
93
|
+
var firstLine = (text) => {
|
94
|
+
return text.split("\n").at(0) ?? "";
|
95
|
+
};
|
96
|
+
var leadingSpacesCount = (text) => {
|
97
|
+
return text.match(/^\s+/)?.at(0)?.length ?? 0;
|
98
|
+
};
|
99
|
+
var isEmpty = (text) => {
|
100
|
+
return text.trim() === "";
|
101
|
+
};
|
102
|
+
|
103
|
+
// src/str.chainable.ts
|
104
|
+
var Str = class _Str extends Pipeable {
|
105
|
+
constructor(text) {
|
106
|
+
super();
|
107
|
+
this.text = text;
|
108
|
+
}
|
109
|
+
valueOf() {
|
110
|
+
return this.text;
|
111
|
+
}
|
112
|
+
toString() {
|
113
|
+
return this.text;
|
114
|
+
}
|
115
|
+
instanceOf() {
|
116
|
+
return this;
|
117
|
+
}
|
118
|
+
/**
|
119
|
+
* Remove empty lines at the start of the text but leave whitespace on the first line with content
|
120
|
+
*/
|
121
|
+
chopEmptyLinesStart() {
|
122
|
+
return new _Str(chopEmptyLinesStart(this.text));
|
123
|
+
}
|
124
|
+
/**
|
125
|
+
* Remove empty lines at the end of the text but leave whitespace on the last line with content
|
126
|
+
*/
|
127
|
+
chopEmptyLinesEnd() {
|
128
|
+
return new _Str(chopEmptyLinesEnd(this.text));
|
129
|
+
}
|
130
|
+
/**
|
131
|
+
* Remove all space (\s) characters in lines without content
|
132
|
+
*/
|
133
|
+
trimEmptyLines() {
|
134
|
+
return new _Str(trimEmptyLines(this.text));
|
135
|
+
}
|
136
|
+
/**
|
137
|
+
* Remove all spaces (\s) characters at the end of lines
|
138
|
+
*/
|
139
|
+
trimLinesTrailingSpaces() {
|
140
|
+
return new _Str(trimLinesTrailingSpaces(this.text));
|
141
|
+
}
|
142
|
+
/**
|
143
|
+
* Removes the leading and trailing white space and line terminator characters
|
144
|
+
*/
|
145
|
+
trim() {
|
146
|
+
return new _Str(trim(this.text));
|
147
|
+
}
|
148
|
+
/**
|
149
|
+
* Removes the leading white space and line terminator characters
|
150
|
+
*/
|
151
|
+
trimStart() {
|
152
|
+
return new _Str(trimStart(this.text));
|
153
|
+
}
|
154
|
+
/**
|
155
|
+
* Removes the trailing white space and line terminator characters
|
156
|
+
*/
|
157
|
+
trimEnd() {
|
158
|
+
return new _Str(trimEnd(this.text));
|
159
|
+
}
|
160
|
+
/**
|
161
|
+
* Split a string into substrings using the specified separator and return them as an array
|
162
|
+
*/
|
163
|
+
split(separator, limit) {
|
164
|
+
return split(this.text, separator, limit).map((v) => new _Str(v));
|
165
|
+
}
|
166
|
+
/**
|
167
|
+
* Add line numbers to a string
|
168
|
+
* @param separator The separator to use between the line number and the line content.
|
169
|
+
* Defaults to ":"
|
170
|
+
*/
|
171
|
+
addLineNumbers(separator = ":") {
|
172
|
+
return new _Str(addLineNumbers(this.text, separator));
|
173
|
+
}
|
174
|
+
/**
|
175
|
+
* Returns the character at the specified index
|
176
|
+
* @return string or undefined if the index is out of bounds
|
177
|
+
*/
|
178
|
+
at(index) {
|
179
|
+
return this.text.at(index);
|
180
|
+
}
|
181
|
+
/**
|
182
|
+
* Returns the length of the string
|
183
|
+
*/
|
184
|
+
length() {
|
185
|
+
return this.text.length;
|
186
|
+
}
|
187
|
+
/**
|
188
|
+
* Indent the string by the specified number of spaces
|
189
|
+
* @param size The number of spaces to indent by
|
190
|
+
* @param char The character to use for indentation. Defaults to " "
|
191
|
+
*/
|
192
|
+
indent(size, char = " ") {
|
193
|
+
return new _Str(indent(this.text, size, char));
|
194
|
+
}
|
195
|
+
/**
|
196
|
+
* Dedent the string by the specified number of spaces
|
197
|
+
* @param indentation The number of spaces to dedent by.
|
198
|
+
* If not provided, it will be calculated automatically based on the maximum indentation in the string.
|
199
|
+
*/
|
200
|
+
dedent(indentation2) {
|
201
|
+
return new _Str(dedent(this.text, indentation2));
|
202
|
+
}
|
203
|
+
/**
|
204
|
+
* Chop the string at the start by the specified number of characters
|
205
|
+
*/
|
206
|
+
chopStart(count) {
|
207
|
+
return new _Str(chopStart(this.text, count));
|
208
|
+
}
|
209
|
+
/**
|
210
|
+
* Chop the string at the end by the specified number of characters
|
211
|
+
*/
|
212
|
+
chopEnd(count) {
|
213
|
+
if (count === 0) return this;
|
214
|
+
return new _Str(chopEnd(this.text, count));
|
215
|
+
}
|
216
|
+
/**
|
217
|
+
* Remove successive newlines of the specified repetition or more
|
218
|
+
* @param maxRepeat the maximum number of newlines to allow
|
219
|
+
*/
|
220
|
+
chopRepeatNewlines(maxRepeat) {
|
221
|
+
if (maxRepeat === 0) return this;
|
222
|
+
return new _Str(chopRepeatNewlines(this.text, maxRepeat));
|
223
|
+
}
|
224
|
+
/**
|
225
|
+
* Take the first n characters of the string
|
226
|
+
*/
|
227
|
+
takeStart(count) {
|
228
|
+
return new _Str(takeStart(this.text, count));
|
229
|
+
}
|
230
|
+
/**
|
231
|
+
* Take the last n characters of the string
|
232
|
+
*/
|
233
|
+
takeEnd(count) {
|
234
|
+
return new _Str(takeEnd(this.text, count));
|
235
|
+
}
|
236
|
+
/**
|
237
|
+
* Returns the last line of the string
|
238
|
+
*/
|
239
|
+
lastLine() {
|
240
|
+
return new _Str(lastLine(this.text));
|
241
|
+
}
|
242
|
+
/**
|
243
|
+
* Returns the first line of the string
|
244
|
+
*/
|
245
|
+
firstLine() {
|
246
|
+
return new _Str(firstLine(this.text));
|
247
|
+
}
|
248
|
+
/**
|
249
|
+
* Returns the number of leading spaces in the string
|
250
|
+
*/
|
251
|
+
leadingSpacesCount() {
|
252
|
+
return leadingSpacesCount(this.text);
|
253
|
+
}
|
254
|
+
/**
|
255
|
+
* Returns the indentation level of the string skipping empty lines in the process
|
256
|
+
*/
|
257
|
+
indentation() {
|
258
|
+
return indentation(this.text);
|
259
|
+
}
|
260
|
+
/**
|
261
|
+
* Returns true if the string is empty or contains only whitespace
|
262
|
+
*/
|
263
|
+
isEmpty() {
|
264
|
+
return isEmpty(this.text);
|
265
|
+
}
|
266
|
+
/**
|
267
|
+
* Converts the string to camel case
|
268
|
+
*/
|
269
|
+
camelCase() {
|
270
|
+
return new _Str(changeCase.camelCase(this.text));
|
271
|
+
}
|
272
|
+
/**
|
273
|
+
* Converts the string to capital case
|
274
|
+
*/
|
275
|
+
capitalCase() {
|
276
|
+
return new _Str(changeCase.capitalCase(this.text));
|
277
|
+
}
|
278
|
+
/**
|
279
|
+
* Converts the string to constant case
|
280
|
+
*/
|
281
|
+
constantCase() {
|
282
|
+
return new _Str(changeCase.constantCase(this.text));
|
283
|
+
}
|
284
|
+
/**
|
285
|
+
* Converts the string to dot case
|
286
|
+
*/
|
287
|
+
dotCase() {
|
288
|
+
return new _Str(changeCase.dotCase(this.text));
|
289
|
+
}
|
290
|
+
/**
|
291
|
+
* Converts the string to kebab case
|
292
|
+
*/
|
293
|
+
kebabCase() {
|
294
|
+
return new _Str(changeCase.kebabCase(this.text));
|
295
|
+
}
|
296
|
+
/**
|
297
|
+
* Converts the string to no case
|
298
|
+
*/
|
299
|
+
noCase() {
|
300
|
+
return new _Str(changeCase.noCase(this.text));
|
301
|
+
}
|
302
|
+
/**
|
303
|
+
* Converts the string to pascal case
|
304
|
+
*/
|
305
|
+
pascalCase() {
|
306
|
+
return new _Str(changeCase.pascalCase(this.text));
|
307
|
+
}
|
308
|
+
/**
|
309
|
+
* Converts the string to pascal snake case
|
310
|
+
*/
|
311
|
+
pascalSnakeCase() {
|
312
|
+
return new _Str(changeCase.pascalSnakeCase(this.text));
|
313
|
+
}
|
314
|
+
/**
|
315
|
+
* Converts the string to path case
|
316
|
+
*/
|
317
|
+
pathCase() {
|
318
|
+
return new _Str(changeCase.pathCase(this.text));
|
319
|
+
}
|
320
|
+
/**
|
321
|
+
* Converts the string to sentence case
|
322
|
+
*/
|
323
|
+
sentenceCase() {
|
324
|
+
return new _Str(changeCase.sentenceCase(this.text));
|
325
|
+
}
|
326
|
+
/**
|
327
|
+
* Converts the string to snake case
|
328
|
+
*/
|
329
|
+
snakeCase() {
|
330
|
+
return new _Str(changeCase.snakeCase(this.text));
|
331
|
+
}
|
332
|
+
/**
|
333
|
+
* Converts the string to train case
|
334
|
+
*/
|
335
|
+
trainCase() {
|
336
|
+
return new _Str(changeCase.trainCase(this.text));
|
337
|
+
}
|
338
|
+
/**
|
339
|
+
* Shorthand for `.toString()`
|
340
|
+
*/
|
341
|
+
get str() {
|
342
|
+
return this.toString();
|
343
|
+
}
|
344
|
+
};
|
345
|
+
var str = (text) => {
|
346
|
+
return new Str(text.toString());
|
347
|
+
};
|
348
|
+
export {
|
349
|
+
addLineNumbers,
|
350
|
+
camelCase2 as camelCase,
|
351
|
+
capitalCase2 as capitalCase,
|
352
|
+
chopEmptyLinesEnd,
|
353
|
+
chopEmptyLinesStart,
|
354
|
+
chopEnd,
|
355
|
+
chopRepeatNewlines,
|
356
|
+
chopStart,
|
357
|
+
constantCase2 as constantCase,
|
358
|
+
dedent,
|
359
|
+
dotCase2 as dotCase,
|
360
|
+
firstLine,
|
361
|
+
indent,
|
362
|
+
indentation,
|
363
|
+
isEmpty,
|
364
|
+
kebabCase2 as kebabCase,
|
365
|
+
lastLine,
|
366
|
+
leadingSpacesCount,
|
367
|
+
noCase2 as noCase,
|
368
|
+
pascalCase2 as pascalCase,
|
369
|
+
pascalSnakeCase2 as pascalSnakeCase,
|
370
|
+
pathCase2 as pathCase,
|
371
|
+
sentenceCase2 as sentenceCase,
|
372
|
+
snakeCase2 as snakeCase,
|
373
|
+
split,
|
374
|
+
str,
|
375
|
+
takeEnd,
|
376
|
+
takeStart,
|
377
|
+
trainCase2 as trainCase,
|
378
|
+
trim,
|
379
|
+
trimEmptyLines,
|
380
|
+
trimEnd,
|
381
|
+
trimLinesTrailingSpaces,
|
382
|
+
trimStart
|
383
|
+
};
|
384
|
+
//# sourceMappingURL=str.index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../src/str.bundle.ts","../src/str.chainable.ts","../src/str.lib.ts"],"sourcesContent":["export {\n camelCase,\n capitalCase,\n constantCase,\n dotCase,\n kebabCase,\n noCase,\n pascalCase,\n pascalSnakeCase,\n pathCase,\n sentenceCase,\n snakeCase,\n trainCase,\n} from \"change-case\";\nexport { str } from \"./str.chainable\";\nexport * from \"./str.lib\";\n","import { Pipeable } from \"@synstack/pipe\";\nimport * as changeCase from \"change-case\";\nimport * as lib from \"./str.lib\";\n\nexport type Stringable = {\n toString: () => string;\n};\n\nexport class Str extends Pipeable<Str, string> {\n public constructor(private readonly text: string) {\n super();\n }\n\n public valueOf(): string {\n return this.text;\n }\n\n public toString() {\n return this.text;\n }\n\n public instanceOf(): Str {\n return this;\n }\n\n /**\n * Remove empty lines at the start of the text but leave whitespace on the first line with content\n */\n public chopEmptyLinesStart() {\n return new Str(lib.chopEmptyLinesStart(this.text));\n }\n\n /**\n * Remove empty lines at the end of the text but leave whitespace on the last line with content\n */\n public chopEmptyLinesEnd() {\n return new Str(lib.chopEmptyLinesEnd(this.text));\n }\n\n /**\n * Remove all space (\\s) characters in lines without content\n */\n public trimEmptyLines() {\n return new Str(lib.trimEmptyLines(this.text));\n }\n\n /**\n * Remove all spaces (\\s) characters at the end of lines\n */\n public trimLinesTrailingSpaces() {\n return new Str(lib.trimLinesTrailingSpaces(this.text));\n }\n\n /**\n * Removes the leading and trailing white space and line terminator characters\n */\n public trim() {\n return new Str(lib.trim(this.text));\n }\n\n /**\n * Removes the leading white space and line terminator characters\n */\n public trimStart() {\n return new Str(lib.trimStart(this.text));\n }\n\n /**\n * Removes the trailing white space and line terminator characters\n */\n public trimEnd() {\n return new Str(lib.trimEnd(this.text));\n }\n\n /**\n * Split a string into substrings using the specified separator and return them as an array\n */\n public split(separator: string | RegExp, limit?: number) {\n return lib.split(this.text, separator, limit).map((v) => new Str(v));\n }\n\n /**\n * Add line numbers to a string\n * @param separator The separator to use between the line number and the line content.\n * Defaults to \":\"\n */\n public addLineNumbers(separator: string = \":\") {\n return new Str(lib.addLineNumbers(this.text, separator));\n }\n\n /**\n * Returns the character at the specified index\n * @return string or undefined if the index is out of bounds\n */\n public at(index: number) {\n return this.text.at(index);\n }\n\n /**\n * Returns the length of the string\n */\n public length() {\n return this.text.length;\n }\n\n /**\n * Indent the string by the specified number of spaces\n * @param size The number of spaces to indent by\n * @param char The character to use for indentation. Defaults to \" \"\n */\n public indent(size: number, char: string = \" \") {\n return new Str(lib.indent(this.text, size, char));\n }\n\n /**\n * Dedent the string by the specified number of spaces\n * @param indentation The number of spaces to dedent by.\n * If not provided, it will be calculated automatically based on the maximum indentation in the string.\n */\n public dedent(indentation?: number) {\n return new Str(lib.dedent(this.text, indentation));\n }\n\n /**\n * Chop the string at the start by the specified number of characters\n */\n public chopStart(count: number) {\n return new Str(lib.chopStart(this.text, count));\n }\n\n /**\n * Chop the string at the end by the specified number of characters\n */\n public chopEnd(count: number) {\n if (count === 0) return this;\n return new Str(lib.chopEnd(this.text, count));\n }\n\n /**\n * Remove successive newlines of the specified repetition or more\n * @param maxRepeat the maximum number of newlines to allow\n */\n public chopRepeatNewlines(maxRepeat: number) {\n if (maxRepeat === 0) return this;\n return new Str(lib.chopRepeatNewlines(this.text, maxRepeat));\n }\n\n /**\n * Take the first n characters of the string\n */\n public takeStart(count: number) {\n return new Str(lib.takeStart(this.text, count));\n }\n\n /**\n * Take the last n characters of the string\n */\n public takeEnd(count: number) {\n return new Str(lib.takeEnd(this.text, count));\n }\n\n /**\n * Returns the last line of the string\n */\n public lastLine() {\n return new Str(lib.lastLine(this.text));\n }\n\n /**\n * Returns the first line of the string\n */\n public firstLine() {\n return new Str(lib.firstLine(this.text));\n }\n\n /**\n * Returns the number of leading spaces in the string\n */\n public leadingSpacesCount() {\n return lib.leadingSpacesCount(this.text);\n }\n\n /**\n * Returns the indentation level of the string skipping empty lines in the process\n */\n public indentation() {\n return lib.indentation(this.text);\n }\n\n /**\n * Returns true if the string is empty or contains only whitespace\n */\n public isEmpty() {\n return lib.isEmpty(this.text);\n }\n\n /**\n * Converts the string to camel case\n */\n public camelCase() {\n return new Str(changeCase.camelCase(this.text));\n }\n\n /**\n * Converts the string to capital case\n */\n public capitalCase() {\n return new Str(changeCase.capitalCase(this.text));\n }\n\n /**\n * Converts the string to constant case\n */\n public constantCase() {\n return new Str(changeCase.constantCase(this.text));\n }\n\n /**\n * Converts the string to dot case\n */\n public dotCase() {\n return new Str(changeCase.dotCase(this.text));\n }\n\n /**\n * Converts the string to kebab case\n */\n public kebabCase() {\n return new Str(changeCase.kebabCase(this.text));\n }\n\n /**\n * Converts the string to no case\n */\n public noCase() {\n return new Str(changeCase.noCase(this.text));\n }\n\n /**\n * Converts the string to pascal case\n */\n public pascalCase() {\n return new Str(changeCase.pascalCase(this.text));\n }\n\n /**\n * Converts the string to pascal snake case\n */\n public pascalSnakeCase() {\n return new Str(changeCase.pascalSnakeCase(this.text));\n }\n\n /**\n * Converts the string to path case\n */\n public pathCase() {\n return new Str(changeCase.pathCase(this.text));\n }\n\n /**\n * Converts the string to sentence case\n */\n public sentenceCase() {\n return new Str(changeCase.sentenceCase(this.text));\n }\n\n /**\n * Converts the string to snake case\n */\n public snakeCase() {\n return new Str(changeCase.snakeCase(this.text));\n }\n\n /**\n * Converts the string to train case\n */\n public trainCase() {\n return new Str(changeCase.trainCase(this.text));\n }\n\n /**\n * Shorthand for `.toString()`\n */\n public get str() {\n return this.toString();\n }\n}\n\nexport const str = (text: Stringable) => {\n return new Str(text.toString());\n};\n","/**\n * Remove empty lines at the start of the text but leave whitespace on the first line with content\n */\nexport const chopEmptyLinesStart = (text: string) => {\n return text.replace(/^(\\s*\\n)+/, \"\");\n};\n\n/**\n * Remove empty lines at the end of the text but leave whitespace on the last line with content\n */\nexport const chopEmptyLinesEnd = (text: string) => {\n return text.replace(/(\\n\\s*)+$/, \"\");\n};\n\n/**\n * Remove all space (\\s) characters in lines without content\n */\nexport const trimEmptyLines = (text: string) => {\n return text.replace(/(^|\\n)\\s+(\\n|$)/g, \"$1$2\");\n};\n\n/**\n * Remove all space (\\s) characters at the end of lines\n */\nexport const trimLinesTrailingSpaces = (text: string) => {\n return text.replace(/ +(\\n|$)/g, \"$1\");\n};\n\n/**\n * Removes the leading and trailing white space and line terminator characters\n */\nexport const trim = (text: string) => {\n return text.trim();\n};\n\n/**\n * Removes the leading white space and line terminator characters\n */\nexport const trimStart = (text: string) => {\n return text.trimStart();\n};\n\n/**\n * Removes the trailing white space and line terminator characters\n */\nexport const trimEnd = (text: string) => {\n return text.trimEnd();\n};\n\n/**\n * Split a string into substrings using the specified separator and return them as an array\n */\nexport const split = (\n text: string,\n separator: string | RegExp,\n limit?: number,\n) => {\n return text.split(separator, limit);\n};\n\n/**\n * Add line numbers to a string\n * @param text The string to add line numbers to\n * @param separator The separator to use between the line number and the line content.\n * Defaults to \":\"\n */\nexport const addLineNumbers = (text: string, separator: string = \":\") => {\n return text\n .split(\"\\n\")\n .map((line, index) => `${index}${separator}${line}`)\n .join(\"\\n\");\n};\n\n/**\n * Returns the indentation level of the string skipping empty lines in the process\n */\nexport const indentation = (text: string) => {\n return (\n text.split(\"\\n\").reduce((acc: number | null, line) => {\n if (line.trim() === \"\") return acc;\n const indentation = leadingSpacesCount(line);\n if (acc === null) return indentation;\n return Math.min(acc, indentation);\n }, null) ?? 0\n );\n};\n\n/**\n * Indent the string by the specified number of spaces\n * @param size The number of spaces to indent by\n * @param char The character to use for indentation. Defaults to \" \"\n */\nexport const indent = (text: string, size: number, char: string = \" \") => {\n if (size === 0) return text;\n\n const indentStr = char.repeat(size);\n return text\n .split(\"\\n\")\n .map((line) => indentStr + line)\n .join(\"\\n\");\n};\n\n/**\n * Dedent the string by the specified number of spaces\n * @param indentation The number of spaces to dedent by.\n * If not provided, it will be calculated automatically based on the maximum indentation in the string.\n */\nexport const dedent = (text: string, size?: number) => {\n const _size = size ?? indentation(text);\n if (_size === 0) return text;\n const regex = new RegExp(`^\\\\s{1,${_size}}`);\n return text\n .split(\"\\n\")\n .map((line) => line.replace(regex, \"\"))\n .join(\"\\n\");\n};\n\n/**\n * Chop the string at the end by the specified number of characters\n */\nexport const chopEnd = (text: string, count: number) => {\n if (count === 0) return text;\n return text.slice(0, -count);\n};\n\n/**\n * Chop the string at the start by the specified number of characters\n */\nexport const chopStart = (text: string, count: number) => {\n if (count === 0) return text;\n return text.slice(count);\n};\n\n/**\n * Remove successive newlines of the specified repetition or more\n * @param maxRepeat the maximum number of newlines to allow\n */\nexport const chopRepeatNewlines = (text: string, maxRepeat: number) => {\n if (maxRepeat === 0) return text;\n return text.replace(\n new RegExp(`\\n{${maxRepeat + 1},}`, \"g\"),\n \"\\n\".repeat(maxRepeat),\n );\n};\n\n/**\n * Take the first n characters of the string\n */\nexport const takeStart = (text: string, count: number) => {\n return text.slice(0, count);\n};\n\n/**\n * Take the last n characters of the string\n */\nexport const takeEnd = (text: string, count: number) => {\n return text.slice(-count);\n};\n\n/**\n * Returns the last line of the string\n */\nexport const lastLine = (text: string) => {\n return text.split(\"\\n\").at(-1) ?? \"\";\n};\n\n/**\n * Returns the first line of the string\n */\nexport const firstLine = (text: string) => {\n return text.split(\"\\n\").at(0) ?? \"\";\n};\n\n/**\n * Returns the number of leading spaces in the string\n */\nexport const leadingSpacesCount = (text: string) => {\n return text.match(/^\\s+/)?.at(0)?.length ?? 0;\n};\n\n/**\n * Returns true if the string is empty or contains only whitespace\n */\nexport const isEmpty = (text: string) => {\n return text.trim() === \"\";\n};\n"],"mappings":";AAAA;AAAA,EACE,aAAAA;AAAA,EACA,eAAAC;AAAA,EACA,gBAAAC;AAAA,EACA,WAAAC;AAAA,EACA,aAAAC;AAAA,EACA,UAAAC;AAAA,EACA,cAAAC;AAAA,EACA,mBAAAC;AAAA,EACA,YAAAC;AAAA,EACA,gBAAAC;AAAA,EACA,aAAAC;AAAA,EACA,aAAAC;AAAA,OACK;;;ACbP,SAAS,gBAAgB;AACzB,YAAY,gBAAgB;;;ACErB,IAAM,sBAAsB,CAAC,SAAiB;AACnD,SAAO,KAAK,QAAQ,aAAa,EAAE;AACrC;AAKO,IAAM,oBAAoB,CAA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AY;AACjB,WAAO,IAAI,KAAe,qBAAU,KAAK,IAAI,CAAC;AAAA,EAChD;AAAA;AAAA;AAAA;AAAA,EAKA,IAAW,MAAM;AACf,WAAO,KAAK,SAAS;AAAA,EACvB;AACF;AAEO,IAAM,MAAM,CAAC,SAAqB;AACvC,SAAO,IAAI,IAAI,KAAK,SAAS,CAAC;AAChC;","names":["camelCase","capitalCase","constantCase","dotCase","kebabCase","noCase","pascalCase","pascalSnakeCase","pathCase","sentenceCase","snakeCase","trainCase","indentation","indentation"]}
|
package/package.json
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
{
|
2
|
+
"name": "@synstack/str",
|
3
|
+
"type": "module",
|
4
|
+
"publishConfig": {
|
5
|
+
"access": "public"
|
6
|
+
},
|
7
|
+
"packageManager": "yarn@4.4.0",
|
8
|
+
"version": "1.0.1-alpha.0",
|
9
|
+
"description": "Advanced chainable string manipulation",
|
10
|
+
"keywords": [
|
11
|
+
"string",
|
12
|
+
"typescript",
|
13
|
+
"ts",
|
14
|
+
"chaining",
|
15
|
+
"case"
|
16
|
+
],
|
17
|
+
"author": {
|
18
|
+
"name": "pAIrprog",
|
19
|
+
"url": "https://pairprog.io"
|
20
|
+
},
|
21
|
+
"homepage": "https://github.com/pAIrprogio/synscript/tree/main/packages/str",
|
22
|
+
"repository": {
|
23
|
+
"type": "git",
|
24
|
+
"url": "https://github.com/pAIrprogio/syn-stack.git",
|
25
|
+
"directory": "packages/str"
|
26
|
+
},
|
27
|
+
"license": "Apache-2.0",
|
28
|
+
"scripts": {
|
29
|
+
"publish": "yarn npm publish --access public",
|
30
|
+
"prepublish": "yarn test && yarn build",
|
31
|
+
"build": "tsup",
|
32
|
+
"build:watch": "tsup --watch",
|
33
|
+
"test:types": "tsc --noEmit",
|
34
|
+
"test:unit": "node --import tsx --test src/**/*.test.ts",
|
35
|
+
"test:unit:watch": "node --import tsx --watch --test src/**/*.test.ts",
|
36
|
+
"test": "yarn test:types && yarn test:unit"
|
37
|
+
},
|
38
|
+
"exports": {
|
39
|
+
".": {
|
40
|
+
"import": {
|
41
|
+
"types": "./dist/str.index.d.ts",
|
42
|
+
"default": "./dist/str.index.js"
|
43
|
+
},
|
44
|
+
"require": {
|
45
|
+
"types": "./dist/str.index.d.cts",
|
46
|
+
"default": "./dist/str.index.cjs"
|
47
|
+
}
|
48
|
+
}
|
49
|
+
},
|
50
|
+
"dependencies": {
|
51
|
+
"@synstack/pipe": "1.0.0",
|
52
|
+
"change-case": "^5.4.4"
|
53
|
+
},
|
54
|
+
"devDependencies": {
|
55
|
+
"@types/node": "^22.7.0",
|
56
|
+
"tsup": "^8.3.0",
|
57
|
+
"tsx": "^4.19.1",
|
58
|
+
"typescript": "^5.6.2"
|
59
|
+
},
|
60
|
+
"files": [
|
61
|
+
"src/**/*.ts",
|
62
|
+
"!src/**/*.test.ts",
|
63
|
+
"dist/**/*"
|
64
|
+
],
|
65
|
+
"gitHead": "c3857f4b9ec3129c478b62ddf9dbd039b5b133dd"
|
66
|
+
}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
export {
|
2
|
+
camelCase,
|
3
|
+
capitalCase,
|
4
|
+
constantCase,
|
5
|
+
dotCase,
|
6
|
+
kebabCase,
|
7
|
+
noCase,
|
8
|
+
pascalCase,
|
9
|
+
pascalSnakeCase,
|
10
|
+
pathCase,
|
11
|
+
sentenceCase,
|
12
|
+
snakeCase,
|
13
|
+
trainCase,
|
14
|
+
} from "change-case";
|
15
|
+
export { str } from "./str.chainable";
|
16
|
+
export * from "./str.lib";
|