@ppabari/strio 1.0.2 → 1.1.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/CHANGELOG.md +42 -0
- package/README.md +57 -3
- package/dist/bin/strio.js +53 -0
- package/dist/index.cjs +51 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +51 -3
- package/dist/index.d.ts +51 -3
- package/dist/index.js +49 -1
- package/dist/index.js.map +1 -1
- package/dist/str.cjs +623 -0
- package/dist/str.cjs.map +1 -0
- package/dist/str.d.cts +579 -0
- package/dist/str.d.ts +579 -0
- package/dist/str.js +567 -0
- package/dist/str.js.map +1 -0
- package/dist/{zod-CINbFpp9.d.cts → zod-IWq12GIY.d.cts} +1 -1
- package/dist/{zod-CINbFpp9.d.ts → zod-IWq12GIY.d.ts} +1 -1
- package/dist/zod.d.cts +1 -1
- package/dist/zod.d.ts +1 -1
- package/package.json +31 -8
package/dist/str.d.cts
ADDED
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* str/manipulation.ts
|
|
3
|
+
* Core string manipulation helpers.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Uppercase the first character of a string.
|
|
7
|
+
*
|
|
8
|
+
* @param str The input string.
|
|
9
|
+
* @param lowerRest When `true`, the remaining characters are lowercased
|
|
10
|
+
* (turning `'hELLO'` into `'Hello'`). Defaults to `false`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* capitalize('hello'); // → 'Hello'
|
|
14
|
+
* capitalize('hELLO', true); // → 'Hello'
|
|
15
|
+
*/
|
|
16
|
+
declare function capitalize(str: string, lowerRest?: boolean): string;
|
|
17
|
+
/**
|
|
18
|
+
* Capitalize the first letter of every whitespace-separated word.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* capitalizeWords('hello world'); // → 'Hello World'
|
|
22
|
+
*/
|
|
23
|
+
declare function capitalizeWords(str: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* Reverse a string. Unicode-aware — surrogate pairs (e.g. emoji) stay intact.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* reverse('abc'); // → 'cba'
|
|
29
|
+
* reverse('a😀b'); // → 'b😀a'
|
|
30
|
+
*/
|
|
31
|
+
declare function reverse(str: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* Truncate a string to at most `length` characters, appending `suffix`
|
|
34
|
+
* (default `'…'`) when truncation occurs. The suffix counts toward `length`.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* truncate('The quick brown fox', 9); // → 'The quic…'
|
|
38
|
+
* truncate('The quick brown fox', 9, '...'); // → 'The qu...'
|
|
39
|
+
* truncate('short', 20); // → 'short'
|
|
40
|
+
*/
|
|
41
|
+
declare function truncate(str: string, length: number, suffix?: string): string;
|
|
42
|
+
/**
|
|
43
|
+
* Truncate a string to at most `count` words, appending `suffix`
|
|
44
|
+
* (default `'…'`) when truncation occurs.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* truncateWords('The quick brown fox', 2); // → 'The quick…'
|
|
48
|
+
*/
|
|
49
|
+
declare function truncateWords(str: string, count: number, suffix?: string): string;
|
|
50
|
+
/**
|
|
51
|
+
* Trim characters from both ends of a string. With no `chars` argument this
|
|
52
|
+
* behaves like `String.prototype.trim` (all whitespace). When `chars` is
|
|
53
|
+
* provided, every character in that set is stripped from both ends.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* trim(' hi '); // → 'hi'
|
|
57
|
+
* trim('__hi__', '_'); // → 'hi'
|
|
58
|
+
* trim('xyabcyx', 'xy'); // → 'abc'
|
|
59
|
+
*/
|
|
60
|
+
declare function trim(str: string, chars?: string): string;
|
|
61
|
+
/**
|
|
62
|
+
* Insert `substring` into `str` at the given `index`. Negative indexes count
|
|
63
|
+
* from the end; out-of-range indexes clamp to the nearest boundary.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* insert('helloworld', 5, ' '); // → 'hello world'
|
|
67
|
+
* insert('ac', 1, 'b'); // → 'abc'
|
|
68
|
+
*/
|
|
69
|
+
declare function insert(str: string, index: number, substring: string): string;
|
|
70
|
+
interface MaskOptions {
|
|
71
|
+
/** Character used for masking. @default '*' */
|
|
72
|
+
maskChar?: string;
|
|
73
|
+
/** Number of leading characters left visible. @default 0 */
|
|
74
|
+
visibleStart?: number;
|
|
75
|
+
/** Number of trailing characters left visible. @default 0 */
|
|
76
|
+
visibleEnd?: number;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Mask the middle of a string, revealing only the first `visibleStart` and
|
|
80
|
+
* last `visibleEnd` characters. Useful for redacting emails, cards, tokens.
|
|
81
|
+
*
|
|
82
|
+
* (For secret-grade masking with length hiding see `maskSecret` in the main
|
|
83
|
+
* `@ppabari/strio` export.)
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* mask('4242424242424242', { visibleStart: 0, visibleEnd: 4 });
|
|
87
|
+
* // → '************4242'
|
|
88
|
+
* mask('john@example.com', { visibleStart: 2, visibleEnd: 0 });
|
|
89
|
+
* // → 'jo**************'
|
|
90
|
+
*/
|
|
91
|
+
declare function mask(str: string, options?: MaskOptions): string;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* str/case.ts
|
|
95
|
+
* Case-conversion helpers. All are built on the shared `extractWords` splitter,
|
|
96
|
+
* so they handle camelCase, PascalCase, snake_case, kebab-case, spaces, and
|
|
97
|
+
* mixed inputs consistently.
|
|
98
|
+
*/
|
|
99
|
+
/**
|
|
100
|
+
* Convert a string to `camelCase`.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* camelize('foo-bar'); // → 'fooBar'
|
|
104
|
+
* camelize('Foo Bar Baz'); // → 'fooBarBaz'
|
|
105
|
+
* camelize('foo_bar'); // → 'fooBar'
|
|
106
|
+
*/
|
|
107
|
+
declare function camelize(str: string): string;
|
|
108
|
+
/**
|
|
109
|
+
* Convert a string to `PascalCase` (a.k.a. UpperCamelCase).
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* pascalize('foo-bar'); // → 'FooBar'
|
|
113
|
+
* pascalize('foo bar'); // → 'FooBar'
|
|
114
|
+
*/
|
|
115
|
+
declare function pascalize(str: string): string;
|
|
116
|
+
/**
|
|
117
|
+
* Convert a string to `kebab-case` (dash-separated, lowercase).
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* dasherize('fooBar'); // → 'foo-bar'
|
|
121
|
+
* dasherize('Foo Bar'); // → 'foo-bar'
|
|
122
|
+
* dasherize('foo_bar'); // → 'foo-bar'
|
|
123
|
+
*/
|
|
124
|
+
declare function dasherize(str: string): string;
|
|
125
|
+
/**
|
|
126
|
+
* Convert a string to `snake_case` (underscore-separated, lowercase).
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* underscore('fooBar'); // → 'foo_bar'
|
|
130
|
+
* underscore('Foo-Bar'); // → 'foo_bar'
|
|
131
|
+
*/
|
|
132
|
+
declare function underscore(str: string): string;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* str/validation.ts
|
|
136
|
+
* Boolean predicates for inspecting strings. These never throw — they return
|
|
137
|
+
* `false` for non-string input (except `isString`, whose job is that check).
|
|
138
|
+
*/
|
|
139
|
+
/**
|
|
140
|
+
* Whether the value is a string primitive.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* isString('a'); // → true
|
|
144
|
+
* isString(42); // → false
|
|
145
|
+
*/
|
|
146
|
+
declare function isString(value: unknown): value is string;
|
|
147
|
+
/**
|
|
148
|
+
* Whether the string has zero length. Non-strings are treated as empty.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* isEmpty(''); // → true
|
|
152
|
+
* isEmpty(' '); // → false
|
|
153
|
+
*/
|
|
154
|
+
declare function isEmpty(value: unknown): boolean;
|
|
155
|
+
/**
|
|
156
|
+
* Whether the string is empty or contains only whitespace.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* isBlank(' '); // → true
|
|
160
|
+
* isBlank('\n\t'); // → true
|
|
161
|
+
* isBlank('a'); // → false
|
|
162
|
+
*/
|
|
163
|
+
declare function isBlank(value: unknown): boolean;
|
|
164
|
+
/**
|
|
165
|
+
* Whether the string contains only ASCII letters (`A–Z`, `a–z`) and is
|
|
166
|
+
* non-empty.
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* isAlpha('Hello'); // → true
|
|
170
|
+
* isAlpha('Hi5'); // → false
|
|
171
|
+
*/
|
|
172
|
+
declare function isAlpha(value: unknown): boolean;
|
|
173
|
+
/**
|
|
174
|
+
* Whether the string contains only ASCII digits (`0–9`) and is non-empty.
|
|
175
|
+
* Note: this checks the *character class*, not numeric parseability — `'007'`
|
|
176
|
+
* is numeric, `'-1'` and `'1.5'` are not.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* isNumeric('12345'); // → true
|
|
180
|
+
* isNumeric('1.5'); // → false
|
|
181
|
+
*/
|
|
182
|
+
declare function isNumeric(value: unknown): boolean;
|
|
183
|
+
/**
|
|
184
|
+
* Whether the string contains only ASCII letters and digits, and is non-empty.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* isAlphaNumeric('abc123'); // → true
|
|
188
|
+
* isAlphaNumeric('abc 12'); // → false
|
|
189
|
+
*/
|
|
190
|
+
declare function isAlphaNumeric(value: unknown): boolean;
|
|
191
|
+
/**
|
|
192
|
+
* Whether the string is fully uppercase. It must contain at least one cased
|
|
193
|
+
* letter and be unchanged by `toUpperCase()`.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* isUpperCase('HELLO'); // → true
|
|
197
|
+
* isUpperCase('HELLO1'); // → true
|
|
198
|
+
* isUpperCase('Hello'); // → false
|
|
199
|
+
* isUpperCase('123'); // → false (no cased letters)
|
|
200
|
+
*/
|
|
201
|
+
declare function isUpperCase(value: unknown): boolean;
|
|
202
|
+
/**
|
|
203
|
+
* Whether the string is fully lowercase. It must contain at least one cased
|
|
204
|
+
* letter and be unchanged by `toLowerCase()`.
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* isLowerCase('hello'); // → true
|
|
208
|
+
* isLowerCase('Hello'); // → false
|
|
209
|
+
*/
|
|
210
|
+
declare function isLowerCase(value: unknown): boolean;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* str/searching.ts
|
|
214
|
+
* Substring searching, counting, and extraction helpers.
|
|
215
|
+
*/
|
|
216
|
+
/**
|
|
217
|
+
* Whether `str` contains `substring`.
|
|
218
|
+
*
|
|
219
|
+
* @param caseSensitive Defaults to `true`.
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* contains('Hello World', 'World'); // → true
|
|
223
|
+
* contains('Hello World', 'world'); // → false
|
|
224
|
+
* contains('Hello World', 'world', false); // → true
|
|
225
|
+
*/
|
|
226
|
+
declare function contains(str: string, substring: string, caseSensitive?: boolean): boolean;
|
|
227
|
+
/**
|
|
228
|
+
* Whether `str` contains *every* substring in the list.
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* containsAll('the quick fox', ['the', 'fox']); // → true
|
|
232
|
+
* containsAll('the quick fox', ['the', 'dog']); // → false
|
|
233
|
+
*/
|
|
234
|
+
declare function containsAll(str: string, substrings: string[], caseSensitive?: boolean): boolean;
|
|
235
|
+
/**
|
|
236
|
+
* Whether `str` contains *at least one* substring in the list.
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* containsAny('the quick fox', ['cat', 'fox']); // → true
|
|
240
|
+
* containsAny('the quick fox', ['cat', 'dog']); // → false
|
|
241
|
+
*/
|
|
242
|
+
declare function containsAny(str: string, substrings: string[], caseSensitive?: boolean): boolean;
|
|
243
|
+
/**
|
|
244
|
+
* Count occurrences of `substring` within `str`.
|
|
245
|
+
*
|
|
246
|
+
* @param overlapping When `true`, counts overlapping matches
|
|
247
|
+
* (`count('aaa', 'aa', true)` → 2). Defaults to `false`.
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* count('banana', 'a'); // → 3
|
|
251
|
+
* count('aaaa', 'aa'); // → 2
|
|
252
|
+
* count('aaaa', 'aa', true); // → 3
|
|
253
|
+
*/
|
|
254
|
+
declare function count(str: string, substring: string, overlapping?: boolean): number;
|
|
255
|
+
/**
|
|
256
|
+
* Extract the first substring found between `start` and `end` delimiters.
|
|
257
|
+
* Returns an empty string if the pair is not found.
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* between('[a][b]', '[', ']'); // → 'a'
|
|
261
|
+
* between('key=value;', '=', ';'); // → 'value'
|
|
262
|
+
*/
|
|
263
|
+
declare function between(str: string, start: string, end: string): string;
|
|
264
|
+
/**
|
|
265
|
+
* Extract *all* non-overlapping substrings found between `start` and `end`
|
|
266
|
+
* delimiters, in document order.
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* betweenAll('[a][b][c]', '[', ']'); // → ['a', 'b', 'c']
|
|
270
|
+
*/
|
|
271
|
+
declare function betweenAll(str: string, start: string, end: string): string[];
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* str/formatting.ts
|
|
275
|
+
* Human-facing formatting helpers.
|
|
276
|
+
*
|
|
277
|
+
* NOTE: `titleCase`, `pluralize`, and `ordinalize` implement **English** rules
|
|
278
|
+
* only. Internationalization is out of scope.
|
|
279
|
+
*/
|
|
280
|
+
/**
|
|
281
|
+
* Convert a machine identifier (snake_case, camelCase, kebab-case…) into a
|
|
282
|
+
* human-readable sentence: words separated by spaces with the first letter
|
|
283
|
+
* capitalized.
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* humanize('first_name'); // → 'First name'
|
|
287
|
+
* humanize('userID'); // → 'User id'
|
|
288
|
+
* humanize('created-at'); // → 'Created at'
|
|
289
|
+
*/
|
|
290
|
+
declare function humanize(str: string): string;
|
|
291
|
+
/**
|
|
292
|
+
* Convert a string to Title Case using English conventions: every word is
|
|
293
|
+
* capitalized except minor words (articles, short prepositions, conjunctions),
|
|
294
|
+
* which stay lowercase unless they are the first or last word.
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* titleCase('the lord of the rings'); // → 'The Lord of the Rings'
|
|
298
|
+
* titleCase('a tale of two cities'); // → 'A Tale of Two Cities'
|
|
299
|
+
*/
|
|
300
|
+
declare function titleCase(str: string): string;
|
|
301
|
+
/**
|
|
302
|
+
* Convert a string into a URL-friendly slug: transliterated to ASCII,
|
|
303
|
+
* lowercased, with runs of non-alphanumerics collapsed to `separator`.
|
|
304
|
+
*
|
|
305
|
+
* @param separator Word separator. Defaults to `'-'`.
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* slugify('Héllo, World!'); // → 'hello-world'
|
|
309
|
+
* slugify('Foo Bar', '_'); // → 'foo_bar'
|
|
310
|
+
*/
|
|
311
|
+
declare function slugify(str: string, separator?: string): string;
|
|
312
|
+
/**
|
|
313
|
+
* Return the ordinal form of a number: `1` → `'1st'`, `2` → `'2nd'`,
|
|
314
|
+
* `13` → `'13th'`, `22` → `'22nd'`. Accepts a number or a numeric string.
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* ordinalize(1); // → '1st'
|
|
318
|
+
* ordinalize(11); // → '11th'
|
|
319
|
+
* ordinalize(103); // → '103rd'
|
|
320
|
+
*/
|
|
321
|
+
declare function ordinalize(n: number | string): string;
|
|
322
|
+
/**
|
|
323
|
+
* Pluralize an English word. If `count` is provided, the word is only
|
|
324
|
+
* pluralized when `count !== 1`. Case of the first letter is preserved.
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* pluralize('cat'); // → 'cats'
|
|
328
|
+
* pluralize('bus'); // → 'buses'
|
|
329
|
+
* pluralize('city'); // → 'cities'
|
|
330
|
+
* pluralize('child'); // → 'children'
|
|
331
|
+
* pluralize('cat', 1); // → 'cat'
|
|
332
|
+
* pluralize('cat', 3); // → 'cats'
|
|
333
|
+
*/
|
|
334
|
+
declare function pluralize(word: string, count?: number): string;
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* str/utilities.ts
|
|
338
|
+
* General-purpose string utilities: tokenizing, templating, similarity,
|
|
339
|
+
* transliteration, and cryptographically-secure random strings.
|
|
340
|
+
*/
|
|
341
|
+
/**
|
|
342
|
+
* Split a string into an array of words (runs of letters/digits), discarding
|
|
343
|
+
* punctuation and whitespace. Unlike case conversion, this does NOT break
|
|
344
|
+
* camelCase boundaries.
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
347
|
+
* words('Hello, world! 42'); // → ['Hello', 'world', '42']
|
|
348
|
+
*/
|
|
349
|
+
declare function words(str: string): string[];
|
|
350
|
+
/**
|
|
351
|
+
* Count the words in a string.
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* wordCount('the quick brown fox'); // → 4
|
|
355
|
+
*/
|
|
356
|
+
declare function wordCount(str: string): number;
|
|
357
|
+
/**
|
|
358
|
+
* Join a list of values with a separator, skipping `null`, `undefined`, and
|
|
359
|
+
* empty-string entries so you never get doubled or trailing separators.
|
|
360
|
+
*
|
|
361
|
+
* @param separator Defaults to `' '`.
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* join(['a', '', 'b', null, 'c']); // → 'a b c'
|
|
365
|
+
* join(['2024', '01', '05'], '-'); // → '2024-01-05'
|
|
366
|
+
*/
|
|
367
|
+
declare function join(values: Array<string | number | null | undefined>, separator?: string): string;
|
|
368
|
+
interface TemplateOptions {
|
|
369
|
+
/**
|
|
370
|
+
* Value substituted for keys missing from `data`. When `undefined`
|
|
371
|
+
* (default), the original `{{placeholder}}` is left untouched.
|
|
372
|
+
*/
|
|
373
|
+
fallback?: string;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Interpolate `{{key}}` placeholders in `str` with values from `data`.
|
|
377
|
+
* Whitespace inside the braces is ignored (`{{ name }}` works). Values are
|
|
378
|
+
* coerced to strings.
|
|
379
|
+
*
|
|
380
|
+
* @example
|
|
381
|
+
* template('Hi {{name}}!', { name: 'Ada' }); // → 'Hi Ada!'
|
|
382
|
+
* template('{{a}}/{{b}}', { a: 1, b: 2 }); // → '1/2'
|
|
383
|
+
* template('Hi {{name}}', {}, { fallback: '?' }); // → 'Hi ?'
|
|
384
|
+
*/
|
|
385
|
+
declare function template(str: string, data: Record<string, unknown>, options?: TemplateOptions): string;
|
|
386
|
+
/**
|
|
387
|
+
* Generate a cryptographically-secure random string of the given `length`.
|
|
388
|
+
* Uses the same bias-free Web-Crypto engine as the rest of `@ppabari/strio`.
|
|
389
|
+
*
|
|
390
|
+
* @param charset A named charset alias (e.g. `'base58'`, `'hex'`) or a raw
|
|
391
|
+
* string of characters to draw from. Defaults to
|
|
392
|
+
* `'alphanumeric'`.
|
|
393
|
+
*
|
|
394
|
+
* @example
|
|
395
|
+
* random(16); // → 16 alphanumeric chars
|
|
396
|
+
* random(8, 'hex'); // → 8 hex chars
|
|
397
|
+
* random(4, 'ABC'); // → 4 chars from {A, B, C}
|
|
398
|
+
*/
|
|
399
|
+
declare function random(length: number, charset?: string): string;
|
|
400
|
+
/**
|
|
401
|
+
* Secure random string of ASCII letters (mixed case).
|
|
402
|
+
* @example randomAlpha(10); // → e.g. 'QwErTyUiOp'
|
|
403
|
+
*/
|
|
404
|
+
declare function randomAlpha(length: number): string;
|
|
405
|
+
/**
|
|
406
|
+
* Secure random string of digits.
|
|
407
|
+
* @example randomNumeric(6); // → e.g. '048213'
|
|
408
|
+
*/
|
|
409
|
+
declare function randomNumeric(length: number): string;
|
|
410
|
+
/**
|
|
411
|
+
* Secure random string of ASCII letters and digits.
|
|
412
|
+
* @example randomAlphanumeric(12);
|
|
413
|
+
*/
|
|
414
|
+
declare function randomAlphanumeric(length: number): string;
|
|
415
|
+
/**
|
|
416
|
+
* Compute a normalized similarity ratio in `[0, 1]` between two strings,
|
|
417
|
+
* based on Levenshtein edit distance (`1` = identical, `0` = nothing in
|
|
418
|
+
* common). Two empty strings are considered identical.
|
|
419
|
+
*
|
|
420
|
+
* @example
|
|
421
|
+
* similarity('kitten', 'sitting'); // → ~0.571
|
|
422
|
+
* similarity('abc', 'abc'); // → 1
|
|
423
|
+
*/
|
|
424
|
+
declare function similarity(a: string, b: string): number;
|
|
425
|
+
/**
|
|
426
|
+
* Transliterate accented and special Latin characters to their closest ASCII
|
|
427
|
+
* equivalents. Diacritics are stripped via Unicode NFKD normalization; a small
|
|
428
|
+
* map handles ligatures and letters that don't decompose (ß, æ, ø, …).
|
|
429
|
+
*
|
|
430
|
+
* Non-Latin scripts are left unchanged.
|
|
431
|
+
*
|
|
432
|
+
* @example
|
|
433
|
+
* transliterate('Héllo Wörld'); // → 'Hello World'
|
|
434
|
+
* transliterate('Straße'); // → 'Strasse'
|
|
435
|
+
* transliterate('naïve café'); // → 'naive cafe'
|
|
436
|
+
*/
|
|
437
|
+
declare function transliterate(str: string): string;
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* str/padding.ts
|
|
441
|
+
* Padding and indentation helpers.
|
|
442
|
+
*/
|
|
443
|
+
/**
|
|
444
|
+
* Left-pad a string to `length` using `char` (default `' '`). Returns the
|
|
445
|
+
* string unchanged if it is already at least `length`.
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* padLeft('7', 3, '0'); // → '007'
|
|
449
|
+
* padLeft('hi', 5); // → ' hi'
|
|
450
|
+
*/
|
|
451
|
+
declare function padLeft(str: string, length: number, char?: string): string;
|
|
452
|
+
/**
|
|
453
|
+
* Right-pad a string to `length` using `char` (default `' '`).
|
|
454
|
+
*
|
|
455
|
+
* @example
|
|
456
|
+
* padRight('7', 3, '0'); // → '700'
|
|
457
|
+
*/
|
|
458
|
+
declare function padRight(str: string, length: number, char?: string): string;
|
|
459
|
+
/**
|
|
460
|
+
* Center a string within `length`, padding both sides with `char`. When the
|
|
461
|
+
* padding can't be split evenly, the extra character goes on the right.
|
|
462
|
+
*
|
|
463
|
+
* @example
|
|
464
|
+
* padCenter('hi', 6); // → ' hi '
|
|
465
|
+
* padCenter('hi', 7, '*'); // → '**hi***'
|
|
466
|
+
*/
|
|
467
|
+
declare function padCenter(str: string, length: number, char?: string): string;
|
|
468
|
+
/**
|
|
469
|
+
* Indent every line of a string by `count` repetitions of `char`
|
|
470
|
+
* (default: 2 spaces). Blank lines are left untouched by default.
|
|
471
|
+
*
|
|
472
|
+
* @param char The indentation unit. @default ' '
|
|
473
|
+
* @param indentEmpty When `true`, blank lines are indented too. @default false
|
|
474
|
+
*
|
|
475
|
+
* @example
|
|
476
|
+
* indent('a\nb'); // → ' a\n b'
|
|
477
|
+
* indent('a\nb', 1, '\t'); // → '\ta\n\tb'
|
|
478
|
+
*/
|
|
479
|
+
declare function indent(str: string, count?: number, char?: string, indentEmpty?: boolean): string;
|
|
480
|
+
/**
|
|
481
|
+
* Remove the common leading whitespace from every line — the inverse of
|
|
482
|
+
* `indent`, useful for tidying template literals. Fully-blank lines are ignored
|
|
483
|
+
* when computing the common indent, and leading/trailing blank lines are
|
|
484
|
+
* trimmed.
|
|
485
|
+
*
|
|
486
|
+
* @example
|
|
487
|
+
* dedent(' a\n b'); // → 'a\nb'
|
|
488
|
+
* dedent('\n foo\n bar\n'); // → 'foo\n bar'
|
|
489
|
+
*/
|
|
490
|
+
declare function dedent(str: string): string;
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* str/security.ts
|
|
494
|
+
* Escaping, sanitizing, and safe-affix helpers.
|
|
495
|
+
*
|
|
496
|
+
* NOTE: `escapeHtml` / `stripTags` provide basic protection for text
|
|
497
|
+
* interpolation. They are NOT a substitute for a full HTML sanitizer (e.g.
|
|
498
|
+
* DOMPurify) when rendering untrusted rich markup.
|
|
499
|
+
*/
|
|
500
|
+
/**
|
|
501
|
+
* Escape the five characters that are significant in HTML (`& < > " '`) so a
|
|
502
|
+
* string can be safely interpolated into markup as text.
|
|
503
|
+
*
|
|
504
|
+
* @example
|
|
505
|
+
* escapeHtml('<a href="x">Tom & Jerry</a>');
|
|
506
|
+
* // → '<a href="x">Tom & Jerry</a>'
|
|
507
|
+
*/
|
|
508
|
+
declare function escapeHtml(str: string): string;
|
|
509
|
+
/**
|
|
510
|
+
* Reverse `escapeHtml`, decoding the common named and numeric HTML entities.
|
|
511
|
+
*
|
|
512
|
+
* @example
|
|
513
|
+
* unescapeHtml('<b>hi</b>'); // → '<b>hi</b>'
|
|
514
|
+
*/
|
|
515
|
+
declare function unescapeHtml(str: string): string;
|
|
516
|
+
/**
|
|
517
|
+
* Escape a string so it can be used literally inside a `RegExp` — every
|
|
518
|
+
* regex-special character is backslash-escaped.
|
|
519
|
+
*
|
|
520
|
+
* @example
|
|
521
|
+
* escapeRegExp('a.b*c'); // → 'a\\.b\\*c'
|
|
522
|
+
* new RegExp(escapeRegExp('$5.00')).test('$5.00'); // → true
|
|
523
|
+
*/
|
|
524
|
+
declare function escapeRegExp(str: string): string;
|
|
525
|
+
/**
|
|
526
|
+
* Strip all HTML/XML tags from a string, leaving the text content.
|
|
527
|
+
*
|
|
528
|
+
* @example
|
|
529
|
+
* stripTags('<p>Hello <b>world</b></p>'); // → 'Hello world'
|
|
530
|
+
*/
|
|
531
|
+
declare function stripTags(str: string): string;
|
|
532
|
+
/**
|
|
533
|
+
* Collapse all runs of whitespace (spaces, tabs, newlines) into a single
|
|
534
|
+
* space and trim the ends.
|
|
535
|
+
*
|
|
536
|
+
* @example
|
|
537
|
+
* collapseWhitespace(' a\n\t b c '); // → 'a b c'
|
|
538
|
+
*/
|
|
539
|
+
declare function collapseWhitespace(str: string): string;
|
|
540
|
+
/**
|
|
541
|
+
* Remove `prefix` from the start of `str`, if present. Idempotent.
|
|
542
|
+
*
|
|
543
|
+
* @example
|
|
544
|
+
* stripPrefix('usr_123', 'usr_'); // → '123'
|
|
545
|
+
* stripPrefix('123', 'usr_'); // → '123'
|
|
546
|
+
*/
|
|
547
|
+
declare function stripPrefix(str: string, prefix: string): string;
|
|
548
|
+
/**
|
|
549
|
+
* Remove `suffix` from the end of `str`, if present. Idempotent.
|
|
550
|
+
*
|
|
551
|
+
* @example
|
|
552
|
+
* stripSuffix('report.txt', '.txt'); // → 'report'
|
|
553
|
+
* stripSuffix('report', '.txt'); // → 'report'
|
|
554
|
+
*/
|
|
555
|
+
declare function stripSuffix(str: string, suffix: string): string;
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* str/ensuring.ts
|
|
559
|
+
* Idempotent affix helpers — guarantee a string starts/ends a certain way
|
|
560
|
+
* without duplicating an affix that is already present.
|
|
561
|
+
*/
|
|
562
|
+
/**
|
|
563
|
+
* Ensure `str` starts with `prefix`, prepending it only if missing. Idempotent.
|
|
564
|
+
*
|
|
565
|
+
* @example
|
|
566
|
+
* ensureLeft('example.com', 'https://'); // → 'https://example.com'
|
|
567
|
+
* ensureLeft('https://example.com', 'https://'); // → 'https://example.com'
|
|
568
|
+
*/
|
|
569
|
+
declare function ensureLeft(str: string, prefix: string): string;
|
|
570
|
+
/**
|
|
571
|
+
* Ensure `str` ends with `suffix`, appending it only if missing. Idempotent.
|
|
572
|
+
*
|
|
573
|
+
* @example
|
|
574
|
+
* ensureRight('path', '/'); // → 'path/'
|
|
575
|
+
* ensureRight('path/', '/'); // → 'path/'
|
|
576
|
+
*/
|
|
577
|
+
declare function ensureRight(str: string, suffix: string): string;
|
|
578
|
+
|
|
579
|
+
export { type MaskOptions, type TemplateOptions, between, betweenAll, camelize, capitalize, capitalizeWords, collapseWhitespace, contains, containsAll, containsAny, count, dasherize, dedent, ensureLeft, ensureRight, escapeHtml, escapeRegExp, humanize, indent, insert, isAlpha, isAlphaNumeric, isBlank, isEmpty, isLowerCase, isNumeric, isString, isUpperCase, join, mask, ordinalize, padCenter, padLeft, padRight, pascalize, pluralize, random, randomAlpha, randomAlphanumeric, randomNumeric, reverse, similarity, slugify, stripPrefix, stripSuffix, stripTags, template, titleCase, transliterate, trim, truncate, truncateWords, underscore, unescapeHtml, wordCount, words };
|