@tolki/str 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +7 -0
- package/dist/ascii/index.d.ts +25 -0
- package/dist/ascii/index.d.ts.map +1 -0
- package/dist/ascii/index.js +46 -0
- package/dist/base64/index.d.ts +23 -0
- package/dist/base64/index.d.ts.map +1 -0
- package/dist/base64/index.js +124 -0
- package/dist/convertcase/index.d.ts +48 -0
- package/dist/convertcase/index.d.ts.map +1 -0
- package/dist/convertcase/index.js +49 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +138 -0
- package/dist/markdown/index.d.ts +49 -0
- package/dist/markdown/index.d.ts.map +1 -0
- package/dist/markdown/index.js +32 -0
- package/dist/pluralizer/index.d.ts +100 -0
- package/dist/pluralizer/index.d.ts.map +1 -0
- package/dist/pluralizer/index.js +99 -0
- package/dist/random/index.d.ts +28 -0
- package/dist/random/index.d.ts.map +1 -0
- package/dist/random/index.js +45 -0
- package/dist/replacer/index.d.ts +31 -0
- package/dist/replacer/index.d.ts.map +1 -0
- package/dist/replacer/index.js +42 -0
- package/dist/str.d.ts +968 -0
- package/dist/str.d.ts.map +1 -0
- package/dist/str.js +939 -0
- package/dist/stringable/index.d.ts +952 -0
- package/dist/stringable/index.d.ts.map +1 -0
- package/dist/stringable/index.js +1249 -0
- package/dist/transliterate/index.d.ts +14 -0
- package/dist/transliterate/index.d.ts.map +1 -0
- package/dist/transliterate/index.js +7 -0
- package/dist/trimmer/index.d.ts +25 -0
- package/dist/trimmer/index.d.ts.map +1 -0
- package/dist/trimmer/index.js +128 -0
- package/dist/ulid/index.d.ts +79 -0
- package/dist/ulid/index.d.ts.map +1 -0
- package/dist/ulid/index.js +58 -0
- package/dist/uuid/index.d.ts +92 -0
- package/dist/uuid/index.d.ts.map +1 -0
- package/dist/uuid/index.js +50 -0
- package/package.json +126 -0
|
@@ -0,0 +1,1249 @@
|
|
|
1
|
+
import { after as p, afterLast as d, ascii as f, before as m, beforeLast as _, between as E, betweenFirst as y, camel as A, charAt as W, chopStart as x, chopEnd as N, contains as k, containsAll as U, doesntContain as C, convertCase as L, deduplicate as M, endsWith as F, doesntEndWith as I, excerpt as B, finish as R, is as j, isAscii as D, isJson as T, isUrl as P, isUuid as q, isUlid as J, kebab as V, length as $, limit as z, lower as w, markdown as G, inlineMarkdown as X, mask as H, match as K, isMatch as O, matchAll as Q, numbers as Y, padBoth as Z, padLeft as g, padRight as b, plural as S, pluralStudly as ee, pluralPascal as te, position as se, remove as re, reverse as ne, repeat as ue, replace as he, replaceArray as ie, replaceFirst as le, replaceStart as ae, replaceLast as we, replaceEnd as oe, replaceMatches as ve, squish as ce, start as pe, stripTags as de, upper as fe, title as me, headline as _e, apa as Ee, transliterate as ye, singular as Ae, slug as We, snake as xe, startsWith as Ne, doesntStartWith as ke, studly as Ue, pascal as Ce, substr as Le, substrCount as Me, substrReplace as Fe, swap as Ie, take as Be, trim as o, ltrim as Re, rtrim as je, lcfirst as De, ucfirst as Te, ucsplit as Pe, ucwords as qe, words as Je, wordCount as Ve, wordWrap as $e, wrap as ze, unwrap as Ge, toBase64 as Xe, fromBase64 as He } from "@tolki/str";
|
|
2
|
+
import { isInteger as Ke, isArray as v, isFunction as c } from "@tolki/utils";
|
|
3
|
+
function Ye(a) {
|
|
4
|
+
return new s(a);
|
|
5
|
+
}
|
|
6
|
+
function Ze(a) {
|
|
7
|
+
return new s(a);
|
|
8
|
+
}
|
|
9
|
+
class s {
|
|
10
|
+
/**
|
|
11
|
+
* Create a new instance of the class.
|
|
12
|
+
*
|
|
13
|
+
* @param _value - The initial string value. Defaults to an empty string.
|
|
14
|
+
*/
|
|
15
|
+
constructor(e = "") {
|
|
16
|
+
this._value = e;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Return the remainder of a string after the first occurrence of a given value.
|
|
20
|
+
*
|
|
21
|
+
* @param search - The substring to search for.
|
|
22
|
+
* @returns The portion of the string after the first occurrence of the search value.
|
|
23
|
+
*/
|
|
24
|
+
after(e) {
|
|
25
|
+
return new s(p(this._value, e));
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Return the remainder of a string after the last occurrence of a given value
|
|
29
|
+
*
|
|
30
|
+
* @param search - The substring to search for.
|
|
31
|
+
* @returns The portion of the string after the last occurrence of the search value.
|
|
32
|
+
*/
|
|
33
|
+
afterLast(e) {
|
|
34
|
+
return new s(d(this._value, e));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Append the given values to the string.
|
|
38
|
+
*
|
|
39
|
+
* @param values - The values to append to the string.
|
|
40
|
+
* @returns The updated Stringable instance.
|
|
41
|
+
*/
|
|
42
|
+
append(...e) {
|
|
43
|
+
return new s(this._value + e.map(String).join(""));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Append a new line to the string.
|
|
47
|
+
*
|
|
48
|
+
* @param count - The number of new lines to append. Defaults to 1.
|
|
49
|
+
* @returns The updated Stringable instance.
|
|
50
|
+
*/
|
|
51
|
+
newLine(e = 1) {
|
|
52
|
+
return this.append(`
|
|
53
|
+
`.repeat(Math.max(0, e)));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Transliterate a UTF-8 value to ASCII.
|
|
57
|
+
*
|
|
58
|
+
* @returns The transliterated string as a new Stringable instance.
|
|
59
|
+
*/
|
|
60
|
+
ascii() {
|
|
61
|
+
return new s(f(this._value));
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Get the portion of a string before the first occurrence of a given value.
|
|
65
|
+
*
|
|
66
|
+
* @param search - The substring to search for.
|
|
67
|
+
* @returns The portion of the string before the first occurrence of the search value.
|
|
68
|
+
*/
|
|
69
|
+
before(e) {
|
|
70
|
+
return new s(m(this._value, e));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Get the portion of a string before the last occurrence of a given value.
|
|
74
|
+
*
|
|
75
|
+
* @param search - The substring to search for.
|
|
76
|
+
* @returns The portion of the string before the last occurrence of the search value.
|
|
77
|
+
*/
|
|
78
|
+
beforeLast(e) {
|
|
79
|
+
return new s(_(this._value, e));
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get the portion of a string between two given values.
|
|
83
|
+
*
|
|
84
|
+
* @param from - The starting substring.
|
|
85
|
+
* @param to - The ending substring.
|
|
86
|
+
* @returns The portion of the string between the two given values.
|
|
87
|
+
*/
|
|
88
|
+
between(e, t) {
|
|
89
|
+
return new s(E(this._value, e, t));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Get the smallest possible portion of a string between two given values.
|
|
93
|
+
*
|
|
94
|
+
* @param from - The starting substring.
|
|
95
|
+
* @param to - The ending substring.
|
|
96
|
+
* @returns The smallest possible portion of the string between the two given values.
|
|
97
|
+
*/
|
|
98
|
+
betweenFirst(e, t) {
|
|
99
|
+
return new s(y(this._value, e, t));
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Convert a value to camel case.
|
|
103
|
+
*
|
|
104
|
+
* @returns The camel-cased string as a new Stringable instance.
|
|
105
|
+
*/
|
|
106
|
+
camel() {
|
|
107
|
+
return new s(A(this._value));
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Get the character at the specified index.
|
|
111
|
+
*
|
|
112
|
+
* @param index - The index of the character to retrieve.
|
|
113
|
+
* @returns The character at the specified index, or false if the index is out of bounds.
|
|
114
|
+
*/
|
|
115
|
+
charAt(e) {
|
|
116
|
+
return W(this._value, e);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Remove the given string if it exists at the start of the current string.
|
|
120
|
+
*
|
|
121
|
+
* @param needle - The string or array of strings to remove from the start.
|
|
122
|
+
* @returns The updated Stringable instance.
|
|
123
|
+
*/
|
|
124
|
+
chopStart(e) {
|
|
125
|
+
return new s(x(this._value, e));
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Remove the given string if it exists at the end of the current string.
|
|
129
|
+
*
|
|
130
|
+
* @param needle - The string or array of strings to remove from the end.
|
|
131
|
+
* @returns The updated Stringable instance.
|
|
132
|
+
*/
|
|
133
|
+
chopEnd(e) {
|
|
134
|
+
return new s(N(this._value, e));
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Determine if a given string contains a given substring.
|
|
138
|
+
*
|
|
139
|
+
* @param needles - The substring(s) to search for
|
|
140
|
+
* @param ignoreCase - Whether the search should be case-insensitive
|
|
141
|
+
* @returns boolean - True if the substring(s) are found, false otherwise
|
|
142
|
+
*/
|
|
143
|
+
contains(e, t = !1) {
|
|
144
|
+
return k(this._value, e, t);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Determine if a given string contains all array values.
|
|
148
|
+
*
|
|
149
|
+
* @param needles - The substring(s) to search for
|
|
150
|
+
* @param ignoreCase - Whether the search should be case-insensitive
|
|
151
|
+
* @returns boolean - True if all substring(s) are found, false otherwise
|
|
152
|
+
*/
|
|
153
|
+
containsAll(e, t = !1) {
|
|
154
|
+
return U(this._value, e, t);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Determine if a given string doesn't contain a given substring.
|
|
158
|
+
*
|
|
159
|
+
* @param needles - The substring(s) to search for
|
|
160
|
+
* @param ignoreCase - Whether the search should be case-insensitive
|
|
161
|
+
* @returns boolean - True if the substring(s) are not found, false otherwise
|
|
162
|
+
*/
|
|
163
|
+
doesntContain(e, t = !1) {
|
|
164
|
+
return C(this._value, e, t);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Convert the case of a string.
|
|
168
|
+
*
|
|
169
|
+
* @param mode - The case conversion mode to apply.
|
|
170
|
+
* @returns The converted string as a new Stringable instance.
|
|
171
|
+
*/
|
|
172
|
+
convertCase(e) {
|
|
173
|
+
return new s(L(this._value, e));
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Replace consecutive instances of a given character with a single character.
|
|
177
|
+
*
|
|
178
|
+
* @param character - The character or array of characters to deduplicate. Defaults to a space.
|
|
179
|
+
* @returns The updated Stringable instance.
|
|
180
|
+
*/
|
|
181
|
+
deduplicate(e = " ") {
|
|
182
|
+
return new s(M(this._value, e));
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Determine if a given string ends with a given substring.
|
|
186
|
+
*
|
|
187
|
+
* @param needles - The substring(s) to search for
|
|
188
|
+
* @returns boolean - True if the substring(s) are found, false otherwise
|
|
189
|
+
*/
|
|
190
|
+
endsWith(e) {
|
|
191
|
+
return F(this._value, e);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Determine if a given string doesn't end with a given substring.
|
|
195
|
+
*
|
|
196
|
+
* @param needles - The substring(s) to search for
|
|
197
|
+
* @returns boolean - True if the substring(s) are not found, false otherwise
|
|
198
|
+
*/
|
|
199
|
+
doesntEndWith(e) {
|
|
200
|
+
return I(this._value, e);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Determine if the string is an exact match with the given value.
|
|
204
|
+
*
|
|
205
|
+
* @param value - The value to compare against.
|
|
206
|
+
* @returns True if the strings are exactly the same, false otherwise.
|
|
207
|
+
*/
|
|
208
|
+
exactly(e) {
|
|
209
|
+
const t = e instanceof s ? e.toString() : String(e);
|
|
210
|
+
return this._value === t;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Extracts an excerpt from text that matches the first instance of a phrase.
|
|
214
|
+
*
|
|
215
|
+
* @param phrase - The phrase to search for within the text.
|
|
216
|
+
* @param options - Options to customize the excerpt extraction, including radius and omission string.
|
|
217
|
+
* @returns The extracted excerpt or null if the phrase is not found.
|
|
218
|
+
*/
|
|
219
|
+
excerpt(e = "", t = {}) {
|
|
220
|
+
return B(this._value, e, t);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Explode the string into an array
|
|
224
|
+
*
|
|
225
|
+
* @param delimiter - The delimiter string to split the string by.
|
|
226
|
+
* @param limit - The maximum number of elements to return. Defaults to Number.MAX_SAFE_INTEGER.
|
|
227
|
+
* @returns An array of strings obtained by splitting the original string.
|
|
228
|
+
*/
|
|
229
|
+
explode(e, t = Number.MAX_SAFE_INTEGER) {
|
|
230
|
+
const r = this._value.split(e);
|
|
231
|
+
if (t > 0 && r.length > t) {
|
|
232
|
+
const n = r.slice(0, t - 1);
|
|
233
|
+
return n.push(r.slice(t - 1).join(e)), n;
|
|
234
|
+
} else if (t < 0)
|
|
235
|
+
return r.slice(0, t);
|
|
236
|
+
return r;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Split a string using a regular expression or by length.
|
|
240
|
+
*
|
|
241
|
+
* @param pattern - The regex pattern or length to split the string by.
|
|
242
|
+
* @param limit - The maximum number of splits to perform. Defaults to null (no limit).
|
|
243
|
+
* @returns An array of strings obtained by splitting the original string.
|
|
244
|
+
*/
|
|
245
|
+
split(e, t = null) {
|
|
246
|
+
if (Ke(e)) {
|
|
247
|
+
const n = Number(e), h = [];
|
|
248
|
+
for (let i = 0; i < this._value.length; i += n)
|
|
249
|
+
h.push(this._value.substr(i, n));
|
|
250
|
+
return h;
|
|
251
|
+
}
|
|
252
|
+
return this._value.split(
|
|
253
|
+
new RegExp(String(e)),
|
|
254
|
+
t ?? void 0
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Cap a string with a single instance of a given value.
|
|
259
|
+
*
|
|
260
|
+
* @param cap - The string to cap the original string with.
|
|
261
|
+
* @returns The updated Stringable instance.
|
|
262
|
+
*/
|
|
263
|
+
finish(e) {
|
|
264
|
+
return new s(R(this._value, e));
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Determine if a given string matches a given pattern.
|
|
268
|
+
*
|
|
269
|
+
* @param pattern - The pattern(s) to match against
|
|
270
|
+
* @returns boolean - True if the pattern(s) match, false otherwise
|
|
271
|
+
*/
|
|
272
|
+
is(e, t = !1) {
|
|
273
|
+
return j(e, this._value, t);
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Determine if a given string is 7 bit ASCII.
|
|
277
|
+
*
|
|
278
|
+
* @returns True if the string is ASCII, false otherwise.
|
|
279
|
+
*/
|
|
280
|
+
isAscii() {
|
|
281
|
+
return D(this._value);
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Determine if a given string is valid JSON.
|
|
285
|
+
*
|
|
286
|
+
* @returns True if the string is valid JSON, false otherwise.
|
|
287
|
+
*/
|
|
288
|
+
isJson() {
|
|
289
|
+
return T(this._value);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Determine if a given value is a valid URL.
|
|
293
|
+
*
|
|
294
|
+
* @param protocols - An array of allowed protocols (e.g., ['http', 'https']).
|
|
295
|
+
* @returns True if the string is a valid URL, false otherwise.
|
|
296
|
+
*/
|
|
297
|
+
isUrl(e = []) {
|
|
298
|
+
return P(this._value, e);
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Determine if a given string is a valid UUID.
|
|
302
|
+
*
|
|
303
|
+
* @param version - The UUID version to validate against (1-5), "nil", "max", or null for any version.
|
|
304
|
+
* @returns True if the string is a valid UUID, false otherwise.
|
|
305
|
+
*/
|
|
306
|
+
isUuid(e = null) {
|
|
307
|
+
return q(this._value, e);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Determine if a given string is a valid ULID.
|
|
311
|
+
*
|
|
312
|
+
* @return True if the string is a valid ULID, false otherwise.
|
|
313
|
+
*/
|
|
314
|
+
isUlid() {
|
|
315
|
+
return J(this._value);
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Determine if the given string is empty.
|
|
319
|
+
*
|
|
320
|
+
* @return True if the string is empty, false otherwise.
|
|
321
|
+
*/
|
|
322
|
+
isEmpty() {
|
|
323
|
+
return this._value === "";
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Determine if the given string is not empty.
|
|
327
|
+
*
|
|
328
|
+
* @return True if the string is not empty, false otherwise.
|
|
329
|
+
*/
|
|
330
|
+
isNotEmpty() {
|
|
331
|
+
return !this.isEmpty();
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Convert a string to kebab case.
|
|
335
|
+
*
|
|
336
|
+
* @returns The kebab-cased string as a new Stringable instance.
|
|
337
|
+
*/
|
|
338
|
+
kebab() {
|
|
339
|
+
return new s(V(this._value));
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Return the length of the given string.
|
|
343
|
+
*
|
|
344
|
+
* @returns The length of the string.
|
|
345
|
+
*/
|
|
346
|
+
length() {
|
|
347
|
+
return $(this._value);
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Limit the number of characters in a string.
|
|
351
|
+
*
|
|
352
|
+
* @param limitValue - The maximum number of characters allowed.
|
|
353
|
+
* @param end - The string to append if the original string exceeds the limit.
|
|
354
|
+
* @param preserveWords - Whether to avoid cutting off in the middle of a word.
|
|
355
|
+
* @returns A new Stringable instance with the limited string.
|
|
356
|
+
*/
|
|
357
|
+
limit(e = 100, t = "...", r = !1) {
|
|
358
|
+
return new s(
|
|
359
|
+
z(this._value, e, t, r)
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Convert the given string to lower-case.
|
|
364
|
+
*
|
|
365
|
+
* @returns The lower-cased string as a new Stringable instance.
|
|
366
|
+
*/
|
|
367
|
+
lower() {
|
|
368
|
+
return new s(w(this._value));
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Convert GitHub flavored Markdown into HTML.
|
|
372
|
+
*
|
|
373
|
+
* @param options - Options to customize the markdown rendering. Defaults to GFM enabled and no anchors.
|
|
374
|
+
* @param extensions - An array of markdown-it extensions to apply during rendering.
|
|
375
|
+
* @returns The resulting HTML string as a new Stringable instance.
|
|
376
|
+
*/
|
|
377
|
+
markdown(e = { gfm: !0, anchors: !1 }, t = []) {
|
|
378
|
+
return new s(G(this._value, e, t));
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Convert inline Markdown into HTML.
|
|
382
|
+
*
|
|
383
|
+
* @param options - Options to customize the markdown rendering. Defaults to GFM enabled.
|
|
384
|
+
* @param extensions - An array of markdown-it extensions to apply during rendering.
|
|
385
|
+
* @returns The resulting HTML string as a new Stringable instance.
|
|
386
|
+
*/
|
|
387
|
+
inlineMarkdown(e = { gfm: !0 }, t = []) {
|
|
388
|
+
return new s(X(this._value, e, t));
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Masks a portion of a string with a repeated character.
|
|
392
|
+
*
|
|
393
|
+
* @param character - The character to use for masking.
|
|
394
|
+
* @param index - The starting index to begin masking.
|
|
395
|
+
* @param length - The number of characters to mask. If null, masks to the end of the string.
|
|
396
|
+
* @returns The masked string as a new Stringable instance.
|
|
397
|
+
*/
|
|
398
|
+
mask(e, t, r = null) {
|
|
399
|
+
return new s(H(this._value, e, t, r));
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Get the string matching the given pattern.
|
|
403
|
+
*
|
|
404
|
+
* @param pattern - The pattern to match against.
|
|
405
|
+
* @returns A new Stringable instance containing the matched string.
|
|
406
|
+
*/
|
|
407
|
+
match(e) {
|
|
408
|
+
return new s(K(e, this._value));
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Determine if a given string matches a given pattern.
|
|
412
|
+
*
|
|
413
|
+
* @param pattern - The pattern(s) to match against
|
|
414
|
+
* @returns boolean - True if the pattern(s) match, false otherwise
|
|
415
|
+
*/
|
|
416
|
+
isMatch(e) {
|
|
417
|
+
return O(e, this._value);
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Get the string matching the given pattern.
|
|
421
|
+
*
|
|
422
|
+
* @param pattern - The pattern to match against.
|
|
423
|
+
* @returns An array of all matched strings.
|
|
424
|
+
*/
|
|
425
|
+
matchAll(e) {
|
|
426
|
+
return Q(e, this._value);
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Determine if the string matches the given pattern.
|
|
430
|
+
*
|
|
431
|
+
* @param pattern - The pattern(s) to match against
|
|
432
|
+
* @returns boolean - True if the pattern(s) match, false otherwise
|
|
433
|
+
*/
|
|
434
|
+
test(e) {
|
|
435
|
+
return this.isMatch(e);
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Remove all non-numeric characters from a string.
|
|
439
|
+
*
|
|
440
|
+
* @returns The numeric string as a new Stringable instance.
|
|
441
|
+
*/
|
|
442
|
+
numbers() {
|
|
443
|
+
return new s(Y(this._value));
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Pad both sides of the string with another.
|
|
447
|
+
*
|
|
448
|
+
* @param length - The desired total length of the string after padding.
|
|
449
|
+
* @param pad - The string to use for padding. Defaults to a space.
|
|
450
|
+
* @returns The padded string as a new Stringable instance.
|
|
451
|
+
*/
|
|
452
|
+
padBoth(e, t = " ") {
|
|
453
|
+
return new s(Z(this._value, e, t));
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Pad the left side of the string with another.
|
|
457
|
+
*
|
|
458
|
+
* @param length - The desired total length of the string after padding.
|
|
459
|
+
* @param pad - The string to use for padding. Defaults to a space.
|
|
460
|
+
* @returns The padded string as a new Stringable instance.
|
|
461
|
+
*/
|
|
462
|
+
padLeft(e, t = " ") {
|
|
463
|
+
return new s(g(this._value, e, t));
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Pad the right side of the string with another.
|
|
467
|
+
*
|
|
468
|
+
* @param length - The desired total length of the string after padding.
|
|
469
|
+
* @param pad - The string to use for padding. Defaults to a space.
|
|
470
|
+
* @returns The padded string as a new Stringable instance.
|
|
471
|
+
*/
|
|
472
|
+
padRight(e, t = " ") {
|
|
473
|
+
return new s(b(this._value, e, t));
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Call the given callback and return a new string.
|
|
477
|
+
*
|
|
478
|
+
* @param callback - The callback function to process the string.
|
|
479
|
+
* @returns The processed string as a new Stringable instance.
|
|
480
|
+
*/
|
|
481
|
+
pipe(e) {
|
|
482
|
+
const t = e(this);
|
|
483
|
+
return t instanceof s ? t : new s(String(t));
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Get the plural form of an English word.
|
|
487
|
+
*
|
|
488
|
+
* @param count - The count to determine singular or plural form. Defaults to 2.
|
|
489
|
+
* @param prependCount - Whether to prepend the count to the result. Defaults to false.
|
|
490
|
+
* @returns The pluralized string as a new Stringable instance.
|
|
491
|
+
*/
|
|
492
|
+
plural(e = 2, t = !1) {
|
|
493
|
+
return new s(S(this._value, e, t));
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Pluralize the last word of an English, studly caps case string.
|
|
497
|
+
*
|
|
498
|
+
* @param count - The count to determine singular or plural form. Defaults to 2.
|
|
499
|
+
* @returns The pluralized string as a new Stringable instance.
|
|
500
|
+
*/
|
|
501
|
+
pluralStudly(e = 2) {
|
|
502
|
+
const t = v(e) ? e.length : Number(e);
|
|
503
|
+
return new s(ee(this._value, t));
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Pluralize the last word of an English, Pascal caps case string.
|
|
507
|
+
*
|
|
508
|
+
* @param count - The count to determine singular or plural form. Defaults to 2.
|
|
509
|
+
* @returns The pluralized string as a new Stringable instance.
|
|
510
|
+
*/
|
|
511
|
+
pluralPascal(e = 2) {
|
|
512
|
+
const t = v(e) ? e.length : Number(e);
|
|
513
|
+
return new s(te(this._value, t));
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Find the multi-byte safe position of the first occurrence of the given substring.
|
|
517
|
+
*
|
|
518
|
+
* @param needle - The substring to search for.
|
|
519
|
+
* @param offset - The offset from which to start the search. Defaults to 0.
|
|
520
|
+
* @returns The position of the first occurrence of the substring, or false if not found.
|
|
521
|
+
*/
|
|
522
|
+
position(e, t = 0) {
|
|
523
|
+
return se(this._value, e, t);
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Prepend the given values to the string.
|
|
527
|
+
*
|
|
528
|
+
* @param values - The values to prepend to the string.
|
|
529
|
+
* @returns The updated Stringable instance.
|
|
530
|
+
*/
|
|
531
|
+
prepend(...e) {
|
|
532
|
+
return new s(e.map(String).join("") + this._value);
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Remove any occurrence of the given string in the subject.
|
|
536
|
+
*
|
|
537
|
+
* @param search - The string or iterable of strings to remove.
|
|
538
|
+
* @param caseSensitive - Whether the search should be case-sensitive. Defaults to true.
|
|
539
|
+
* @returns The updated Stringable instance.
|
|
540
|
+
*/
|
|
541
|
+
remove(e, t = !0) {
|
|
542
|
+
return new s(
|
|
543
|
+
re(e, this._value, t)
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* Reverse the string.
|
|
548
|
+
*
|
|
549
|
+
* @returns The reversed string as a new Stringable instance.
|
|
550
|
+
*/
|
|
551
|
+
reverse() {
|
|
552
|
+
return new s(ne(this._value));
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Repeat the string.
|
|
556
|
+
*
|
|
557
|
+
* @param times - The number of times to repeat the string.
|
|
558
|
+
* @returns The repeated string as a new Stringable instance.
|
|
559
|
+
*/
|
|
560
|
+
repeat(e) {
|
|
561
|
+
return new s(ue(this._value, e));
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Replace the given value in the given string.
|
|
565
|
+
*
|
|
566
|
+
* @param search - The value or iterable of values to search for.
|
|
567
|
+
* @param replacement - The replacement value or iterable of values.
|
|
568
|
+
* @param caseSensitive - Whether the search should be case-sensitive. Defaults to true.
|
|
569
|
+
* @returns The updated Stringable instance.
|
|
570
|
+
*/
|
|
571
|
+
replace(e, t, r = !0) {
|
|
572
|
+
return new s(
|
|
573
|
+
he(e, t, this._value, r)
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Replace a given value in the string sequentially with an array.
|
|
578
|
+
*
|
|
579
|
+
* @param search - The value to search for.
|
|
580
|
+
* @param replace - The array or record of replacements.
|
|
581
|
+
* @returns The updated Stringable instance.
|
|
582
|
+
*/
|
|
583
|
+
replaceArray(e, t) {
|
|
584
|
+
return new s(ie(e, t, this._value));
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Replace the first occurrence of a given value in the string.
|
|
588
|
+
*
|
|
589
|
+
* @param search - The value to search for.
|
|
590
|
+
* @param replace - The replacement value.
|
|
591
|
+
* @returns The updated Stringable instance.
|
|
592
|
+
*/
|
|
593
|
+
replaceFirst(e, t) {
|
|
594
|
+
return new s(le(e, t, this._value));
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Replace the first occurrence of the given value if it appears at the start of the string.
|
|
598
|
+
*
|
|
599
|
+
* @param search - The value to search for.
|
|
600
|
+
* @param replace - The replacement value.
|
|
601
|
+
* @returns The updated Stringable instance.
|
|
602
|
+
*/
|
|
603
|
+
replaceStart(e, t) {
|
|
604
|
+
return new s(ae(e, t, this._value));
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Replace the last occurrence of a given value in the string.
|
|
608
|
+
*
|
|
609
|
+
* @param search - The value to search for.
|
|
610
|
+
* @param replace - The replacement value.
|
|
611
|
+
* @returns The updated Stringable instance.
|
|
612
|
+
*/
|
|
613
|
+
replaceLast(e, t) {
|
|
614
|
+
return new s(we(e, t, this._value));
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* Replace the last occurrence of a given value if it appears at the end of the string.
|
|
618
|
+
*
|
|
619
|
+
* @param search - The value to search for.
|
|
620
|
+
* @param replace - The replacement value.
|
|
621
|
+
* @returns The updated Stringable instance.
|
|
622
|
+
*/
|
|
623
|
+
replaceEnd(e, t) {
|
|
624
|
+
return new s(oe(e, t, this._value));
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Replace the patterns matching the given regular expression.
|
|
628
|
+
*
|
|
629
|
+
* @param pattern - The pattern(s) to search for.
|
|
630
|
+
* @param replace - The replacement string(s) or a callback function.
|
|
631
|
+
* @param limit - The maximum number of replacements to perform. Defaults to -1 (no limit).
|
|
632
|
+
* @returns The updated Stringable instance.
|
|
633
|
+
*/
|
|
634
|
+
replaceMatches(e, t, r = -1) {
|
|
635
|
+
const n = ve(e, t, this._value, r);
|
|
636
|
+
return new s(n);
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Parse input from a string to an array, according to a format.
|
|
640
|
+
*
|
|
641
|
+
* @param format - A format string like "%d", "%s", "%[^,],%s", etc.
|
|
642
|
+
* @returns Array of parsed values according to the format
|
|
643
|
+
*
|
|
644
|
+
* @example
|
|
645
|
+
*
|
|
646
|
+
* Str.of("SN/123456").scan("SN/%d") -> ["123456"]
|
|
647
|
+
* Str.of("Otwell, Taylor").scan("%[^,],%s") -> ["Otwell", "Taylor"]
|
|
648
|
+
* Str.of("filename.jpg").scan("%[^.].%s") -> ["filename", "jpg"]
|
|
649
|
+
*/
|
|
650
|
+
scan(e) {
|
|
651
|
+
let t = "", r = 0;
|
|
652
|
+
for (; r < e.length; )
|
|
653
|
+
if (e[r] === "%")
|
|
654
|
+
if (e[r + 1] === "[") {
|
|
655
|
+
let u = r + 2;
|
|
656
|
+
for (; u < e.length && e[u] !== "]"; )
|
|
657
|
+
u++;
|
|
658
|
+
const l = e.substring(r + 2, u);
|
|
659
|
+
t += "([" + l + "]+)", r = u + 1;
|
|
660
|
+
} else e[r + 1] === "d" ? (t += "\\s*(-?\\d+)", r += 2) : e[r + 1] === "s" ? (t += "\\s*(\\S+)", r += 2) : (t += e[r], r++);
|
|
661
|
+
else {
|
|
662
|
+
const u = e[r];
|
|
663
|
+
u && /[.+?^${}()|\\]/.test(u) ? t += "\\" + u : t += u, r++;
|
|
664
|
+
}
|
|
665
|
+
const n = new RegExp("^" + t + "$"), h = this._value.match(n);
|
|
666
|
+
if (!h)
|
|
667
|
+
return [];
|
|
668
|
+
const i = [];
|
|
669
|
+
for (let u = 1; u < h.length; u++) {
|
|
670
|
+
const l = h[u];
|
|
671
|
+
l && i.push(l);
|
|
672
|
+
}
|
|
673
|
+
return i;
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* Remove all "extra" blank space from the given string.
|
|
677
|
+
*
|
|
678
|
+
* @returns The updated Stringable instance.
|
|
679
|
+
*/
|
|
680
|
+
squish() {
|
|
681
|
+
return new s(ce(this._value));
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* Begin a string with a single instance of a given value.
|
|
685
|
+
*
|
|
686
|
+
* @param prefix - The string to start the original string with.
|
|
687
|
+
* @returns The updated Stringable instance.
|
|
688
|
+
*/
|
|
689
|
+
start(e) {
|
|
690
|
+
return new s(pe(this._value, e));
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Strip HTML and PHP tags from the given string.
|
|
694
|
+
*
|
|
695
|
+
* @returns The updated Stringable instance.
|
|
696
|
+
*/
|
|
697
|
+
stripTags() {
|
|
698
|
+
return new s(de(this._value));
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Convert the given string to upper-case.
|
|
702
|
+
*
|
|
703
|
+
* @returns The upper-cased string as a new Stringable instance.
|
|
704
|
+
*/
|
|
705
|
+
upper() {
|
|
706
|
+
return new s(fe(this._value));
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* Convert the given string to proper case.
|
|
710
|
+
*
|
|
711
|
+
* @returns The title-cased string as a new Stringable instance.
|
|
712
|
+
*/
|
|
713
|
+
title() {
|
|
714
|
+
return new s(me(this._value));
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* Convert the given string to proper case for each word.
|
|
718
|
+
*
|
|
719
|
+
* @returns The headline-cased string as a new Stringable instance.
|
|
720
|
+
*/
|
|
721
|
+
headline() {
|
|
722
|
+
return new s(_e(this._value));
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* Convert the given string to APA-style title case.
|
|
726
|
+
*
|
|
727
|
+
* @returns The APA title-cased string as a new Stringable instance.
|
|
728
|
+
*/
|
|
729
|
+
apa() {
|
|
730
|
+
return new s(Ee(this._value));
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* Transliterate a string to its closest ASCII representation.
|
|
734
|
+
*
|
|
735
|
+
* @returns The transliterated string as a new Stringable instance.
|
|
736
|
+
*/
|
|
737
|
+
transliterate() {
|
|
738
|
+
return new s(ye(this._value));
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* Get the singular form of an English word.
|
|
742
|
+
*
|
|
743
|
+
* @returns The singularized string as a new Stringable instance.
|
|
744
|
+
*/
|
|
745
|
+
singular() {
|
|
746
|
+
return new s(Ae(this._value));
|
|
747
|
+
}
|
|
748
|
+
/**
|
|
749
|
+
* Generate a URL friendly "slug" from a given string.
|
|
750
|
+
*
|
|
751
|
+
* @param separator - The separator to use in the slug. Defaults to "-".
|
|
752
|
+
* @param dictionary - A dictionary of characters to replace. Defaults to { "@": "at" }.
|
|
753
|
+
* @returns The slugified string as a new Stringable instance.
|
|
754
|
+
*/
|
|
755
|
+
slug(e = "-", t = { "@": "at" }) {
|
|
756
|
+
return new s(We(this._value, e, t));
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* Convert a string to snake case.
|
|
760
|
+
*
|
|
761
|
+
* @param delimiter - The delimiter to use in the snake case. Defaults to "_".
|
|
762
|
+
* @returns The snake-cased string as a new Stringable instance.
|
|
763
|
+
*/
|
|
764
|
+
snake(e = "_") {
|
|
765
|
+
return new s(xe(this._value, e));
|
|
766
|
+
}
|
|
767
|
+
/**
|
|
768
|
+
* Determine if a given string starts with a given substring.
|
|
769
|
+
*
|
|
770
|
+
* @param needles - The substring(s) to search for
|
|
771
|
+
* @returns boolean - True if the substring(s) are found, false otherwise
|
|
772
|
+
*/
|
|
773
|
+
startsWith(e) {
|
|
774
|
+
return Ne(this._value, e);
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Determine if a given string doesn't start with a given substring.
|
|
778
|
+
*
|
|
779
|
+
* @param needles - The substring(s) to search for
|
|
780
|
+
* @returns boolean - True if the substring(s) are not found, false otherwise
|
|
781
|
+
*/
|
|
782
|
+
doesntStartWith(e) {
|
|
783
|
+
return ke(this._value, e);
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* Convert a value to studly caps case.
|
|
787
|
+
*
|
|
788
|
+
* @returns The studly-cased string as a new Stringable instance.
|
|
789
|
+
*/
|
|
790
|
+
studly() {
|
|
791
|
+
return new s(Ue(this._value));
|
|
792
|
+
}
|
|
793
|
+
/**
|
|
794
|
+
* Convert the string to Pascal case.
|
|
795
|
+
*
|
|
796
|
+
* @returns The pascal-cased string as a new Stringable instance.
|
|
797
|
+
*/
|
|
798
|
+
pascal() {
|
|
799
|
+
return new s(Ce(this._value));
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Returns the portion of the string specified by the start and length parameters.
|
|
803
|
+
*
|
|
804
|
+
* @param start - The starting position of the substring.
|
|
805
|
+
* @param length - The length of the substring. If null, extracts to the end of the string.
|
|
806
|
+
* @returns The extracted substring as a new Stringable instance.
|
|
807
|
+
*/
|
|
808
|
+
substr(e, t = null) {
|
|
809
|
+
return new s(Le(this._value, e, t));
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* Returns the number of substring occurrences.
|
|
813
|
+
*
|
|
814
|
+
* @param needle - The substring to search for.
|
|
815
|
+
* @param offset - The offset to start searching from. Defaults to 0.
|
|
816
|
+
* @param length - The length of the string to search within. If null, searches to the end of the string.
|
|
817
|
+
* @returns The number of occurrences of the substring.
|
|
818
|
+
*/
|
|
819
|
+
substrCount(e, t = 0, r = null) {
|
|
820
|
+
return Me(this._value, e, t, r);
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* Replace text within a portion of a string.
|
|
824
|
+
*
|
|
825
|
+
* @param replace - The replacement string.
|
|
826
|
+
* @param offset - The starting position to begin replacing.
|
|
827
|
+
* @param length - The number of characters to replace. If null, replaces to the end of the string.
|
|
828
|
+
* @returns The updated Stringable instance.
|
|
829
|
+
*/
|
|
830
|
+
substrReplace(e, t = 0, r = null) {
|
|
831
|
+
return new s(
|
|
832
|
+
Fe(this._value, e, t, r)
|
|
833
|
+
);
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* Swap multiple keywords in a string with other keywords.
|
|
837
|
+
*
|
|
838
|
+
* @param map - A record of keywords to swap (key: search, value: replacement).
|
|
839
|
+
* @returns The updated Stringable instance.
|
|
840
|
+
*/
|
|
841
|
+
swap(e) {
|
|
842
|
+
return new s(Ie(e, this._value));
|
|
843
|
+
}
|
|
844
|
+
/**
|
|
845
|
+
* Take the first or last {$limit} characters.
|
|
846
|
+
*
|
|
847
|
+
* @param limit - The number of characters to take. Positive for start, negative for end.
|
|
848
|
+
* @returns The resulting substring as a new Stringable instance.
|
|
849
|
+
*/
|
|
850
|
+
take(e) {
|
|
851
|
+
return new s(Be(this._value, e));
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* Trim the string of the given characters.
|
|
855
|
+
*
|
|
856
|
+
* @param charlist - The characters to trim from the string. If null, trims whitespace.
|
|
857
|
+
* @returns The trimmed string as a new Stringable instance.
|
|
858
|
+
*/
|
|
859
|
+
trim(e = null) {
|
|
860
|
+
return new s(o(this._value, e));
|
|
861
|
+
}
|
|
862
|
+
/**
|
|
863
|
+
* Left trim the string of the given characters.
|
|
864
|
+
*
|
|
865
|
+
* @param charlist - The characters to trim from the start of the string. If null, trims whitespace.
|
|
866
|
+
* @returns The left-trimmed string as a new Stringable instance.
|
|
867
|
+
*/
|
|
868
|
+
ltrim(e = null) {
|
|
869
|
+
return new s(Re(this._value, e));
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* Right trim the string of the given characters.
|
|
873
|
+
*
|
|
874
|
+
* @param charlist - The characters to trim from the end of the string. If null, trims whitespace.
|
|
875
|
+
* @returns The right-trimmed string as a new Stringable instance.
|
|
876
|
+
*/
|
|
877
|
+
rtrim(e = null) {
|
|
878
|
+
return new s(je(this._value, e));
|
|
879
|
+
}
|
|
880
|
+
/**
|
|
881
|
+
* Make a string's first character lowercase.
|
|
882
|
+
*
|
|
883
|
+
* @returns The updated Stringable instance.
|
|
884
|
+
*/
|
|
885
|
+
lcfirst() {
|
|
886
|
+
return new s(De(this._value));
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Make a string's first character uppercase.
|
|
890
|
+
*
|
|
891
|
+
* @returns The updated Stringable instance.
|
|
892
|
+
*/
|
|
893
|
+
ucfirst() {
|
|
894
|
+
return new s(Te(this._value));
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* Split a string by uppercase characters.
|
|
898
|
+
*
|
|
899
|
+
* @returns An array of substrings split at uppercase characters.
|
|
900
|
+
*/
|
|
901
|
+
ucsplit() {
|
|
902
|
+
return Pe(this._value);
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* Uppercase the first character of each word in a string.
|
|
906
|
+
*
|
|
907
|
+
* @returns The updated Stringable instance.
|
|
908
|
+
*/
|
|
909
|
+
ucwords() {
|
|
910
|
+
return new s(qe(this._value));
|
|
911
|
+
}
|
|
912
|
+
/**
|
|
913
|
+
* Apply the callback if the given "value" is (or resolves to) truthy.
|
|
914
|
+
*
|
|
915
|
+
* @param value - The value to evaluate or a closure that returns the value
|
|
916
|
+
* @param callback - The callback to execute if the value is truthy
|
|
917
|
+
* @param defaultCallback - The callback to execute if the value is falsy
|
|
918
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
919
|
+
*
|
|
920
|
+
* @example
|
|
921
|
+
*
|
|
922
|
+
* ```typescript
|
|
923
|
+
* const str = new Stringable('hello world');
|
|
924
|
+
*
|
|
925
|
+
* Using a direct value
|
|
926
|
+
* str.when(true, s => s.upper()); // Returns 'HELLO WORLD'
|
|
927
|
+
* str.when(false, s => s.upper(), s => s.lower()); // Returns 'hello world'
|
|
928
|
+
*
|
|
929
|
+
* Using a closure to determine the value
|
|
930
|
+
* str.when(s => s.contains('world'), s => s.upper()); // Returns 'HELLO WORLD'
|
|
931
|
+
* str.when(s => s.contains('foo'), s => s.upper(), s => s.lower()); // Returns 'hello world'
|
|
932
|
+
* ```
|
|
933
|
+
*/
|
|
934
|
+
when(e, t = null, r = null) {
|
|
935
|
+
const n = c(e) ? e(this) : e;
|
|
936
|
+
return n ? t?.(this, n) ?? this : r ? r(this, n) ?? this : this;
|
|
937
|
+
}
|
|
938
|
+
/**
|
|
939
|
+
* Apply the callback if the given "value" is (or resolves to) falsy.
|
|
940
|
+
*
|
|
941
|
+
* @param value - The value to evaluate or a closure that returns the value
|
|
942
|
+
* @param callback - The callback to execute if the value is falsy
|
|
943
|
+
* @param defaultCallback - The callback to execute if the value is truthy
|
|
944
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
945
|
+
*
|
|
946
|
+
* @example
|
|
947
|
+
*
|
|
948
|
+
* const str = new Stringable('hello world');
|
|
949
|
+
* str.unless(true, s => s.upper()); // Returns 'hello world'
|
|
950
|
+
* str.unless(false, s => s.upper(), s => s.lower()); // Returns 'HELLO WORLD'
|
|
951
|
+
*/
|
|
952
|
+
unless(e, t = null, r = null) {
|
|
953
|
+
const n = c(e) ? e(this) : e;
|
|
954
|
+
if (n) {
|
|
955
|
+
if (r)
|
|
956
|
+
return r(this, n) ?? this;
|
|
957
|
+
} else return t?.(this, n) ?? this;
|
|
958
|
+
return this;
|
|
959
|
+
}
|
|
960
|
+
/**
|
|
961
|
+
* Execute the given callback if the string contains a given substring.
|
|
962
|
+
*
|
|
963
|
+
* @param needles - The substring(s) to search for
|
|
964
|
+
* @param callback - The callback to execute if the substring(s) are found
|
|
965
|
+
* @param defaultCallback - The callback to execute if the substring(s) are not found
|
|
966
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
967
|
+
*/
|
|
968
|
+
whenContains(e, t, r = null) {
|
|
969
|
+
return this.when(this.contains(e), t, r);
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* Execute the given callback if the string contains all array values.
|
|
973
|
+
*
|
|
974
|
+
* @param needles - The substring(s) to search for
|
|
975
|
+
* @param callback - The callback to execute if all substring(s) are found
|
|
976
|
+
* @param defaultCallback - The callback to execute if not all substring(s) are found
|
|
977
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
978
|
+
*/
|
|
979
|
+
whenContainsAll(e, t, r = null) {
|
|
980
|
+
return this.when(this.containsAll(e), t, r);
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Execute the given callback if the string is empty.
|
|
984
|
+
*
|
|
985
|
+
* @param callback - The callback to execute if the string is empty
|
|
986
|
+
* @param defaultCallback - The callback to execute if the string is not empty
|
|
987
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
988
|
+
*/
|
|
989
|
+
whenEmpty(e, t = null) {
|
|
990
|
+
return this.when(this.isEmpty(), e, t);
|
|
991
|
+
}
|
|
992
|
+
/**
|
|
993
|
+
* Execute the given callback if the string is not empty.
|
|
994
|
+
*
|
|
995
|
+
* @param callback - The callback to execute if the string is not empty
|
|
996
|
+
* @param defaultCallback - The callback to execute if the string is empty
|
|
997
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
998
|
+
*/
|
|
999
|
+
whenNotEmpty(e, t = null) {
|
|
1000
|
+
return this.when(this.isNotEmpty(), e, t);
|
|
1001
|
+
}
|
|
1002
|
+
/**
|
|
1003
|
+
* Execute the given callback if the string ends with a given substring.
|
|
1004
|
+
*
|
|
1005
|
+
* @param needles - The substring(s) to search for
|
|
1006
|
+
* @param callback - The callback to execute if the substring(s) are found
|
|
1007
|
+
* @param defaultCallback - The callback to execute if the substring(s) are not found
|
|
1008
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
1009
|
+
*/
|
|
1010
|
+
whenEndsWith(e, t, r = null) {
|
|
1011
|
+
return this.when(this.endsWith(e), t, r);
|
|
1012
|
+
}
|
|
1013
|
+
/**
|
|
1014
|
+
* Execute the given callback if the string doesn't end with a given substring.
|
|
1015
|
+
*
|
|
1016
|
+
* @param needles - The substring(s) to search for
|
|
1017
|
+
* @param callback - The callback to execute if the substring(s) are not found
|
|
1018
|
+
* @param defaultCallback - The callback to execute if the substring(s) are found
|
|
1019
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
1020
|
+
*/
|
|
1021
|
+
whenDoesntEndWith(e, t, r = null) {
|
|
1022
|
+
return this.when(
|
|
1023
|
+
this.doesntEndWith(e),
|
|
1024
|
+
t,
|
|
1025
|
+
r
|
|
1026
|
+
);
|
|
1027
|
+
}
|
|
1028
|
+
/**
|
|
1029
|
+
* Execute the given callback if the string is an exact match with the given value.
|
|
1030
|
+
*
|
|
1031
|
+
* @param value - The value to compare against
|
|
1032
|
+
* @param callback - The callback to execute if the string matches the value
|
|
1033
|
+
* @param defaultCallback - The callback to execute if the string does not match the value
|
|
1034
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
1035
|
+
*/
|
|
1036
|
+
whenExactly(e, t, r = null) {
|
|
1037
|
+
return this.when(this.exactly(e), t, r);
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* Execute the given callback if the string is not an exact match with the given value.
|
|
1041
|
+
*
|
|
1042
|
+
* @param value - The value to compare against
|
|
1043
|
+
* @param callback - The callback to execute if the string does not match the value
|
|
1044
|
+
* @param defaultCallback - The callback to execute if the string matches the value
|
|
1045
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
1046
|
+
*/
|
|
1047
|
+
whenNotExactly(e, t, r = null) {
|
|
1048
|
+
return this.when(!this.exactly(e), t, r);
|
|
1049
|
+
}
|
|
1050
|
+
/**
|
|
1051
|
+
* Execute the given callback if the string matches a given pattern.
|
|
1052
|
+
*
|
|
1053
|
+
* @param pattern - The pattern(s) to match against
|
|
1054
|
+
* @param callback - The callback to execute if the pattern(s) match
|
|
1055
|
+
* @param defaultCallback - The callback to execute if the pattern(s) do not match
|
|
1056
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
1057
|
+
*/
|
|
1058
|
+
whenIs(e, t, r = null) {
|
|
1059
|
+
return this.when(this.is(e), t, r);
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
* Execute the given callback if the string is 7 bit ASCII.
|
|
1063
|
+
*
|
|
1064
|
+
* @param callback - The callback to execute if the string is ASCII
|
|
1065
|
+
* @param defaultCallback - The callback to execute if the string is not ASCII
|
|
1066
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
1067
|
+
*/
|
|
1068
|
+
whenIsAscii(e, t = null) {
|
|
1069
|
+
return this.when(this.isAscii(), e, t);
|
|
1070
|
+
}
|
|
1071
|
+
/**
|
|
1072
|
+
* Execute the given callback if the string is a valid UUID.
|
|
1073
|
+
*
|
|
1074
|
+
* @param callback - The callback to execute if the string is a UUID
|
|
1075
|
+
* @param defaultCallback - The callback to execute if the string is not a UUID
|
|
1076
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
1077
|
+
*/
|
|
1078
|
+
whenIsUuid(e, t = null) {
|
|
1079
|
+
return this.when(this.isUuid(), e, t);
|
|
1080
|
+
}
|
|
1081
|
+
/**
|
|
1082
|
+
* Execute the given callback if the string is a valid ULID.
|
|
1083
|
+
*
|
|
1084
|
+
* @param callback - The callback to execute if the string is a ULID
|
|
1085
|
+
* @param defaultCallback - The callback to execute if the string is not a ULID
|
|
1086
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
1087
|
+
*/
|
|
1088
|
+
whenIsUlid(e, t = null) {
|
|
1089
|
+
return this.when(this.isUlid(), e, t);
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
* Execute the given callback if the string starts with a given substring.
|
|
1093
|
+
*
|
|
1094
|
+
* @param needles - The substring(s) to search for
|
|
1095
|
+
* @param callback - The callback to execute if the substring(s) are found
|
|
1096
|
+
* @param defaultCallback - The callback to execute if the substring(s) are not found
|
|
1097
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
1098
|
+
*/
|
|
1099
|
+
whenStartsWith(e, t, r = null) {
|
|
1100
|
+
return this.when(this.startsWith(e), t, r);
|
|
1101
|
+
}
|
|
1102
|
+
/**
|
|
1103
|
+
* Execute the given callback if the string matches the given pattern.
|
|
1104
|
+
*
|
|
1105
|
+
* @param pattern - The pattern(s) to match against
|
|
1106
|
+
* @param callback - The callback to execute if the pattern(s) match
|
|
1107
|
+
* @param defaultCallback - The callback to execute if the pattern(s) do not match
|
|
1108
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
1109
|
+
*/
|
|
1110
|
+
whenTest(e, t, r = null) {
|
|
1111
|
+
return this.when(this.test(e), t, r);
|
|
1112
|
+
}
|
|
1113
|
+
/**
|
|
1114
|
+
* Limit the number of words in a string.
|
|
1115
|
+
*
|
|
1116
|
+
* @param wordsValue - The maximum number of words allowed. Defaults to 100.
|
|
1117
|
+
* @param end - The string to append if the string is truncated. Defaults to "...".
|
|
1118
|
+
* @returns The truncated string as a new Stringable instance.
|
|
1119
|
+
*/
|
|
1120
|
+
words(e = 100, t = "...") {
|
|
1121
|
+
return new s(Je(this._value, e, t));
|
|
1122
|
+
}
|
|
1123
|
+
/**
|
|
1124
|
+
* Get the number of words a string contains.
|
|
1125
|
+
*
|
|
1126
|
+
* @param characters - The characters to consider as word boundaries. If null, defaults to standard word boundaries.
|
|
1127
|
+
* @returns The number of words in the string.
|
|
1128
|
+
*/
|
|
1129
|
+
wordCount(e = null) {
|
|
1130
|
+
return Ve(this._value, e);
|
|
1131
|
+
}
|
|
1132
|
+
/**
|
|
1133
|
+
* Wrap a string to a given number of characters.
|
|
1134
|
+
*
|
|
1135
|
+
* @param characters - The number of characters at which to wrap the string. Defaults to 75.
|
|
1136
|
+
* @param breakStr - The string to insert as a break. Defaults to "\n".
|
|
1137
|
+
* @param cutLongWords - Whether to cut words longer than the specified length. Defaults to false.
|
|
1138
|
+
* @returns The wrapped string as a new Stringable instance.
|
|
1139
|
+
*/
|
|
1140
|
+
wordWrap(e = 75, t = `
|
|
1141
|
+
`, r = !1) {
|
|
1142
|
+
return new s(
|
|
1143
|
+
$e(this._value, e, t, r)
|
|
1144
|
+
);
|
|
1145
|
+
}
|
|
1146
|
+
/**
|
|
1147
|
+
* Wrap the string with the given strings.
|
|
1148
|
+
*
|
|
1149
|
+
* @param before - The string to prepend
|
|
1150
|
+
* @param after - The string to append. Defaults to the value of `before`
|
|
1151
|
+
* @returns The wrapped string as a new Stringable instance.
|
|
1152
|
+
*/
|
|
1153
|
+
wrap(e, t = null) {
|
|
1154
|
+
return new s(ze(this._value, e, t));
|
|
1155
|
+
}
|
|
1156
|
+
/**
|
|
1157
|
+
* Unwrap the string with the given strings.
|
|
1158
|
+
*
|
|
1159
|
+
* @param before - The string to remove from the start
|
|
1160
|
+
* @param after - The string to remove from the end. Defaults to the value of `before`
|
|
1161
|
+
* @returns The unwrapped string as a new Stringable instance.
|
|
1162
|
+
*/
|
|
1163
|
+
unwrap(e, t = null) {
|
|
1164
|
+
return new s(Ge(this._value, e, t));
|
|
1165
|
+
}
|
|
1166
|
+
/**
|
|
1167
|
+
* Convert the string to Base64 encoding.
|
|
1168
|
+
*
|
|
1169
|
+
* @returns The Base64 encoded string as a new Stringable instance.
|
|
1170
|
+
*/
|
|
1171
|
+
toBase64() {
|
|
1172
|
+
return new s(Xe(this._value));
|
|
1173
|
+
}
|
|
1174
|
+
/**
|
|
1175
|
+
* Decode the Base64 encoded string.
|
|
1176
|
+
*
|
|
1177
|
+
* @param strict - Whether to use strict decoding. Defaults to false.
|
|
1178
|
+
* @returns The decoded string as a new Stringable instance, or false on failure.
|
|
1179
|
+
*/
|
|
1180
|
+
fromBase64(e = !1) {
|
|
1181
|
+
const t = He(this._value, e);
|
|
1182
|
+
return t === !1 ? !1 : new s(t);
|
|
1183
|
+
}
|
|
1184
|
+
/**
|
|
1185
|
+
* Get the underlying string value.
|
|
1186
|
+
*
|
|
1187
|
+
* @returns The string representation of the object.
|
|
1188
|
+
*/
|
|
1189
|
+
toString() {
|
|
1190
|
+
return this.value();
|
|
1191
|
+
}
|
|
1192
|
+
/**
|
|
1193
|
+
* Get the underlying string value.
|
|
1194
|
+
*
|
|
1195
|
+
* @returns The string value.
|
|
1196
|
+
*/
|
|
1197
|
+
value() {
|
|
1198
|
+
return String(this._value);
|
|
1199
|
+
}
|
|
1200
|
+
/**
|
|
1201
|
+
* Get the underlying string value as an integer.
|
|
1202
|
+
*
|
|
1203
|
+
* @param base - The base to use for conversion. Defaults to 10.
|
|
1204
|
+
* @returns The integer value.
|
|
1205
|
+
*/
|
|
1206
|
+
toInteger(e = 10) {
|
|
1207
|
+
const t = Number(e) || 10;
|
|
1208
|
+
return parseInt(this._value, t);
|
|
1209
|
+
}
|
|
1210
|
+
/**
|
|
1211
|
+
* Get the underlying string value as a float.
|
|
1212
|
+
*
|
|
1213
|
+
* @returns The float value.
|
|
1214
|
+
*/
|
|
1215
|
+
toFloat() {
|
|
1216
|
+
return parseFloat(this._value);
|
|
1217
|
+
}
|
|
1218
|
+
/**
|
|
1219
|
+
* Get the underlying string value as a boolean.
|
|
1220
|
+
*
|
|
1221
|
+
* @returns true when value is "1", "true", "on", and "yes". Otherwise, returns false.
|
|
1222
|
+
*/
|
|
1223
|
+
toBoolean() {
|
|
1224
|
+
const e = w(o(this._value));
|
|
1225
|
+
return e === "1" || e === "true" || e === "on" || e === "yes";
|
|
1226
|
+
}
|
|
1227
|
+
/**
|
|
1228
|
+
* Get the underlying string value as a Carbon instance.
|
|
1229
|
+
*
|
|
1230
|
+
* @returns The Date instance or null if parsing fails.
|
|
1231
|
+
*/
|
|
1232
|
+
toDate() {
|
|
1233
|
+
const e = Date.parse(this._value);
|
|
1234
|
+
return isNaN(e) ? null : new Date(e);
|
|
1235
|
+
}
|
|
1236
|
+
/**
|
|
1237
|
+
* Convert the object to a string when JSON encoded.
|
|
1238
|
+
*
|
|
1239
|
+
* @returns The string representation of the object.
|
|
1240
|
+
*/
|
|
1241
|
+
jsonSerialize() {
|
|
1242
|
+
return this.toString();
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
export {
|
|
1246
|
+
s as Stringable,
|
|
1247
|
+
Ye as of,
|
|
1248
|
+
Ze as str
|
|
1249
|
+
};
|