@zinaid/str 0.0.1
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 +0 -0
- package/dist/base64.d.ts +7 -0
- package/dist/base64.d.ts.map +1 -0
- package/dist/base64.js +123 -0
- package/dist/convertcase.d.ts +38 -0
- package/dist/convertcase.d.ts.map +1 -0
- package/dist/convertcase.js +49 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +133 -0
- package/dist/markdown.d.ts +26 -0
- package/dist/markdown.d.ts.map +1 -0
- package/dist/markdown.js +32 -0
- package/dist/pluralizer.d.ts +39 -0
- package/dist/pluralizer.d.ts.map +1 -0
- package/dist/pluralizer.js +90 -0
- package/dist/random.d.ts +28 -0
- package/dist/random.d.ts.map +1 -0
- package/dist/random.js +44 -0
- package/dist/replacer.d.ts +31 -0
- package/dist/replacer.d.ts.map +1 -0
- package/dist/replacer.js +42 -0
- package/dist/str.d.ts +846 -0
- package/dist/str.d.ts.map +1 -0
- package/dist/str.js +1096 -0
- package/dist/stringable.d.ts +555 -0
- package/dist/stringable.d.ts.map +1 -0
- package/dist/stringable.js +862 -0
- package/dist/trimmer.d.ts +4 -0
- package/dist/trimmer.d.ts.map +1 -0
- package/dist/trimmer.js +128 -0
- package/package.json +43 -0
|
@@ -0,0 +1,862 @@
|
|
|
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 a, 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 w, 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 "@zinaid/str";
|
|
2
|
+
import { isInteger as Ke, isArray as o, isFunction as v } from "@zinaid/utils";
|
|
3
|
+
function Ye(c) {
|
|
4
|
+
return new s(c);
|
|
5
|
+
}
|
|
6
|
+
class s {
|
|
7
|
+
/**
|
|
8
|
+
* Create a new instance of the class.
|
|
9
|
+
*/
|
|
10
|
+
constructor(e = "") {
|
|
11
|
+
this._value = e;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Return the remainder of a string after the first occurrence of a given value.
|
|
15
|
+
*/
|
|
16
|
+
after(e) {
|
|
17
|
+
return new s(p(this._value, e));
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Return the remainder of a string after the last occurrence of a given value
|
|
21
|
+
*/
|
|
22
|
+
afterLast(e) {
|
|
23
|
+
return new s(d(this._value, e));
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Append the given values to the string.
|
|
27
|
+
*/
|
|
28
|
+
append(...e) {
|
|
29
|
+
return new s(this._value + e.map(String).join(""));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Append a new line to the string.
|
|
33
|
+
*/
|
|
34
|
+
newLine(e = 1) {
|
|
35
|
+
return this.append(`
|
|
36
|
+
`.repeat(Math.max(0, e)));
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Transliterate a UTF-8 value to ASCII.
|
|
40
|
+
*/
|
|
41
|
+
ascii() {
|
|
42
|
+
return new s(f(this._value));
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get the portion of a string before the first occurrence of a given value.
|
|
46
|
+
*/
|
|
47
|
+
before(e) {
|
|
48
|
+
return new s(m(this._value, e));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Get the portion of a string before the last occurrence of a given value.
|
|
52
|
+
*/
|
|
53
|
+
beforeLast(e) {
|
|
54
|
+
return new s(_(this._value, e));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Get the portion of a string between two given values.
|
|
58
|
+
*/
|
|
59
|
+
between(e, t) {
|
|
60
|
+
return new s(E(this._value, e, t));
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get the smallest possible portion of a string between two given values.
|
|
64
|
+
*/
|
|
65
|
+
betweenFirst(e, t) {
|
|
66
|
+
return new s(y(this._value, e, t));
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Convert a value to camel case.
|
|
70
|
+
*/
|
|
71
|
+
camel() {
|
|
72
|
+
return new s(A(this._value));
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Get the character at the specified index.
|
|
76
|
+
*/
|
|
77
|
+
charAt(e) {
|
|
78
|
+
return W(this._value, e);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Remove the given string if it exists at the start of the current string.
|
|
82
|
+
*/
|
|
83
|
+
chopStart(e) {
|
|
84
|
+
return new s(x(this._value, e));
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Remove the given string if it exists at the end of the current string.
|
|
88
|
+
*/
|
|
89
|
+
chopEnd(e) {
|
|
90
|
+
return new s(N(this._value, e));
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Determine if a given string contains a given substring.
|
|
94
|
+
*/
|
|
95
|
+
contains(e, t = !1) {
|
|
96
|
+
return k(this._value, e, t);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Determine if a given string contains all array values.
|
|
100
|
+
*/
|
|
101
|
+
containsAll(e, t = !1) {
|
|
102
|
+
return U(this._value, e, t);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Determine if a given string doesn't contain a given substring.
|
|
106
|
+
*
|
|
107
|
+
* @param needles - The substring(s) to search for
|
|
108
|
+
* @param ignoreCase - Whether the search should be case-insensitive
|
|
109
|
+
* @returns boolean - True if the substring(s) are not found, false otherwise
|
|
110
|
+
*/
|
|
111
|
+
doesntContain(e, t = !1) {
|
|
112
|
+
return C(this._value, e, t);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Convert the case of a string.
|
|
116
|
+
*/
|
|
117
|
+
convertCase(e) {
|
|
118
|
+
return new s(L(this._value, e));
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Replace consecutive instances of a given character with a single character.
|
|
122
|
+
*/
|
|
123
|
+
deduplicate(e = " ") {
|
|
124
|
+
return new s(M(this._value, e));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Determine if a given string ends with a given substring.
|
|
128
|
+
*/
|
|
129
|
+
endsWith(e) {
|
|
130
|
+
return F(this._value, e);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Determine if a given string doesn't end with a given substring.
|
|
134
|
+
*/
|
|
135
|
+
doesntEndWith(e) {
|
|
136
|
+
return I(this._value, e);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Determine if the string is an exact match with the given value.
|
|
140
|
+
*/
|
|
141
|
+
exactly(e) {
|
|
142
|
+
const t = e instanceof s ? e.toString() : String(e);
|
|
143
|
+
return this._value === t;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Extracts an excerpt from text that matches the first instance of a phrase.
|
|
147
|
+
*/
|
|
148
|
+
excerpt(e = "", t = {}) {
|
|
149
|
+
return B(this._value, e, t);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Explode the string into an array
|
|
153
|
+
*/
|
|
154
|
+
explode(e, t = Number.MAX_SAFE_INTEGER) {
|
|
155
|
+
const r = this._value.split(e);
|
|
156
|
+
if (t > 0 && r.length > t) {
|
|
157
|
+
const n = r.slice(0, t - 1);
|
|
158
|
+
return n.push(r.slice(t - 1).join(e)), n;
|
|
159
|
+
} else if (t < 0)
|
|
160
|
+
return r.slice(0, t);
|
|
161
|
+
return r;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Split a string using a regular expression or by length.
|
|
165
|
+
*/
|
|
166
|
+
split(e, t = null) {
|
|
167
|
+
if (Ke(e)) {
|
|
168
|
+
const n = Number(e), h = [];
|
|
169
|
+
for (let i = 0; i < this._value.length; i += n)
|
|
170
|
+
h.push(this._value.substr(i, n));
|
|
171
|
+
return h;
|
|
172
|
+
}
|
|
173
|
+
return this._value.split(
|
|
174
|
+
new RegExp(String(e)),
|
|
175
|
+
t ?? void 0
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Cap a string with a single instance of a given value.
|
|
180
|
+
*/
|
|
181
|
+
finish(e) {
|
|
182
|
+
return new s(R(this._value, e));
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Determine if a given string matches a given pattern.
|
|
186
|
+
*/
|
|
187
|
+
is(e, t = !1) {
|
|
188
|
+
return j(e, this._value, t);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Determine if a given string is 7 bit ASCII.
|
|
192
|
+
*/
|
|
193
|
+
isAscii() {
|
|
194
|
+
return D(this._value);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Determine if a given string is valid JSON.
|
|
198
|
+
*/
|
|
199
|
+
isJson() {
|
|
200
|
+
return T(this._value);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Determine if a given value is a valid URL.
|
|
204
|
+
*/
|
|
205
|
+
isUrl(e = []) {
|
|
206
|
+
return P(this._value, e);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Determine if a given string is a valid UUID.
|
|
210
|
+
*/
|
|
211
|
+
isUuid(e = null) {
|
|
212
|
+
return q(this._value, e);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Determine if a given string is a valid ULID.
|
|
216
|
+
*/
|
|
217
|
+
isUlid() {
|
|
218
|
+
return J(this._value);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Determine if the given string is empty.
|
|
222
|
+
*/
|
|
223
|
+
isEmpty() {
|
|
224
|
+
return this._value === "";
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Determine if the given string is not empty.
|
|
228
|
+
*/
|
|
229
|
+
isNotEmpty() {
|
|
230
|
+
return !this.isEmpty();
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Convert a string to kebab case.
|
|
234
|
+
*/
|
|
235
|
+
kebab() {
|
|
236
|
+
return new s(V(this._value));
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Return the length of the given string.
|
|
240
|
+
*/
|
|
241
|
+
length() {
|
|
242
|
+
return $(this._value);
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Limit the number of characters in a string.
|
|
246
|
+
*/
|
|
247
|
+
limit(e = 100, t = "...", r = !1) {
|
|
248
|
+
return new s(
|
|
249
|
+
z(this._value, e, t, r)
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Convert the given string to lower-case.
|
|
254
|
+
*/
|
|
255
|
+
lower() {
|
|
256
|
+
return new s(a(this._value));
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Convert GitHub flavored Markdown into HTML.
|
|
260
|
+
*/
|
|
261
|
+
markdown(e = { gfm: !0, anchors: !1 }, t = []) {
|
|
262
|
+
return new s(G(this._value, e, t));
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Convert inline Markdown into HTML.
|
|
266
|
+
*/
|
|
267
|
+
inlineMarkdown(e = { gfm: !0 }, t = []) {
|
|
268
|
+
return new s(X(this._value, e, t));
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Masks a portion of a string with a repeated character.
|
|
272
|
+
*/
|
|
273
|
+
mask(e, t, r = null) {
|
|
274
|
+
return new s(H(this._value, e, t, r));
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Get the string matching the given pattern.
|
|
278
|
+
*/
|
|
279
|
+
match(e) {
|
|
280
|
+
return new s(K(e, this._value));
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Determine if a given string matches a given pattern.
|
|
284
|
+
*/
|
|
285
|
+
isMatch(e) {
|
|
286
|
+
return O(e, this._value);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Get the string matching the given pattern.
|
|
290
|
+
*/
|
|
291
|
+
matchAll(e) {
|
|
292
|
+
return Q(e, this._value);
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Determine if the string matches the given pattern.
|
|
296
|
+
*/
|
|
297
|
+
test(e) {
|
|
298
|
+
return this.isMatch(e);
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Remove all non-numeric characters from a string.
|
|
302
|
+
*/
|
|
303
|
+
numbers() {
|
|
304
|
+
return new s(Y(this._value));
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Pad both sides of the string with another.
|
|
308
|
+
*/
|
|
309
|
+
padBoth(e, t = " ") {
|
|
310
|
+
return new s(Z(this._value, e, t));
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Pad the left side of the string with another.
|
|
314
|
+
*/
|
|
315
|
+
padLeft(e, t = " ") {
|
|
316
|
+
return new s(g(this._value, e, t));
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Pad the right side of the string with another.
|
|
320
|
+
*/
|
|
321
|
+
padRight(e, t = " ") {
|
|
322
|
+
return new s(b(this._value, e, t));
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Call the given callback and return a new string.
|
|
326
|
+
*/
|
|
327
|
+
pipe(e) {
|
|
328
|
+
const t = e(this);
|
|
329
|
+
return t instanceof s ? t : new s(String(t));
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Get the plural form of an English word.
|
|
333
|
+
*/
|
|
334
|
+
plural(e = 2, t = !1) {
|
|
335
|
+
return new s(S(this._value, e, t));
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Pluralize the last word of an English, studly caps case string.
|
|
339
|
+
*/
|
|
340
|
+
pluralStudly(e = 2) {
|
|
341
|
+
const t = o(e) ? e.length : Number(e);
|
|
342
|
+
return new s(ee(this._value, t));
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Pluralize the last word of an English, Pascal caps case string.
|
|
346
|
+
*/
|
|
347
|
+
pluralPascal(e = 2) {
|
|
348
|
+
const t = o(e) ? e.length : Number(e);
|
|
349
|
+
return new s(te(this._value, t));
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Find the multi-byte safe position of the first occurrence of the given substring.
|
|
353
|
+
*/
|
|
354
|
+
position(e, t = 0) {
|
|
355
|
+
return se(this._value, e, t);
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Prepend the given values to the string.
|
|
359
|
+
*/
|
|
360
|
+
prepend(...e) {
|
|
361
|
+
return new s(e.map(String).join("") + this._value);
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Remove any occurrence of the given string in the subject.
|
|
365
|
+
*/
|
|
366
|
+
remove(e, t = !0) {
|
|
367
|
+
return new s(
|
|
368
|
+
re(e, this._value, t)
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Reverse the string.
|
|
373
|
+
*/
|
|
374
|
+
reverse() {
|
|
375
|
+
return new s(ne(this._value));
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Repeat the string.
|
|
379
|
+
*/
|
|
380
|
+
repeat(e) {
|
|
381
|
+
return new s(ue(this._value, e));
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Replace the given value in the given string.
|
|
385
|
+
*/
|
|
386
|
+
replace(e, t, r = !0) {
|
|
387
|
+
return new s(
|
|
388
|
+
he(e, t, this._value, r)
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Replace a given value in the string sequentially with an array.
|
|
393
|
+
*/
|
|
394
|
+
replaceArray(e, t) {
|
|
395
|
+
return new s(ie(e, t, this._value));
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Replace the first occurrence of a given value in the string.
|
|
399
|
+
*/
|
|
400
|
+
replaceFirst(e, t) {
|
|
401
|
+
return new s(le(e, t, this._value));
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Replace the first occurrence of the given value if it appears at the start of the string.
|
|
405
|
+
*/
|
|
406
|
+
replaceStart(e, t) {
|
|
407
|
+
return new s(ae(e, t, this._value));
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Replace the last occurrence of a given value in the string.
|
|
411
|
+
*/
|
|
412
|
+
replaceLast(e, t) {
|
|
413
|
+
return new s(we(e, t, this._value));
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Replace the last occurrence of a given value if it appears at the end of the string.
|
|
417
|
+
*/
|
|
418
|
+
replaceEnd(e, t) {
|
|
419
|
+
return new s(oe(e, t, this._value));
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Replace the patterns matching the given regular expression.
|
|
423
|
+
*/
|
|
424
|
+
replaceMatches(e, t, r = -1) {
|
|
425
|
+
const n = ve(e, t, this._value, r);
|
|
426
|
+
return new s(n);
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Parse input from a string to an array, according to a format.
|
|
430
|
+
*
|
|
431
|
+
* @param format - A format string like "%d", "%s", "%[^,],%s", etc.
|
|
432
|
+
* @returns Array of parsed values according to the format
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
*
|
|
436
|
+
* Str.of("SN/123456").scan("SN/%d") -> ["123456"]
|
|
437
|
+
* Str.of("Otwell, Taylor").scan("%[^,],%s") -> ["Otwell", "Taylor"]
|
|
438
|
+
* Str.of("filename.jpg").scan("%[^.].%s") -> ["filename", "jpg"]
|
|
439
|
+
*/
|
|
440
|
+
scan(e) {
|
|
441
|
+
let t = "", r = 0;
|
|
442
|
+
for (; r < e.length; )
|
|
443
|
+
if (e[r] === "%")
|
|
444
|
+
if (e[r + 1] === "[") {
|
|
445
|
+
let u = r + 2;
|
|
446
|
+
for (; u < e.length && e[u] !== "]"; )
|
|
447
|
+
u++;
|
|
448
|
+
const l = e.substring(r + 2, u);
|
|
449
|
+
t += "([" + l + "]+)", r = u + 1;
|
|
450
|
+
} else e[r + 1] === "d" ? (t += "\\s*(-?\\d+)", r += 2) : e[r + 1] === "s" ? (t += "\\s*(\\S+)", r += 2) : (t += e[r], r++);
|
|
451
|
+
else {
|
|
452
|
+
const u = e[r];
|
|
453
|
+
u && /[.+?^${}()|\\]/.test(u) ? t += "\\" + u : t += u, r++;
|
|
454
|
+
}
|
|
455
|
+
const n = new RegExp("^" + t + "$"), h = this._value.match(n);
|
|
456
|
+
if (!h)
|
|
457
|
+
return [];
|
|
458
|
+
const i = [];
|
|
459
|
+
for (let u = 1; u < h.length; u++) {
|
|
460
|
+
const l = h[u];
|
|
461
|
+
l && i.push(l);
|
|
462
|
+
}
|
|
463
|
+
return i;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Remove all "extra" blank space from the given string.
|
|
467
|
+
*/
|
|
468
|
+
squish() {
|
|
469
|
+
return new s(ce(this._value));
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Begin a string with a single instance of a given value.
|
|
473
|
+
*/
|
|
474
|
+
start(e) {
|
|
475
|
+
return new s(pe(this._value, e));
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Strip HTML and PHP tags from the given string.
|
|
479
|
+
*/
|
|
480
|
+
stripTags() {
|
|
481
|
+
return new s(de(this._value));
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Convert the given string to upper-case.
|
|
485
|
+
*/
|
|
486
|
+
upper() {
|
|
487
|
+
return new s(fe(this._value));
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Convert the given string to proper case.
|
|
491
|
+
*/
|
|
492
|
+
title() {
|
|
493
|
+
return new s(me(this._value));
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Convert the given string to proper case for each word.
|
|
497
|
+
*/
|
|
498
|
+
headline() {
|
|
499
|
+
return new s(_e(this._value));
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Convert the given string to APA-style title case.
|
|
503
|
+
*/
|
|
504
|
+
apa() {
|
|
505
|
+
return new s(Ee(this._value));
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Transliterate a string to its closest ASCII representation.
|
|
509
|
+
*/
|
|
510
|
+
transliterate() {
|
|
511
|
+
return new s(ye(this._value));
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Get the singular form of an English word.
|
|
515
|
+
*/
|
|
516
|
+
singular() {
|
|
517
|
+
return new s(Ae(this._value));
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Generate a URL friendly "slug" from a given string.
|
|
521
|
+
*/
|
|
522
|
+
slug(e = "-", t = { "@": "at" }) {
|
|
523
|
+
return new s(We(this._value, e, t));
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Convert a string to snake case.
|
|
527
|
+
*/
|
|
528
|
+
snake(e = "_") {
|
|
529
|
+
return new s(xe(this._value, e));
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Determine if a given string starts with a given substring.
|
|
533
|
+
*/
|
|
534
|
+
startsWith(e) {
|
|
535
|
+
return Ne(this._value, e);
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Determine if a given string doesn't start with a given substring.
|
|
539
|
+
*/
|
|
540
|
+
doesntStartWith(e) {
|
|
541
|
+
return ke(this._value, e);
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Convert a value to studly caps case.
|
|
545
|
+
*/
|
|
546
|
+
studly() {
|
|
547
|
+
return new s(Ue(this._value));
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Convert the string to Pascal case.
|
|
551
|
+
*/
|
|
552
|
+
pascal() {
|
|
553
|
+
return new s(Ce(this._value));
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Returns the portion of the string specified by the start and length parameters.
|
|
557
|
+
*/
|
|
558
|
+
substr(e, t = null) {
|
|
559
|
+
return new s(Le(this._value, e, t));
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Returns the number of substring occurrences.
|
|
563
|
+
*/
|
|
564
|
+
substrCount(e, t = 0, r = null) {
|
|
565
|
+
return Me(this._value, e, t, r);
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Replace text within a portion of a string.
|
|
569
|
+
*/
|
|
570
|
+
substrReplace(e, t = 0, r = null) {
|
|
571
|
+
return new s(
|
|
572
|
+
Fe(this._value, e, t, r)
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Swap multiple keywords in a string with other keywords.
|
|
577
|
+
*/
|
|
578
|
+
swap(e) {
|
|
579
|
+
return new s(Ie(e, this._value));
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* Take the first or last {$limit} characters.
|
|
583
|
+
*/
|
|
584
|
+
take(e) {
|
|
585
|
+
return new s(Be(this._value, e));
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Trim the string of the given characters.
|
|
589
|
+
*/
|
|
590
|
+
trim(e = null) {
|
|
591
|
+
return new s(w(this._value, e));
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Left trim the string of the given characters.
|
|
595
|
+
*/
|
|
596
|
+
ltrim(e = null) {
|
|
597
|
+
return new s(Re(this._value, e));
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
* Right trim the string of the given characters.
|
|
601
|
+
*/
|
|
602
|
+
rtrim(e = null) {
|
|
603
|
+
return new s(je(this._value, e));
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Make a string's first character lowercase.
|
|
607
|
+
*/
|
|
608
|
+
lcfirst() {
|
|
609
|
+
return new s(De(this._value));
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Make a string's first character uppercase.
|
|
613
|
+
*/
|
|
614
|
+
ucfirst() {
|
|
615
|
+
return new s(Te(this._value));
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Split a string by uppercase characters.
|
|
619
|
+
*/
|
|
620
|
+
ucsplit() {
|
|
621
|
+
return Pe(this._value);
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Uppercase the first character of each word in a string.
|
|
625
|
+
*/
|
|
626
|
+
ucwords() {
|
|
627
|
+
return new s(qe(this._value));
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Apply the callback if the given "value" is (or resolves to) truthy.
|
|
631
|
+
*
|
|
632
|
+
* @param value - The value to evaluate or a closure that returns the value
|
|
633
|
+
* @param callback - The callback to execute if the value is truthy
|
|
634
|
+
* @param defaultCallback - The callback to execute if the value is falsy
|
|
635
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
636
|
+
*
|
|
637
|
+
* @example
|
|
638
|
+
*
|
|
639
|
+
* ```typescript
|
|
640
|
+
* const str = new Stringable('hello world');
|
|
641
|
+
*
|
|
642
|
+
* Using a direct value
|
|
643
|
+
* str.when(true, s => s.upper()); // Returns 'HELLO WORLD'
|
|
644
|
+
* str.when(false, s => s.upper(), s => s.lower()); // Returns 'hello world'
|
|
645
|
+
*
|
|
646
|
+
* Using a closure to determine the value
|
|
647
|
+
* str.when(s => s.contains('world'), s => s.upper()); // Returns 'HELLO WORLD'
|
|
648
|
+
* str.when(s => s.contains('foo'), s => s.upper(), s => s.lower()); // Returns 'hello world'
|
|
649
|
+
* ```
|
|
650
|
+
*/
|
|
651
|
+
when(e, t = null, r = null) {
|
|
652
|
+
const n = v(e) ? e(this) : e;
|
|
653
|
+
return n ? t?.(this, n) ?? this : r ? r(this, n) ?? this : this;
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Apply the callback if the given "value" is (or resolves to) falsy.
|
|
657
|
+
*
|
|
658
|
+
* @param value - The value to evaluate or a closure that returns the value
|
|
659
|
+
* @param callback - The callback to execute if the value is falsy
|
|
660
|
+
* @param defaultCallback - The callback to execute if the value is truthy
|
|
661
|
+
* @returns Stringable - The current instance or the result of the callback
|
|
662
|
+
*
|
|
663
|
+
* @example
|
|
664
|
+
*
|
|
665
|
+
* const str = new Stringable('hello world');
|
|
666
|
+
* str.unless(true, s => s.upper()); // Returns 'hello world'
|
|
667
|
+
* str.unless(false, s => s.upper(), s => s.lower()); // Returns 'HELLO WORLD'
|
|
668
|
+
*/
|
|
669
|
+
unless(e, t = null, r = null) {
|
|
670
|
+
const n = v(e) ? e(this) : e;
|
|
671
|
+
if (n) {
|
|
672
|
+
if (r)
|
|
673
|
+
return r(this, n) ?? this;
|
|
674
|
+
} else return t?.(this, n) ?? this;
|
|
675
|
+
return this;
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* Execute the given callback if the string contains a given substring.
|
|
679
|
+
*/
|
|
680
|
+
whenContains(e, t, r = null) {
|
|
681
|
+
return this.when(this.contains(e), t, r);
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* Execute the given callback if the string contains all array values.
|
|
685
|
+
*/
|
|
686
|
+
whenContainsAll(e, t, r = null) {
|
|
687
|
+
return this.when(this.containsAll(e), t, r);
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* Execute the given callback if the string is empty.
|
|
691
|
+
*/
|
|
692
|
+
whenEmpty(e, t = null) {
|
|
693
|
+
return this.when(this.isEmpty(), e, t);
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Execute the given callback if the string is not empty.
|
|
697
|
+
*/
|
|
698
|
+
whenNotEmpty(e, t = null) {
|
|
699
|
+
return this.when(this.isNotEmpty(), e, t);
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* Execute the given callback if the string ends with a given substring.
|
|
703
|
+
*/
|
|
704
|
+
whenEndsWith(e, t, r = null) {
|
|
705
|
+
return this.when(this.endsWith(e), t, r);
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* Execute the given callback if the string doesn't end with a given substring.
|
|
709
|
+
*/
|
|
710
|
+
whenDoesntEndWith(e, t, r = null) {
|
|
711
|
+
return this.when(
|
|
712
|
+
this.doesntEndWith(e),
|
|
713
|
+
t,
|
|
714
|
+
r
|
|
715
|
+
);
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* Execute the given callback if the string is an exact match with the given value.
|
|
719
|
+
*/
|
|
720
|
+
whenExactly(e, t, r = null) {
|
|
721
|
+
return this.when(this.exactly(e), t, r);
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Execute the given callback if the string is not an exact match with the given value.
|
|
725
|
+
*/
|
|
726
|
+
whenNotExactly(e, t, r = null) {
|
|
727
|
+
return this.when(!this.exactly(e), t, r);
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* Execute the given callback if the string matches a given pattern.
|
|
731
|
+
*/
|
|
732
|
+
whenIs(e, t, r = null) {
|
|
733
|
+
return this.when(this.is(e), t, r);
|
|
734
|
+
}
|
|
735
|
+
/**
|
|
736
|
+
* Execute the given callback if the string is 7 bit ASCII.
|
|
737
|
+
*/
|
|
738
|
+
whenIsAscii(e, t = null) {
|
|
739
|
+
return this.when(this.isAscii(), e, t);
|
|
740
|
+
}
|
|
741
|
+
/**
|
|
742
|
+
* Execute the given callback if the string is a valid UUID.
|
|
743
|
+
*/
|
|
744
|
+
whenIsUuid(e, t = null) {
|
|
745
|
+
return this.when(this.isUuid(), e, t);
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Execute the given callback if the string is a valid ULID.
|
|
749
|
+
*/
|
|
750
|
+
whenIsUlid(e, t = null) {
|
|
751
|
+
return this.when(this.isUlid(), e, t);
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Execute the given callback if the string starts with a given substring.
|
|
755
|
+
*/
|
|
756
|
+
whenStartsWith(e, t, r = null) {
|
|
757
|
+
return this.when(this.startsWith(e), t, r);
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Execute the given callback if the string matches the given pattern.
|
|
761
|
+
*/
|
|
762
|
+
whenTest(e, t, r = null) {
|
|
763
|
+
return this.when(this.test(e), t, r);
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* Limit the number of words in a string.
|
|
767
|
+
*/
|
|
768
|
+
words(e = 100, t = "...") {
|
|
769
|
+
return new s(Je(this._value, e, t));
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* Get the number of words a string contains.
|
|
773
|
+
*/
|
|
774
|
+
wordCount(e = null) {
|
|
775
|
+
return Ve(this._value, e);
|
|
776
|
+
}
|
|
777
|
+
/**
|
|
778
|
+
* Wrap a string to a given number of characters.
|
|
779
|
+
*/
|
|
780
|
+
wordWrap(e = 75, t = `
|
|
781
|
+
`, r = !1) {
|
|
782
|
+
return new s(
|
|
783
|
+
$e(this._value, e, t, r)
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
/**
|
|
787
|
+
* Wrap the string with the given strings.
|
|
788
|
+
*/
|
|
789
|
+
wrap(e, t = null) {
|
|
790
|
+
return new s(ze(this._value, e, t));
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* Unwrap the string with the given strings.
|
|
794
|
+
*/
|
|
795
|
+
unwrap(e, t = null) {
|
|
796
|
+
return new s(Ge(this._value, e, t));
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* Convert the string to Base64 encoding.
|
|
800
|
+
*/
|
|
801
|
+
toBase64() {
|
|
802
|
+
return new s(Xe(this._value));
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* Decode the Base64 encoded string.
|
|
806
|
+
*/
|
|
807
|
+
fromBase64(e = !1) {
|
|
808
|
+
const t = He(this._value, e);
|
|
809
|
+
return t === !1 ? !1 : new s(t);
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* Get the underlying string value.
|
|
813
|
+
*/
|
|
814
|
+
toString() {
|
|
815
|
+
return this.value();
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* Get the underlying string value.
|
|
819
|
+
*/
|
|
820
|
+
value() {
|
|
821
|
+
return String(this._value);
|
|
822
|
+
}
|
|
823
|
+
/**
|
|
824
|
+
* Get the underlying string value as an integer.
|
|
825
|
+
*/
|
|
826
|
+
toInteger(e = 10) {
|
|
827
|
+
const t = Number(e) || 10;
|
|
828
|
+
return parseInt(this._value, t);
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* Get the underlying string value as a float.
|
|
832
|
+
*/
|
|
833
|
+
toFloat() {
|
|
834
|
+
return parseFloat(this._value);
|
|
835
|
+
}
|
|
836
|
+
/**
|
|
837
|
+
* Get the underlying string value as a boolean.
|
|
838
|
+
*
|
|
839
|
+
* Returns true when value is "1", "true", "on", and "yes". Otherwise, returns false.
|
|
840
|
+
*/
|
|
841
|
+
toBoolean() {
|
|
842
|
+
const e = a(w(this._value));
|
|
843
|
+
return e === "1" || e === "true" || e === "on" || e === "yes";
|
|
844
|
+
}
|
|
845
|
+
/**
|
|
846
|
+
* Get the underlying string value as a Carbon instance.
|
|
847
|
+
*/
|
|
848
|
+
toDate() {
|
|
849
|
+
const e = Date.parse(this._value);
|
|
850
|
+
return isNaN(e) ? null : new Date(e);
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
* Convert the object to a string when JSON encoded.
|
|
854
|
+
*/
|
|
855
|
+
jsonSerialize() {
|
|
856
|
+
return this.toString();
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
export {
|
|
860
|
+
s as Stringable,
|
|
861
|
+
Ye as of
|
|
862
|
+
};
|