@tolki/str 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1825 -0
- package/dist/str.d.ts +2 -2
- package/dist/str.js +1 -1
- package/package.json +12 -4
package/README.md
CHANGED
|
@@ -5,3 +5,1828 @@ This package provides string utility functions similar to Laravel's Str class fa
|
|
|
5
5
|
## Documentation
|
|
6
6
|
|
|
7
7
|
The full documentation for the string utilities can be found at [https://tolki.abe.dev](https://tolki.abe.dev/strings/).
|
|
8
|
+
|
|
9
|
+
<!-- AUTO-GENERATED-DOCS:START -->
|
|
10
|
+
|
|
11
|
+
## String Utilities Installation
|
|
12
|
+
|
|
13
|
+
The [`@tolki/str`](https://www.npmjs.com/package/@tolki/str) package provides a variety of string manipulation utilities inspired by Laravel's Str class. You can install it via npm, yarn, or pnpm:
|
|
14
|
+
|
|
15
|
+
```bash [npm]
|
|
16
|
+
npm install @tolki/str
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```bash [yarn]
|
|
20
|
+
yarn add @tolki/str
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```bash [pnpm]
|
|
24
|
+
pnpm add @tolki/str
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Importing the Package
|
|
28
|
+
|
|
29
|
+
You can import individual functions for better tree-shaking or the entire package (not recommended).:
|
|
30
|
+
|
|
31
|
+
Importing individual functions:
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
// Import individual functions
|
|
35
|
+
import { camel, chopEnd } from "@tolki/str";
|
|
36
|
+
|
|
37
|
+
// Example usage
|
|
38
|
+
const camelCaseString = camel("hello world"); // 'helloWorld'
|
|
39
|
+
const choppedString = chopEnd("HelloWorld", 5); // 'Hello'
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Importing the entire package:
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
// Import the entire package (not recommended for tree-shaking)
|
|
46
|
+
import * as Str from "@tolki/str";
|
|
47
|
+
|
|
48
|
+
// Example usage
|
|
49
|
+
const camelCaseString = Str.camel("hello world"); // 'helloWorld'
|
|
50
|
+
const choppedString = Str.chopEnd("HelloWorld", 5); // 'Hello'
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The reason that importing the entire package is not recommended is that it will include all functions in your bundle, even the ones you don't use, which can lead to larger bundle sizes. Importing individual functions allows your end files to only include the code that is actually used, resulting in smaller and more efficient bundles.
|
|
54
|
+
|
|
55
|
+
However, if you are working on a backend project where bundle size is not a concern, importing the entire package can be more convenient.
|
|
56
|
+
|
|
57
|
+
## Tolki String Utilities List
|
|
58
|
+
|
|
59
|
+
### String Utilities List
|
|
60
|
+
|
|
61
|
+
These are the string utilities that can be used independently as single functions.
|
|
62
|
+
|
|
63
|
+
[after](#after) [afterLast](#afterlast) [apa](#apa) [ascii](#ascii) [before](#before) [beforeLast](#beforelast) [between](#between) [betweenFirst](#betweenfirst) [camel](#camel) [charAt](#charat) [chopEnd](#chopend) [chopStart](#chopstart) [contains](#contains) [containsAll](#containsall) [doesntContain](#doesntcontain) [deduplicate](#deduplicate) [doesntEndWith](#doesntendwith) [doesntStartWith](#doesntstartwith) [endsWith](#endswith) [excerpt](#excerpt) [finish](#finish) [fromBase64](#frombase64) [headline](#headline) [inlineMarkdown](#inlinemarkdown) [is](#is) [isAscii](#isascii) [isJson](#isjson) [isUrl](#isurl) [isUlid](#isulid) [isUuid](#isuuid) [kebab](#kebab) [lcfirst](#lcfirst) [length](#length) [limit](#limit) [lower](#lower) [markdown](#markdown) [mask](#mask) [match](#match) [matchAll](#matchall) [isMatch](#ismatch) [numbers](#numbers) [padBoth](#padboth) [padLeft](#padleft) [padRight](#padright) [pascal](#pascal) [pluralPascal](#pluralpascal) [password](#password) [plural](#plural) [pluralStudly](#pluralstudly) [position](#position) [random](#random) [remove](#remove) [repeat](#repeat) [replace](#replace) [replaceArray](#replacearray) [replaceFirst](#replacefirst) [replaceLast](#replacelast) [replaceMatches](#replacematches) [replaceStart](#replacestart) [replaceEnd](#replaceend) [reverse](#reverse) [singular](#singular) [slug](#slug) [snake](#snake) [squish](#squish) [start](#start) [startsWith](#startswith) [stripTags](#striptags) [studly](#studly) [substr](#substr) [substrCount](#substrcount) [substrReplace](#substrreplace) [swap](#swap) [take](#take) [title](#title) [toBase64](#tobase64) [transliterate](#transliterate) [trim](#trim) [ltrim](#ltrim) [rtrim](#rtrim) [ucfirst](#ucfirst) [ucsplit](#ucsplit) [ucwords](#ucwords) [upper](#upper) [ulid](#ulid) [unwrap](#unwrap) [uuid](#uuid) [uuid7](#uuid7) [wordCount](#wordcount) [wordWrap](#wordwrap) [words](#words) [wrap](#wrap) [str](#str) [of](#of)
|
|
64
|
+
|
|
65
|
+
### String Utilities Details
|
|
66
|
+
|
|
67
|
+
#### after
|
|
68
|
+
|
|
69
|
+
Return the remainder of a string after the first occurrence of a given value.
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
import { after } from "@tolki/str";
|
|
73
|
+
|
|
74
|
+
const result = after("This is my name", "This is");
|
|
75
|
+
|
|
76
|
+
// result is " my name"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
#### afterLast
|
|
80
|
+
|
|
81
|
+
Return the remainder of a string after the last occurrence of a given value.
|
|
82
|
+
|
|
83
|
+
```javascript
|
|
84
|
+
import { afterLast } from "@tolki/str";
|
|
85
|
+
|
|
86
|
+
const result = afterLast("App\Http\Controllers\Controller", "\\");
|
|
87
|
+
|
|
88
|
+
// result is "Controller"
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
#### apa
|
|
92
|
+
|
|
93
|
+
Convert the given string to [APA-style](https://apastyle.apa.org/style-grammar-guidelines/capitalization/title-case) title case.
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
import { apa } from "@tolki/str";
|
|
97
|
+
|
|
98
|
+
const result = apa("Creating A Project");
|
|
99
|
+
|
|
100
|
+
// result is "Creating a Project"
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
#### ascii
|
|
104
|
+
|
|
105
|
+
Transliterate a UTF-8 value to ASCII.
|
|
106
|
+
|
|
107
|
+
Uses the [`transliteration`](https://www.npmjs.com/package/transliteration) package.
|
|
108
|
+
|
|
109
|
+
```javascript
|
|
110
|
+
import { ascii } from "@tolki/str";
|
|
111
|
+
|
|
112
|
+
const result = ascii("û");
|
|
113
|
+
|
|
114
|
+
// result is "u"
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
#### before
|
|
118
|
+
|
|
119
|
+
Get the portion of a string before the first occurrence of a given value.
|
|
120
|
+
|
|
121
|
+
```javascript
|
|
122
|
+
import { before } from "@tolki/str";
|
|
123
|
+
|
|
124
|
+
const result = before("This is my name", "my");
|
|
125
|
+
|
|
126
|
+
// result is "This is "
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
#### beforeLast
|
|
130
|
+
|
|
131
|
+
Get the portion of a string before the last occurrence of a given value.
|
|
132
|
+
|
|
133
|
+
```javascript
|
|
134
|
+
import { beforeLast } from "@tolki/str";
|
|
135
|
+
|
|
136
|
+
const result = beforeLast("This is my name", "is");
|
|
137
|
+
|
|
138
|
+
// result is "This "
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
#### between
|
|
142
|
+
|
|
143
|
+
Get the portion of a string between two given values.
|
|
144
|
+
|
|
145
|
+
```javascript
|
|
146
|
+
import { between } from "@tolki/str";
|
|
147
|
+
|
|
148
|
+
const result = between("This is my name", "This", "name");
|
|
149
|
+
|
|
150
|
+
// result is " is my "
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#### betweenFirst
|
|
154
|
+
|
|
155
|
+
Get the smallest possible portion of a string between two given values.
|
|
156
|
+
|
|
157
|
+
```javascript
|
|
158
|
+
import { betweenFirst } from "@tolki/str";
|
|
159
|
+
|
|
160
|
+
const result = betweenFirst("[a] bc [d]", "[", "]");
|
|
161
|
+
|
|
162
|
+
// result is "a"
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
#### camel
|
|
166
|
+
|
|
167
|
+
Convert a value to camel case.
|
|
168
|
+
|
|
169
|
+
```javascript
|
|
170
|
+
import { camel } from "@tolki/str";
|
|
171
|
+
|
|
172
|
+
const result = camel("foo_bar");
|
|
173
|
+
|
|
174
|
+
// result is "fooBar"
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
#### charAt
|
|
178
|
+
|
|
179
|
+
Get the character at the specified index.
|
|
180
|
+
|
|
181
|
+
```javascript
|
|
182
|
+
import { charAt } from "@tolki/str";
|
|
183
|
+
|
|
184
|
+
const result = charAt("This is my name.", 6);
|
|
185
|
+
|
|
186
|
+
// result is "s"
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
#### chopStart
|
|
190
|
+
|
|
191
|
+
Remove the given string(s) if it exists at the start of the haystack.
|
|
192
|
+
|
|
193
|
+
```javascript
|
|
194
|
+
import { chopStart } from "@tolki/str";
|
|
195
|
+
|
|
196
|
+
const result = chopStart("https://laravel.com", "https://");
|
|
197
|
+
|
|
198
|
+
// result is "laravel.com"
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
You may also pass an array of string as the second argument:
|
|
202
|
+
|
|
203
|
+
```javascript
|
|
204
|
+
import { chopStart } from "@tolki/str";
|
|
205
|
+
|
|
206
|
+
const result = chopStart("http://laravel.com", ["https://", "http://"]);
|
|
207
|
+
|
|
208
|
+
// result is "laravel.com"
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### chopEnd
|
|
212
|
+
|
|
213
|
+
Remove the given string(s) if it exists at the end of the haystack.
|
|
214
|
+
|
|
215
|
+
```javascript
|
|
216
|
+
import { chopEnd } from "@tolki/str";
|
|
217
|
+
|
|
218
|
+
const result = chopEnd("app/Models/Photograph.php", ".php");
|
|
219
|
+
|
|
220
|
+
// result is "app/Models/Photograph"
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
You may also pass an array of string as the second argument:
|
|
224
|
+
|
|
225
|
+
```javascript
|
|
226
|
+
import { chopEnd } from "@tolki/str";
|
|
227
|
+
|
|
228
|
+
const result = chopEnd("laravel.com/index.php", ["/index.html", "/index.php"]);
|
|
229
|
+
|
|
230
|
+
// result is "laravel.com"
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
#### contains
|
|
234
|
+
|
|
235
|
+
Determine if a given string contains a given substring.
|
|
236
|
+
|
|
237
|
+
```javascript
|
|
238
|
+
import { contains } from "@tolki/str";
|
|
239
|
+
|
|
240
|
+
const result = contains("This is my name", "my");
|
|
241
|
+
|
|
242
|
+
// result is true
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
You may also pass an array of strings as the second argument:
|
|
246
|
+
|
|
247
|
+
```javascript
|
|
248
|
+
import { contains } from "@tolki/str";
|
|
249
|
+
|
|
250
|
+
const result = contains("This is my name", ["my", "foo"]);
|
|
251
|
+
|
|
252
|
+
// result is true
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
You may also disable case sensitivity by passing true as the third argument:
|
|
256
|
+
|
|
257
|
+
```javascript
|
|
258
|
+
import { contains } from "@tolki/str";
|
|
259
|
+
|
|
260
|
+
const result = contains("This is my name", "MY", true);
|
|
261
|
+
|
|
262
|
+
// result is true
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
#### containsAll
|
|
266
|
+
|
|
267
|
+
Determine if a given string contains all array values.
|
|
268
|
+
|
|
269
|
+
```javascript
|
|
270
|
+
import { containsAll } from "@tolki/str";
|
|
271
|
+
|
|
272
|
+
const result = containsAll("This is my name", ["my", "name"]);
|
|
273
|
+
|
|
274
|
+
// result is true
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
You may also disable case sensitivity by passing true as the second argument:
|
|
278
|
+
|
|
279
|
+
```javascript
|
|
280
|
+
import { containsAll } from "@tolki/str";
|
|
281
|
+
|
|
282
|
+
const result = containsAll("This is my name", ["MY", "NAME"], true);
|
|
283
|
+
|
|
284
|
+
// result is true
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
#### doesntContain
|
|
288
|
+
|
|
289
|
+
Determine if a given string doesn't contain a given substring.
|
|
290
|
+
|
|
291
|
+
```javascript
|
|
292
|
+
import { doesntContain } from "@tolki/str";
|
|
293
|
+
|
|
294
|
+
const result = doesntContain("This is name", "my");
|
|
295
|
+
|
|
296
|
+
// result is true
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
You may also pass an array of strings as the second argument:
|
|
300
|
+
|
|
301
|
+
```javascript
|
|
302
|
+
import { doesntContain } from "@tolki/str";
|
|
303
|
+
|
|
304
|
+
const result = doesntContain("This is name", ["my", "framework"]);
|
|
305
|
+
|
|
306
|
+
// result is true
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
You may also disable case sensitivity by passing true as the third argument:
|
|
310
|
+
|
|
311
|
+
```javascript
|
|
312
|
+
import { doesntContain } from "@tolki/str";
|
|
313
|
+
|
|
314
|
+
const result = doesntContain("This is name", "MY", true);
|
|
315
|
+
|
|
316
|
+
// result is true
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
#### deduplicate
|
|
320
|
+
|
|
321
|
+
Replace consecutive instances of a given character with a single character in the given string.
|
|
322
|
+
|
|
323
|
+
```javascript
|
|
324
|
+
import { deduplicate } from "@tolki/str";
|
|
325
|
+
|
|
326
|
+
const result = deduplicate("The Laravel Framework");
|
|
327
|
+
|
|
328
|
+
// result is "The Laravel Framework"
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
You can specify which character to deduplicate as the second argument (defaults to space):
|
|
332
|
+
|
|
333
|
+
```javascript
|
|
334
|
+
import { deduplicate } from "@tolki/str";
|
|
335
|
+
|
|
336
|
+
const result = deduplicate("The---Laravel---Framework", "-");
|
|
337
|
+
|
|
338
|
+
// result is "The-Laravel-Framework"
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
You can also specify multiple characters to deduplicate by passing an array as the second argument:
|
|
342
|
+
|
|
343
|
+
```javascript
|
|
344
|
+
import { deduplicate } from "@tolki/str";
|
|
345
|
+
|
|
346
|
+
const result = deduplicate("The---Laravel Framework", ["-", " "]);
|
|
347
|
+
|
|
348
|
+
// result is "The-Laravel Framework"
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
#### doesntEndWith
|
|
352
|
+
|
|
353
|
+
Determine if a given string doesn't end with a given substring.
|
|
354
|
+
|
|
355
|
+
```javascript
|
|
356
|
+
import { doesntEndWith } from "@tolki/str";
|
|
357
|
+
|
|
358
|
+
const result = doesntEndWith("This is my name", "dog");
|
|
359
|
+
|
|
360
|
+
// result is true
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
You may also pass an array of strings as the second argument. If the string ends with any of the substrings, the function will return false.
|
|
364
|
+
|
|
365
|
+
```javascript
|
|
366
|
+
import { doesntEndWith } from "@tolki/str";
|
|
367
|
+
|
|
368
|
+
const result = doesntEndWith("This is my name", ["this", "foo"]);
|
|
369
|
+
|
|
370
|
+
// result is true
|
|
371
|
+
|
|
372
|
+
const result2 = doesntEndWith("This is my name", ["name", "foo"]);
|
|
373
|
+
|
|
374
|
+
// result2 is false
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
#### doesntStartWith
|
|
378
|
+
|
|
379
|
+
Determine if a given string doesn't start with a given substring.
|
|
380
|
+
|
|
381
|
+
```javascript
|
|
382
|
+
import { doesntStartWith } from "@tolki/str";
|
|
383
|
+
|
|
384
|
+
const result = doesntStartWith("This is my name", "That");
|
|
385
|
+
|
|
386
|
+
// result is true
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
You may also pass an array of strings as the second argument. If the string starts with any of the substrings, the function will return false.
|
|
390
|
+
|
|
391
|
+
```javascript
|
|
392
|
+
import { doesntStartWith } from "@tolki/str";
|
|
393
|
+
|
|
394
|
+
const result = doesntStartWith("This is my name", ["this", "foo"]);
|
|
395
|
+
|
|
396
|
+
// result is true
|
|
397
|
+
|
|
398
|
+
const result2 = doesntStartWith("This is my name", ["name", "foo"]);
|
|
399
|
+
|
|
400
|
+
// result2 is true
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
#### endsWith
|
|
404
|
+
|
|
405
|
+
Determine if a given string ends with a given substring.
|
|
406
|
+
|
|
407
|
+
```javascript
|
|
408
|
+
import { endsWith } from "@tolki/str";
|
|
409
|
+
|
|
410
|
+
const result = endsWith("This is my name", "name");
|
|
411
|
+
|
|
412
|
+
// result is true
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
You may also pass an array of strings as the second argument to determine if the string ends with any of the substrings.
|
|
416
|
+
|
|
417
|
+
```javascript
|
|
418
|
+
import { endsWith } from "@tolki/str";
|
|
419
|
+
|
|
420
|
+
const result = endsWith("This is my name", ["name", "foo"]);
|
|
421
|
+
|
|
422
|
+
// result is true
|
|
423
|
+
|
|
424
|
+
const result2 = endsWith("This is my name", ["this", "foo"]);
|
|
425
|
+
|
|
426
|
+
// result2 is false
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
#### excerpt
|
|
430
|
+
|
|
431
|
+
Extracts an excerpt from text that matches the first instance of a phrase.
|
|
432
|
+
|
|
433
|
+
```javascript
|
|
434
|
+
import { excerpt } from "@tolki/str";
|
|
435
|
+
|
|
436
|
+
const result = excerpt("This is my name", "my", { radius: 3 });
|
|
437
|
+
|
|
438
|
+
// result is "... is my na ..."
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
The radius option, which defaults to 100, allows you to define the number of characters that should appear on each side of the truncated string.
|
|
442
|
+
|
|
443
|
+
In addition, you may use the omission option to define the string that will be prepended and appended to the truncated string:
|
|
444
|
+
|
|
445
|
+
```javascript
|
|
446
|
+
import { excerpt } from "@tolki/str";
|
|
447
|
+
|
|
448
|
+
const result = excerpt("This is my name", "name", {
|
|
449
|
+
radius: 3,
|
|
450
|
+
omission: "(...) ",
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
// result is "(...) my name"
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
#### finish
|
|
457
|
+
|
|
458
|
+
Cap a string with a single instance of a given value.
|
|
459
|
+
|
|
460
|
+
```javascript
|
|
461
|
+
import { finish } from "@tolki/str";
|
|
462
|
+
|
|
463
|
+
const result = finish("this/string", "/");
|
|
464
|
+
|
|
465
|
+
// result is 'this/string/'
|
|
466
|
+
|
|
467
|
+
const result2 = finish("this/string/", "/");
|
|
468
|
+
|
|
469
|
+
// result2 is also 'this/string/'
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
#### fromBase64
|
|
473
|
+
|
|
474
|
+
Decode the given Base64 encoded string.
|
|
475
|
+
|
|
476
|
+
```javascript
|
|
477
|
+
import { fromBase64 } from "@tolki/str";
|
|
478
|
+
|
|
479
|
+
const result = fromBase64("TGFyYXZlbA==");
|
|
480
|
+
|
|
481
|
+
// result is 'Laravel'
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
#### headline
|
|
485
|
+
|
|
486
|
+
Convert the given string to proper case for each word.
|
|
487
|
+
|
|
488
|
+
```javascript
|
|
489
|
+
import { headline } from "@tolki/str";
|
|
490
|
+
|
|
491
|
+
const result = headline("steve_jobs");
|
|
492
|
+
|
|
493
|
+
// result is "Steve Jobs"
|
|
494
|
+
|
|
495
|
+
const result2 = headline("EmailNotificationSent");
|
|
496
|
+
|
|
497
|
+
// result2 is "Email Notification Sent"
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
#### inlineMarkdown
|
|
501
|
+
|
|
502
|
+
Converts inline Markdown into HTML.
|
|
503
|
+
|
|
504
|
+
Uses the [`markdown-it`](https://www.npmjs.com/package/markdown-it) package.
|
|
505
|
+
|
|
506
|
+
```javascript
|
|
507
|
+
import { inlineMarkdown } from "@tolki/str";
|
|
508
|
+
|
|
509
|
+
const result = inlineMarkdown("This is **bold** and this is *italic*.");
|
|
510
|
+
|
|
511
|
+
// result is 'This is <strong>bold</strong> and this is <em>italic</em>.'
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
##### Inline Markdown Security
|
|
515
|
+
|
|
516
|
+
By default, the `inlineMarkdown` function disables raw HTML and unsafe links (e.g., `javascript:` URLs) to prevent XSS attacks. You can enable raw HTML by passing `{ html: true }` in the options.
|
|
517
|
+
|
|
518
|
+
```javascript
|
|
519
|
+
import { inlineMarkdown } from "@tolki/str";
|
|
520
|
+
|
|
521
|
+
const result = inlineMarkdown(
|
|
522
|
+
"This is <strong>bold</strong> and this is <em>italic</em>.",
|
|
523
|
+
{ html: true },
|
|
524
|
+
);
|
|
525
|
+
|
|
526
|
+
// result is 'This is <strong>bold</strong> and this is <em>italic</em>.'
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
You can also enable unsafe links by passing `{ allowUnsafeLinks: true }` in the options.
|
|
530
|
+
|
|
531
|
+
```javascript
|
|
532
|
+
import { inlineMarkdown } from "@tolki/str";
|
|
533
|
+
|
|
534
|
+
const result = inlineMarkdown("[click me](javascript:alert(1))", {
|
|
535
|
+
allowUnsafeLinks: true,
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
// result is '<a href="javascript:alert(1)">click me</a>'
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
#### is
|
|
542
|
+
|
|
543
|
+
Determine if a given string matches a given pattern.
|
|
544
|
+
|
|
545
|
+
```javascript
|
|
546
|
+
import { is } from "@tolki/str";
|
|
547
|
+
|
|
548
|
+
const result = is("foo*", "foobar");
|
|
549
|
+
|
|
550
|
+
// result is true
|
|
551
|
+
|
|
552
|
+
const result2 = is("baz*", "foobar");
|
|
553
|
+
|
|
554
|
+
// result2 is false
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
You can disable case sensitivity by passing true as the third argument:
|
|
558
|
+
|
|
559
|
+
```javascript
|
|
560
|
+
import { is } from "@tolki/str";
|
|
561
|
+
|
|
562
|
+
const result = is("*.jpg", "photo.JPG", true);
|
|
563
|
+
|
|
564
|
+
// result is true
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
#### isAscii
|
|
568
|
+
|
|
569
|
+
Determine if a given string is 7 bit ASCII.
|
|
570
|
+
|
|
571
|
+
```javascript
|
|
572
|
+
import { isAscii } from "@tolki/str";
|
|
573
|
+
|
|
574
|
+
const result = isAscii("Taylor");
|
|
575
|
+
|
|
576
|
+
// result is true
|
|
577
|
+
|
|
578
|
+
const result2 = isAscii("û");
|
|
579
|
+
|
|
580
|
+
// result2 is false
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
#### isJson
|
|
584
|
+
|
|
585
|
+
Determine if a given value is valid JSON.
|
|
586
|
+
|
|
587
|
+
```javascript
|
|
588
|
+
import { isJson } from "@tolki/str";
|
|
589
|
+
|
|
590
|
+
const result = isJson("[1,2,3]");
|
|
591
|
+
|
|
592
|
+
// result is true
|
|
593
|
+
|
|
594
|
+
const result2 = isJson('{"first": "John", "last": "Doe"}');
|
|
595
|
+
|
|
596
|
+
// result2 is true
|
|
597
|
+
|
|
598
|
+
const result3 = isJson('{first: "John", last: "Doe"}');
|
|
599
|
+
|
|
600
|
+
// result3 is false
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
#### isUrl
|
|
604
|
+
|
|
605
|
+
Determine if a given value is a valid URL.
|
|
606
|
+
|
|
607
|
+
```javascript
|
|
608
|
+
import { isUrl } from "@tolki/str";
|
|
609
|
+
|
|
610
|
+
const result = isUrl("https://example.com");
|
|
611
|
+
|
|
612
|
+
// result is true
|
|
613
|
+
|
|
614
|
+
const result2 = isUrl("tolki js");
|
|
615
|
+
|
|
616
|
+
// result2 is false
|
|
617
|
+
```
|
|
618
|
+
|
|
619
|
+
You may also specify an array of allowed protocols as the second argument:
|
|
620
|
+
|
|
621
|
+
```javascript
|
|
622
|
+
import { isUrl } from "@tolki/str";
|
|
623
|
+
|
|
624
|
+
const result = isUrl("https://example.com", ["https", "http"]);
|
|
625
|
+
|
|
626
|
+
// result is true
|
|
627
|
+
|
|
628
|
+
const result2 = isUrl("http://example.com", ["https"]);
|
|
629
|
+
|
|
630
|
+
// result2 is false
|
|
631
|
+
```
|
|
632
|
+
|
|
633
|
+
#### isUlid
|
|
634
|
+
|
|
635
|
+
Determine if a given value is a valid ULID.
|
|
636
|
+
|
|
637
|
+
```javascript
|
|
638
|
+
import { isUlid } from "@tolki/str";
|
|
639
|
+
|
|
640
|
+
const result = isUlid("01gd6r360bp37zj17nxb55yv40");
|
|
641
|
+
|
|
642
|
+
// result is true
|
|
643
|
+
|
|
644
|
+
const result2 = isUlid("tolkijs");
|
|
645
|
+
|
|
646
|
+
// result2 is false
|
|
647
|
+
```
|
|
648
|
+
|
|
649
|
+
#### isUuid
|
|
650
|
+
|
|
651
|
+
Determine if a given value is a valid UUID.
|
|
652
|
+
|
|
653
|
+
Uses the [`uuid`](https://www.npmjs.com/package/uuid) package.
|
|
654
|
+
|
|
655
|
+
```javascript
|
|
656
|
+
import { isUuid } from "@tolki/str";
|
|
657
|
+
|
|
658
|
+
const result = isUuid("a0a2a2d2-0b87-4a18-83f2-2529882be2de");
|
|
659
|
+
|
|
660
|
+
// result is true
|
|
661
|
+
|
|
662
|
+
const result2 = isUuid("tolkijs");
|
|
663
|
+
|
|
664
|
+
// result2 is false
|
|
665
|
+
```
|
|
666
|
+
|
|
667
|
+
You may also validate that the given UUID matches a UUID specification by version (1, 3, 4, 5, 6, 7, or 8):
|
|
668
|
+
|
|
669
|
+
```javascript
|
|
670
|
+
import { isUuid } from "@tolki/str";
|
|
671
|
+
|
|
672
|
+
const result = isUuid("a0a2a2d2-0b87-4a18-83f2-2529882be2de", 4);
|
|
673
|
+
|
|
674
|
+
// result is true
|
|
675
|
+
|
|
676
|
+
const result2 = isUuid("a0a2a2d2-0b87-4a18-83f2-2529882be2de", 5);
|
|
677
|
+
|
|
678
|
+
// result2 is false
|
|
679
|
+
```
|
|
680
|
+
|
|
681
|
+
#### kebab
|
|
682
|
+
|
|
683
|
+
Convert a string to kebab case.
|
|
684
|
+
|
|
685
|
+
```javascript
|
|
686
|
+
import { kebab } from "@tolki/str";
|
|
687
|
+
|
|
688
|
+
const result = kebab("fooBar");
|
|
689
|
+
|
|
690
|
+
// result is "foo-bar"
|
|
691
|
+
```
|
|
692
|
+
|
|
693
|
+
#### lcfirst
|
|
694
|
+
|
|
695
|
+
Make a string's first character lowercase.
|
|
696
|
+
|
|
697
|
+
```javascript
|
|
698
|
+
import { lcfirst } from "@tolki/str";
|
|
699
|
+
|
|
700
|
+
const result = lcfirst("Foo Bar");
|
|
701
|
+
|
|
702
|
+
// result is "foo Bar"
|
|
703
|
+
```
|
|
704
|
+
|
|
705
|
+
#### length
|
|
706
|
+
|
|
707
|
+
Return the length of the given string.
|
|
708
|
+
|
|
709
|
+
```javascript
|
|
710
|
+
import { length } from "@tolki/str";
|
|
711
|
+
|
|
712
|
+
const result = length("Tolki JS");
|
|
713
|
+
|
|
714
|
+
// result is 8
|
|
715
|
+
```
|
|
716
|
+
|
|
717
|
+
#### limit
|
|
718
|
+
|
|
719
|
+
Limit the number of characters in a string.
|
|
720
|
+
|
|
721
|
+
```javascript
|
|
722
|
+
import { limit } from "@tolki/str";
|
|
723
|
+
|
|
724
|
+
const result = limit("The quick brown fox jumps over the lazy dog", 20);
|
|
725
|
+
|
|
726
|
+
// result is "The quick brown fox "
|
|
727
|
+
```
|
|
728
|
+
|
|
729
|
+
You may pass a third argument to specify the string to append to the truncated string (defaults to an ellipsis):
|
|
730
|
+
|
|
731
|
+
```javascript
|
|
732
|
+
import { limit } from "@tolki/str";
|
|
733
|
+
|
|
734
|
+
const result = limit("The quick brown fox jumps over the lazy dog", 20, "...");
|
|
735
|
+
|
|
736
|
+
// result is "The quick brown fox..."
|
|
737
|
+
```
|
|
738
|
+
|
|
739
|
+
You may also pass a fourth argument to indicate whether to avoid cutting off words (defaults to false):
|
|
740
|
+
|
|
741
|
+
```javascript
|
|
742
|
+
import { limit } from "@tolki/str";
|
|
743
|
+
|
|
744
|
+
const result = limit(
|
|
745
|
+
"The quick brown fox jumps over the lazy dog",
|
|
746
|
+
12,
|
|
747
|
+
"...",
|
|
748
|
+
true,
|
|
749
|
+
);
|
|
750
|
+
|
|
751
|
+
// result is "The quick..."
|
|
752
|
+
```
|
|
753
|
+
|
|
754
|
+
#### lower
|
|
755
|
+
|
|
756
|
+
Convert the given string to lower-case.
|
|
757
|
+
|
|
758
|
+
```javascript
|
|
759
|
+
import { lower } from "@tolki/str";
|
|
760
|
+
|
|
761
|
+
const result = lower("LARAVEL");
|
|
762
|
+
|
|
763
|
+
// result is "laravel"
|
|
764
|
+
```
|
|
765
|
+
|
|
766
|
+
#### markdown
|
|
767
|
+
|
|
768
|
+
Converts GitHub flavored Markdown into HTML.
|
|
769
|
+
|
|
770
|
+
Uses the [`markdown-it`](https://www.npmjs.com/package/markdown-it) package.
|
|
771
|
+
|
|
772
|
+
```javascript
|
|
773
|
+
import { markdown } from "@tolki/str";
|
|
774
|
+
|
|
775
|
+
const result = markdown("# Laravel");
|
|
776
|
+
|
|
777
|
+
// result is "<h1>Laravel</h1>"
|
|
778
|
+
```
|
|
779
|
+
|
|
780
|
+
##### Markdown Security
|
|
781
|
+
|
|
782
|
+
By default, the `markdown` function disables raw HTML and unsafe links (e.g., `javascript:` URLs) to prevent XSS attacks. You can enable raw HTML by passing `{ html: true }` in the options.
|
|
783
|
+
|
|
784
|
+
```javascript
|
|
785
|
+
import { markdown } from "@tolki/str";
|
|
786
|
+
|
|
787
|
+
const result = markdown(
|
|
788
|
+
"This is <strong>bold</strong> and this is <em>italic</em>.",
|
|
789
|
+
{ html: true },
|
|
790
|
+
);
|
|
791
|
+
|
|
792
|
+
// result is '<p>This is <strong>bold</strong> and this is <em>italic</em>.</p>'
|
|
793
|
+
```
|
|
794
|
+
|
|
795
|
+
You can also enable unsafe links by passing `{ allowUnsafeLinks: true }` in the options.
|
|
796
|
+
|
|
797
|
+
```javascript
|
|
798
|
+
import { markdown } from "@tolki/str";
|
|
799
|
+
|
|
800
|
+
const result = markdown("[click me](javascript:alert(1))", {
|
|
801
|
+
allowUnsafeLinks: true,
|
|
802
|
+
});
|
|
803
|
+
|
|
804
|
+
// result is '<p><a href="javascript:alert(1)">click me</a></p>'
|
|
805
|
+
```
|
|
806
|
+
|
|
807
|
+
#### mask
|
|
808
|
+
|
|
809
|
+
Masks a portion of a string with a repeated character.
|
|
810
|
+
|
|
811
|
+
```javascript
|
|
812
|
+
import { mask } from "@tolki/str";
|
|
813
|
+
|
|
814
|
+
const result = mask("taylor@example.com", "*", 3);
|
|
815
|
+
|
|
816
|
+
// result is "tay***************"
|
|
817
|
+
```
|
|
818
|
+
|
|
819
|
+
If needed, you provide a negative value for the third argument, which instructs the function to begin masking from the end of the string. A fourth argument may also provided to specify the number of masked characters.
|
|
820
|
+
|
|
821
|
+
```javascript
|
|
822
|
+
import { mask } from "@tolki/str";
|
|
823
|
+
|
|
824
|
+
const result = mask("taylor@example.com", "*", -15, 3);
|
|
825
|
+
|
|
826
|
+
// result is "tay***@example.com"
|
|
827
|
+
```
|
|
828
|
+
|
|
829
|
+
#### match
|
|
830
|
+
|
|
831
|
+
Get the string matching the given pattern.
|
|
832
|
+
|
|
833
|
+
```javascript
|
|
834
|
+
import { match } from "@tolki/str";
|
|
835
|
+
|
|
836
|
+
const result = match("/bar/", "foo bar");
|
|
837
|
+
|
|
838
|
+
// result is "bar"
|
|
839
|
+
|
|
840
|
+
const result2 = match("/foo (.*)/", "foo bar");
|
|
841
|
+
|
|
842
|
+
// result2 is "bar"
|
|
843
|
+
```
|
|
844
|
+
|
|
845
|
+
#### matchAll
|
|
846
|
+
|
|
847
|
+
Get the string(s) matching the given pattern.
|
|
848
|
+
|
|
849
|
+
```javascript
|
|
850
|
+
import { matchAll } from "@tolki/str";
|
|
851
|
+
|
|
852
|
+
const result = matchAll("/bar/", "bar foo bar");
|
|
853
|
+
|
|
854
|
+
// result is ["bar", "bar"]
|
|
855
|
+
```
|
|
856
|
+
|
|
857
|
+
If no matches are found, an empty array will be returned.
|
|
858
|
+
|
|
859
|
+
#### isMatch
|
|
860
|
+
|
|
861
|
+
Determine if a given string matches a given pattern.
|
|
862
|
+
|
|
863
|
+
```javascript
|
|
864
|
+
import { isMatch } from "@tolki/str";
|
|
865
|
+
|
|
866
|
+
const result = isMatch("/foo (.*)/", "foo bar");
|
|
867
|
+
|
|
868
|
+
// result is true
|
|
869
|
+
|
|
870
|
+
const result2 = isMatch("/foo (.*)/", "laravel");
|
|
871
|
+
|
|
872
|
+
// result2 is false
|
|
873
|
+
```
|
|
874
|
+
|
|
875
|
+
#### orderedUuid
|
|
876
|
+
|
|
877
|
+
This function is purposely not implemented. Use the `uuid7()` function instead to generate a UUIDv7, which is a time-ordered UUID.
|
|
878
|
+
|
|
879
|
+
See more details on this [StackOverflow discussion](https://stackoverflow.com/a/79196945).
|
|
880
|
+
|
|
881
|
+
#### numbers
|
|
882
|
+
|
|
883
|
+
Remove all non-numeric characters from a string.
|
|
884
|
+
|
|
885
|
+
```javascript
|
|
886
|
+
import { numbers } from "@tolki/str";
|
|
887
|
+
|
|
888
|
+
const result = numbers("(555) 123-4567");
|
|
889
|
+
|
|
890
|
+
// result is "5551234567"
|
|
891
|
+
|
|
892
|
+
const result2 = numbers("L4r4v3l!");
|
|
893
|
+
|
|
894
|
+
// result2 is "443"
|
|
895
|
+
```
|
|
896
|
+
|
|
897
|
+
#### padBoth
|
|
898
|
+
|
|
899
|
+
Pad both sides of a string with another string to a certain length.
|
|
900
|
+
|
|
901
|
+
```javascript
|
|
902
|
+
import { padBoth } from "@tolki/str";
|
|
903
|
+
|
|
904
|
+
const result = padBoth("James", 10, "_");
|
|
905
|
+
|
|
906
|
+
// result is "__James__"
|
|
907
|
+
|
|
908
|
+
const result2 = padBoth("James", 10);
|
|
909
|
+
|
|
910
|
+
// result2 is " James "
|
|
911
|
+
```
|
|
912
|
+
|
|
913
|
+
#### padLeft
|
|
914
|
+
|
|
915
|
+
Pad the left side of a string with another string to a certain length.
|
|
916
|
+
|
|
917
|
+
```javascript
|
|
918
|
+
import { padLeft } from "@tolki/str";
|
|
919
|
+
|
|
920
|
+
const result = padLeft("James", 10, "-=");
|
|
921
|
+
|
|
922
|
+
// result is "-=-=-James"
|
|
923
|
+
|
|
924
|
+
const result2 = padLeft("James", 10);
|
|
925
|
+
|
|
926
|
+
// result2 is " James"
|
|
927
|
+
```
|
|
928
|
+
|
|
929
|
+
#### padRight
|
|
930
|
+
|
|
931
|
+
Pad the right side of a string with another string to a certain length.
|
|
932
|
+
|
|
933
|
+
```javascript
|
|
934
|
+
import { padRight } from "@tolki/str";
|
|
935
|
+
|
|
936
|
+
const result = padRight("James", 10, "-");
|
|
937
|
+
|
|
938
|
+
// result is "James-----"
|
|
939
|
+
|
|
940
|
+
const result2 = padRight("James", 10);
|
|
941
|
+
|
|
942
|
+
// result2 is "James "
|
|
943
|
+
```
|
|
944
|
+
|
|
945
|
+
#### pascal
|
|
946
|
+
|
|
947
|
+
Convert a value to Pascal case.
|
|
948
|
+
|
|
949
|
+
```javascript
|
|
950
|
+
import { pascal } from "@tolki/str";
|
|
951
|
+
|
|
952
|
+
const result = pascal("hello world");
|
|
953
|
+
|
|
954
|
+
// result is "HelloWorld"
|
|
955
|
+
```
|
|
956
|
+
|
|
957
|
+
#### pluralPascal
|
|
958
|
+
|
|
959
|
+
Pluralize the last word of an English, Pascal caps case string.
|
|
960
|
+
|
|
961
|
+
```javascript
|
|
962
|
+
import { pluralPascal } from "@tolki/str";
|
|
963
|
+
|
|
964
|
+
const result = pluralPascal("HelloWorld");
|
|
965
|
+
|
|
966
|
+
// result is "HelloWorlds"
|
|
967
|
+
```
|
|
968
|
+
|
|
969
|
+
#### password
|
|
970
|
+
|
|
971
|
+
Generate a random, secure password.
|
|
972
|
+
|
|
973
|
+
```javascript
|
|
974
|
+
import { password } from "@tolki/str";
|
|
975
|
+
|
|
976
|
+
const result = password();
|
|
977
|
+
|
|
978
|
+
// result is a random, secure password
|
|
979
|
+
```
|
|
980
|
+
|
|
981
|
+
#### plural
|
|
982
|
+
|
|
983
|
+
Get the plural form of an English word.
|
|
984
|
+
|
|
985
|
+
```javascript
|
|
986
|
+
import { plural } from "@tolki/str";
|
|
987
|
+
|
|
988
|
+
const result = plural("car");
|
|
989
|
+
|
|
990
|
+
// result is "cars"
|
|
991
|
+
|
|
992
|
+
const result2 = plural("child");
|
|
993
|
+
|
|
994
|
+
// result2 is "children"
|
|
995
|
+
```
|
|
996
|
+
|
|
997
|
+
You may provide a second argument to specify the count. If the count is 1, the singular form will be returned.
|
|
998
|
+
|
|
999
|
+
```javascript
|
|
1000
|
+
import { plural } from "@tolki/str";
|
|
1001
|
+
|
|
1002
|
+
const result = plural("child", 2);
|
|
1003
|
+
|
|
1004
|
+
// result is "children"
|
|
1005
|
+
|
|
1006
|
+
const result2 = plural("child", 1);
|
|
1007
|
+
|
|
1008
|
+
// result2 is "child"
|
|
1009
|
+
```
|
|
1010
|
+
|
|
1011
|
+
You pass a third argument to prepend the count to the resulting string.
|
|
1012
|
+
|
|
1013
|
+
```javascript
|
|
1014
|
+
import { plural } from "@tolki/str";
|
|
1015
|
+
|
|
1016
|
+
const result = plural("car", 1000, true);
|
|
1017
|
+
|
|
1018
|
+
// result is "1,000 cars"
|
|
1019
|
+
```
|
|
1020
|
+
|
|
1021
|
+
#### pluralStudly
|
|
1022
|
+
|
|
1023
|
+
Pluralize the last word of an English, studly caps case string.
|
|
1024
|
+
|
|
1025
|
+
```javascript
|
|
1026
|
+
import { pluralStudly } from "@tolki/str";
|
|
1027
|
+
|
|
1028
|
+
const result = pluralStudly("VerifiedHuman");
|
|
1029
|
+
|
|
1030
|
+
// result is "VerifiedHumans"
|
|
1031
|
+
|
|
1032
|
+
const result2 = pluralStudly("UserFeedback");
|
|
1033
|
+
|
|
1034
|
+
// result2 is "UserFeedback"
|
|
1035
|
+
```
|
|
1036
|
+
|
|
1037
|
+
#### position
|
|
1038
|
+
|
|
1039
|
+
Find the multi-byte safe position of the first occurrence of a given substring in a string.
|
|
1040
|
+
|
|
1041
|
+
```javascript
|
|
1042
|
+
import { position } from "@tolki/str";
|
|
1043
|
+
|
|
1044
|
+
const result = position("Hello, World!", "Hello");
|
|
1045
|
+
|
|
1046
|
+
// result is 0
|
|
1047
|
+
|
|
1048
|
+
const result2 = position("Hello, World!", "W");
|
|
1049
|
+
|
|
1050
|
+
// result2 is 7
|
|
1051
|
+
```
|
|
1052
|
+
|
|
1053
|
+
#### random
|
|
1054
|
+
|
|
1055
|
+
Generate a more truly "random" alpha-numeric string.
|
|
1056
|
+
|
|
1057
|
+
```javascript
|
|
1058
|
+
import { random } from "@tolki/str";
|
|
1059
|
+
|
|
1060
|
+
const result = random(40);
|
|
1061
|
+
|
|
1062
|
+
// result is a random 40-character alpha-numeric string
|
|
1063
|
+
```
|
|
1064
|
+
|
|
1065
|
+
During testing, you can use the `createRandomStringsUsing` function to provide a custom random string generator for predictable results.
|
|
1066
|
+
|
|
1067
|
+
```javascript
|
|
1068
|
+
import { createRandomStringsUsing, random } from "@tolki/str";
|
|
1069
|
+
|
|
1070
|
+
createRandomStringsUsing((length) => "A".repeat(length));
|
|
1071
|
+
|
|
1072
|
+
const result = random(5);
|
|
1073
|
+
|
|
1074
|
+
// result is "AAAAA"
|
|
1075
|
+
```
|
|
1076
|
+
|
|
1077
|
+
You can reset to the default random string generator by calling the `createRandomStringsNormally` function.
|
|
1078
|
+
|
|
1079
|
+
```javascript
|
|
1080
|
+
import {
|
|
1081
|
+
createRandomStringsUsing,
|
|
1082
|
+
createRandomStringsNormally,
|
|
1083
|
+
random,
|
|
1084
|
+
} from "@tolki/str";
|
|
1085
|
+
|
|
1086
|
+
createRandomStringsUsing((length) => "A".repeat(length));
|
|
1087
|
+
|
|
1088
|
+
const result = random(5);
|
|
1089
|
+
|
|
1090
|
+
// result is "AAAAA"
|
|
1091
|
+
|
|
1092
|
+
createRandomStringsNormally();
|
|
1093
|
+
|
|
1094
|
+
const result2 = random(5);
|
|
1095
|
+
|
|
1096
|
+
// result is a random 5-character alpha-numeric string
|
|
1097
|
+
```
|
|
1098
|
+
|
|
1099
|
+
#### remove
|
|
1100
|
+
|
|
1101
|
+
Remove any occurrence of the given string in the subject.
|
|
1102
|
+
|
|
1103
|
+
```javascript
|
|
1104
|
+
import { remove } from "@tolki/str";
|
|
1105
|
+
|
|
1106
|
+
const result = remove("e", "Peter Piper picked a peck of pickled peppers.");
|
|
1107
|
+
|
|
1108
|
+
// result is "Ptr Pipr pickd a pck of pickld ppprs."
|
|
1109
|
+
```
|
|
1110
|
+
|
|
1111
|
+
You can pass false as the third argument to disable case sensitivity:
|
|
1112
|
+
|
|
1113
|
+
```javascript
|
|
1114
|
+
import { remove } from "@tolki/str";
|
|
1115
|
+
|
|
1116
|
+
const result = remove(
|
|
1117
|
+
"e",
|
|
1118
|
+
"Peter Piper picked a peck of pickled peppers.",
|
|
1119
|
+
false,
|
|
1120
|
+
);
|
|
1121
|
+
|
|
1122
|
+
// result is "Ptr Pipr pickd a pck of pickld ppprs."
|
|
1123
|
+
```
|
|
1124
|
+
|
|
1125
|
+
#### repeat
|
|
1126
|
+
|
|
1127
|
+
Repeat the given string.
|
|
1128
|
+
|
|
1129
|
+
```javascript
|
|
1130
|
+
import { repeat } from "@tolki/str";
|
|
1131
|
+
|
|
1132
|
+
const result = repeat("a", 5);
|
|
1133
|
+
|
|
1134
|
+
// result is "aaaaa"
|
|
1135
|
+
```
|
|
1136
|
+
|
|
1137
|
+
#### replace
|
|
1138
|
+
|
|
1139
|
+
Replace the given value in the given string.
|
|
1140
|
+
|
|
1141
|
+
```javascript
|
|
1142
|
+
import { replace } from "@tolki/str";
|
|
1143
|
+
|
|
1144
|
+
const result = replace("11.x", "12.x", "Laravel 11.x");
|
|
1145
|
+
|
|
1146
|
+
// result is "Laravel 12.x"
|
|
1147
|
+
```
|
|
1148
|
+
|
|
1149
|
+
The replace function also accepts a fourth argument to disable case sensitivity:
|
|
1150
|
+
|
|
1151
|
+
```javascript
|
|
1152
|
+
import { replace } from "@tolki/str";
|
|
1153
|
+
|
|
1154
|
+
const result = replace(
|
|
1155
|
+
"php",
|
|
1156
|
+
"Laravel",
|
|
1157
|
+
"PHP Framework for Web Artisans",
|
|
1158
|
+
false,
|
|
1159
|
+
);
|
|
1160
|
+
|
|
1161
|
+
// result is "Laravel Framework for Web Artisans"
|
|
1162
|
+
```
|
|
1163
|
+
|
|
1164
|
+
#### replaceArray
|
|
1165
|
+
|
|
1166
|
+
Replace a given value in the string sequentially with an array.
|
|
1167
|
+
|
|
1168
|
+
```javascript
|
|
1169
|
+
import { replaceArray } from "@tolki/str";
|
|
1170
|
+
|
|
1171
|
+
const result = replaceArray(
|
|
1172
|
+
"?",
|
|
1173
|
+
["8:30", "9:00"],
|
|
1174
|
+
"The event will take place between ? and ?",
|
|
1175
|
+
);
|
|
1176
|
+
|
|
1177
|
+
// result is "The event will take place between 8:30 and 9:00"
|
|
1178
|
+
```
|
|
1179
|
+
|
|
1180
|
+
#### replaceFirst
|
|
1181
|
+
|
|
1182
|
+
Replace the first occurrence of a given value in the string.
|
|
1183
|
+
|
|
1184
|
+
```javascript
|
|
1185
|
+
import { replaceFirst } from "@tolki/str";
|
|
1186
|
+
|
|
1187
|
+
const result = replaceFirst(
|
|
1188
|
+
"the",
|
|
1189
|
+
"a",
|
|
1190
|
+
"the quick brown fox jumps over the lazy dog",
|
|
1191
|
+
);
|
|
1192
|
+
|
|
1193
|
+
// result is "a quick brown fox jumps over the lazy dog"
|
|
1194
|
+
```
|
|
1195
|
+
|
|
1196
|
+
#### replaceLast
|
|
1197
|
+
|
|
1198
|
+
Replace the last occurrence of a given value in the string.
|
|
1199
|
+
|
|
1200
|
+
```javascript
|
|
1201
|
+
import { replaceLast } from "@tolki/str";
|
|
1202
|
+
|
|
1203
|
+
const result = replaceLast(
|
|
1204
|
+
"the",
|
|
1205
|
+
"a",
|
|
1206
|
+
"the quick brown fox jumps over the lazy dog",
|
|
1207
|
+
);
|
|
1208
|
+
|
|
1209
|
+
// result is "the quick brown fox jumps over a lazy dog"
|
|
1210
|
+
```
|
|
1211
|
+
|
|
1212
|
+
#### replaceMatches
|
|
1213
|
+
|
|
1214
|
+
Replace the patterns matching the given regular expression.
|
|
1215
|
+
|
|
1216
|
+
```javascript
|
|
1217
|
+
import { replaceMatches } from "@tolki/str";
|
|
1218
|
+
|
|
1219
|
+
const result = replaceMatches(/[^A-Za-z0-9]+/g, "", "(+1) 501-555-1000");
|
|
1220
|
+
|
|
1221
|
+
// result is "15015551000"
|
|
1222
|
+
```
|
|
1223
|
+
|
|
1224
|
+
The `replaceMatches` function also accepts a closure as the second argument, allowing you to perform the replacement logic within the closure and return the replaced value.
|
|
1225
|
+
|
|
1226
|
+
```javascript
|
|
1227
|
+
import { replaceMatches } from "@tolki/str";
|
|
1228
|
+
|
|
1229
|
+
const result = replaceMatches(
|
|
1230
|
+
/\d+/g,
|
|
1231
|
+
(matches) => `number ${matches[0]}`,
|
|
1232
|
+
"My numbers are 123 and 456.",
|
|
1233
|
+
);
|
|
1234
|
+
|
|
1235
|
+
// result is "My numbers are number 123 and number 456."
|
|
1236
|
+
```
|
|
1237
|
+
|
|
1238
|
+
#### replaceStart
|
|
1239
|
+
|
|
1240
|
+
Replace the first occurrence of the given value if it appears at the start of the string.
|
|
1241
|
+
|
|
1242
|
+
```javascript
|
|
1243
|
+
import { replaceStart } from "@tolki/str";
|
|
1244
|
+
|
|
1245
|
+
const result = replaceStart("Hello", "Laravel", "Hello World");
|
|
1246
|
+
|
|
1247
|
+
// result is "Laravel World"
|
|
1248
|
+
|
|
1249
|
+
const result2 = replaceStart("World", "Laravel", "Hello World");
|
|
1250
|
+
|
|
1251
|
+
// result2 is "Hello World"
|
|
1252
|
+
```
|
|
1253
|
+
|
|
1254
|
+
#### replaceEnd
|
|
1255
|
+
|
|
1256
|
+
Replace the last occurrence of a given value if it appears at the end of the string.
|
|
1257
|
+
|
|
1258
|
+
```javascript
|
|
1259
|
+
import { replaceEnd } from "@tolki/str";
|
|
1260
|
+
|
|
1261
|
+
const result = replaceEnd("World", "Laravel", "Hello World");
|
|
1262
|
+
|
|
1263
|
+
// result is "Hello Laravel"
|
|
1264
|
+
|
|
1265
|
+
const result2 = replaceEnd("Hello", "Laravel", "Hello World");
|
|
1266
|
+
|
|
1267
|
+
// result2 is "Hello World"
|
|
1268
|
+
```
|
|
1269
|
+
|
|
1270
|
+
#### reverse
|
|
1271
|
+
|
|
1272
|
+
Reverse the given string.
|
|
1273
|
+
|
|
1274
|
+
```javascript
|
|
1275
|
+
import { reverse } from "@tolki/str";
|
|
1276
|
+
|
|
1277
|
+
const result = reverse("Hello World");
|
|
1278
|
+
|
|
1279
|
+
// result is "dlroW olleH"
|
|
1280
|
+
```
|
|
1281
|
+
|
|
1282
|
+
#### singular
|
|
1283
|
+
|
|
1284
|
+
Get the singular form of an English word.
|
|
1285
|
+
|
|
1286
|
+
Uses the [`pluralize`](https://www.npmjs.com/package/pluralize) package.
|
|
1287
|
+
|
|
1288
|
+
```javascript
|
|
1289
|
+
import { singular } from "@tolki/str";
|
|
1290
|
+
|
|
1291
|
+
const result = singular("cars");
|
|
1292
|
+
|
|
1293
|
+
// result is "car"
|
|
1294
|
+
|
|
1295
|
+
const result2 = singular("children");
|
|
1296
|
+
|
|
1297
|
+
// result2 is "child"
|
|
1298
|
+
```
|
|
1299
|
+
|
|
1300
|
+
#### slug
|
|
1301
|
+
|
|
1302
|
+
Generate a URL friendly "slug" from a given string.
|
|
1303
|
+
|
|
1304
|
+
```javascript
|
|
1305
|
+
import { slug } from "@tolki/str";
|
|
1306
|
+
|
|
1307
|
+
const result = slug("Laravel 5 Framework", "-");
|
|
1308
|
+
|
|
1309
|
+
// result is "laravel-5-framework"
|
|
1310
|
+
```
|
|
1311
|
+
|
|
1312
|
+
#### snake
|
|
1313
|
+
|
|
1314
|
+
Convert a string to snake case.
|
|
1315
|
+
|
|
1316
|
+
```javascript
|
|
1317
|
+
import { snake } from "@tolki/str";
|
|
1318
|
+
|
|
1319
|
+
const result = snake("fooBar");
|
|
1320
|
+
|
|
1321
|
+
// result is "foo_bar"
|
|
1322
|
+
|
|
1323
|
+
const result2 = snake("fooBar", "-");
|
|
1324
|
+
|
|
1325
|
+
// result2 is "foo-bar"
|
|
1326
|
+
```
|
|
1327
|
+
|
|
1328
|
+
#### squish
|
|
1329
|
+
|
|
1330
|
+
Remove all "extra" blank space from the given string.
|
|
1331
|
+
|
|
1332
|
+
```javascript
|
|
1333
|
+
import { squish } from "@tolki/str";
|
|
1334
|
+
|
|
1335
|
+
const result = squish(" laravel framework ");
|
|
1336
|
+
|
|
1337
|
+
// result is "laravel framework"
|
|
1338
|
+
```
|
|
1339
|
+
|
|
1340
|
+
#### start
|
|
1341
|
+
|
|
1342
|
+
Begin a string with a single instance of a given value.
|
|
1343
|
+
|
|
1344
|
+
```javascript
|
|
1345
|
+
import { start } from "@tolki/str";
|
|
1346
|
+
|
|
1347
|
+
const result = start("this/string", "/");
|
|
1348
|
+
|
|
1349
|
+
// result is "/this/string"
|
|
1350
|
+
|
|
1351
|
+
const result2 = start("/this/string", "/");
|
|
1352
|
+
|
|
1353
|
+
// result2 is also '/this/string'
|
|
1354
|
+
```
|
|
1355
|
+
|
|
1356
|
+
#### startsWith
|
|
1357
|
+
|
|
1358
|
+
Determine if a given string starts with a given substring.
|
|
1359
|
+
|
|
1360
|
+
```javascript
|
|
1361
|
+
import { startsWith } from "@tolki/str";
|
|
1362
|
+
|
|
1363
|
+
const result = startsWith("This is my name", "This");
|
|
1364
|
+
|
|
1365
|
+
// result is true
|
|
1366
|
+
```
|
|
1367
|
+
|
|
1368
|
+
The second argument may also be an array of strings to check against. If the string starts with any of the substrings, the function will return true.
|
|
1369
|
+
|
|
1370
|
+
```javascript
|
|
1371
|
+
import { startsWith } from "@tolki/str";
|
|
1372
|
+
|
|
1373
|
+
const result = startsWith("This is my name", ["This", "That", "There"]);
|
|
1374
|
+
|
|
1375
|
+
// result is true
|
|
1376
|
+
```
|
|
1377
|
+
|
|
1378
|
+
#### stripTags
|
|
1379
|
+
|
|
1380
|
+
Strip HTML tags from a string.
|
|
1381
|
+
|
|
1382
|
+
```javascript
|
|
1383
|
+
import { stripTags } from "@tolki/str";
|
|
1384
|
+
|
|
1385
|
+
const result = stripTags("<p>Hello <strong>World</strong></p>");
|
|
1386
|
+
|
|
1387
|
+
// result is "Hello World"
|
|
1388
|
+
```
|
|
1389
|
+
|
|
1390
|
+
#### studly
|
|
1391
|
+
|
|
1392
|
+
Convert a value to studly caps case.
|
|
1393
|
+
|
|
1394
|
+
```javascript
|
|
1395
|
+
import { studly } from "@tolki/str";
|
|
1396
|
+
|
|
1397
|
+
const result = studly("foo_bar");
|
|
1398
|
+
|
|
1399
|
+
// result is "FooBar"
|
|
1400
|
+
```
|
|
1401
|
+
|
|
1402
|
+
#### substr
|
|
1403
|
+
|
|
1404
|
+
Returns the portion of the string specified by the start and length parameters.
|
|
1405
|
+
|
|
1406
|
+
```javascript
|
|
1407
|
+
import { substr } from "@tolki/str";
|
|
1408
|
+
|
|
1409
|
+
const result = substr("The Laravel Framework", 4, 7);
|
|
1410
|
+
|
|
1411
|
+
// result is "Laravel"
|
|
1412
|
+
```
|
|
1413
|
+
|
|
1414
|
+
#### substrCount
|
|
1415
|
+
|
|
1416
|
+
Returns the number of substring occurrences.
|
|
1417
|
+
|
|
1418
|
+
```javascript
|
|
1419
|
+
import { substrCount } from "@tolki/str";
|
|
1420
|
+
|
|
1421
|
+
const result = substrCount(
|
|
1422
|
+
"If you like ice cream, you will like snow cones.",
|
|
1423
|
+
"like",
|
|
1424
|
+
);
|
|
1425
|
+
|
|
1426
|
+
// result is 2
|
|
1427
|
+
```
|
|
1428
|
+
|
|
1429
|
+
#### substrReplace
|
|
1430
|
+
|
|
1431
|
+
Replace text within a portion of a string.
|
|
1432
|
+
|
|
1433
|
+
```javascript
|
|
1434
|
+
import { substrReplace } from "@tolki/str";
|
|
1435
|
+
|
|
1436
|
+
const result = substrReplace("1300", ":", 2);
|
|
1437
|
+
|
|
1438
|
+
// result is "13"
|
|
1439
|
+
|
|
1440
|
+
const result2 = substrReplace("1300", ":", 2, 0);
|
|
1441
|
+
|
|
1442
|
+
// result2 is also "13:00"
|
|
1443
|
+
```
|
|
1444
|
+
|
|
1445
|
+
#### swap
|
|
1446
|
+
|
|
1447
|
+
Swap multiple keywords in a string with other keywords.
|
|
1448
|
+
|
|
1449
|
+
```javascript
|
|
1450
|
+
import { swap } from "@tolki/str";
|
|
1451
|
+
|
|
1452
|
+
const result = swap(
|
|
1453
|
+
{
|
|
1454
|
+
Tacos: "Burritos",
|
|
1455
|
+
great: "fantastic",
|
|
1456
|
+
},
|
|
1457
|
+
"Tacos are great!",
|
|
1458
|
+
);
|
|
1459
|
+
|
|
1460
|
+
// result is "Burritos are fantastic"
|
|
1461
|
+
```
|
|
1462
|
+
|
|
1463
|
+
#### take
|
|
1464
|
+
|
|
1465
|
+
Take the first or last {$limit} characters of a string.
|
|
1466
|
+
|
|
1467
|
+
```javascript
|
|
1468
|
+
import { take } from "@tolki/str";
|
|
1469
|
+
|
|
1470
|
+
const result = take("Build something amazing!", 5);
|
|
1471
|
+
|
|
1472
|
+
// result is "Build"
|
|
1473
|
+
|
|
1474
|
+
const result2 = take("Build something amazing!", -5);
|
|
1475
|
+
|
|
1476
|
+
// result2 is "zing!"
|
|
1477
|
+
```
|
|
1478
|
+
|
|
1479
|
+
#### title
|
|
1480
|
+
|
|
1481
|
+
Convert the given string to proper case.
|
|
1482
|
+
|
|
1483
|
+
```javascript
|
|
1484
|
+
import { title } from "@tolki/str";
|
|
1485
|
+
|
|
1486
|
+
const result = title("a nice title uses the correct case");
|
|
1487
|
+
|
|
1488
|
+
// result is "A Nice Title Uses The Correct Case"
|
|
1489
|
+
```
|
|
1490
|
+
|
|
1491
|
+
#### toBase64
|
|
1492
|
+
|
|
1493
|
+
Convert the given string to Base64 encoding.
|
|
1494
|
+
|
|
1495
|
+
```javascript
|
|
1496
|
+
import { toBase64 } from "@tolki/str";
|
|
1497
|
+
|
|
1498
|
+
const result = toBase64("Laravel");
|
|
1499
|
+
|
|
1500
|
+
// result is "TGFyYXZlbA=="
|
|
1501
|
+
```
|
|
1502
|
+
|
|
1503
|
+
#### transliterate
|
|
1504
|
+
|
|
1505
|
+
Transliterate a string to its closest ASCII representation.
|
|
1506
|
+
|
|
1507
|
+
Uses the [`any-ascii`](https://www.npmjs.com/package/any-ascii) package.
|
|
1508
|
+
|
|
1509
|
+
```javascript
|
|
1510
|
+
import { transliterate } from "@tolki/str";
|
|
1511
|
+
|
|
1512
|
+
const result = transliterate("Æneid");
|
|
1513
|
+
|
|
1514
|
+
// result is "Aeneid"
|
|
1515
|
+
|
|
1516
|
+
const result2 = transliterate("ⓣⓔⓢⓣ@ⓛⓐⓡⓐⓥⓔⓛ.ⓒⓞⓜ");
|
|
1517
|
+
|
|
1518
|
+
// result2 is "test@laravel.com"
|
|
1519
|
+
```
|
|
1520
|
+
|
|
1521
|
+
#### trim
|
|
1522
|
+
|
|
1523
|
+
Remove all whitespace from both ends of a string.
|
|
1524
|
+
|
|
1525
|
+
```javascript
|
|
1526
|
+
import { trim } from "@tolki/str";
|
|
1527
|
+
|
|
1528
|
+
const result = trim(" foo bar ");
|
|
1529
|
+
|
|
1530
|
+
// result is "foo bar"
|
|
1531
|
+
```
|
|
1532
|
+
|
|
1533
|
+
#### ltrim
|
|
1534
|
+
|
|
1535
|
+
Remove all whitespace from the beginning of a string.
|
|
1536
|
+
|
|
1537
|
+
```javascript
|
|
1538
|
+
import { ltrim } from "@tolki/str";
|
|
1539
|
+
|
|
1540
|
+
const result = ltrim(" foo bar ");
|
|
1541
|
+
|
|
1542
|
+
// result is "foo bar "
|
|
1543
|
+
```
|
|
1544
|
+
|
|
1545
|
+
#### rtrim
|
|
1546
|
+
|
|
1547
|
+
Remove all whitespace from the end of a string.
|
|
1548
|
+
|
|
1549
|
+
```javascript
|
|
1550
|
+
import { rtrim } from "@tolki/str";
|
|
1551
|
+
|
|
1552
|
+
const result = rtrim(" foo bar ");
|
|
1553
|
+
|
|
1554
|
+
// result is " foo bar"
|
|
1555
|
+
```
|
|
1556
|
+
|
|
1557
|
+
#### ucfirst
|
|
1558
|
+
|
|
1559
|
+
Make a string's first character uppercase.
|
|
1560
|
+
|
|
1561
|
+
```javascript
|
|
1562
|
+
import { ucfirst } from "@tolki/str";
|
|
1563
|
+
|
|
1564
|
+
const result = ucfirst("foo bar");
|
|
1565
|
+
|
|
1566
|
+
// result is "Foo bar"
|
|
1567
|
+
```
|
|
1568
|
+
|
|
1569
|
+
#### ucsplit
|
|
1570
|
+
|
|
1571
|
+
Split a string into pieces by uppercase characters.
|
|
1572
|
+
|
|
1573
|
+
```javascript
|
|
1574
|
+
import { ucsplit } from "@tolki/str";
|
|
1575
|
+
|
|
1576
|
+
const result = ucsplit("FooBar");
|
|
1577
|
+
|
|
1578
|
+
// result is ["Foo", "Bar"]
|
|
1579
|
+
```
|
|
1580
|
+
|
|
1581
|
+
#### ucwords
|
|
1582
|
+
|
|
1583
|
+
Uppercase the first letter of each word in a string.
|
|
1584
|
+
|
|
1585
|
+
```javascript
|
|
1586
|
+
import { ucwords } from "@tolki/str";
|
|
1587
|
+
|
|
1588
|
+
const result = ucwords("laravel framework");
|
|
1589
|
+
|
|
1590
|
+
// result is "Laravel Framework"
|
|
1591
|
+
```
|
|
1592
|
+
|
|
1593
|
+
#### upper
|
|
1594
|
+
|
|
1595
|
+
Convert the given string to upper-case.
|
|
1596
|
+
|
|
1597
|
+
```javascript
|
|
1598
|
+
import { upper } from "@tolki/str";
|
|
1599
|
+
|
|
1600
|
+
const result = upper("laravel");
|
|
1601
|
+
|
|
1602
|
+
// result is "LARAVEL"
|
|
1603
|
+
```
|
|
1604
|
+
|
|
1605
|
+
#### ulid
|
|
1606
|
+
|
|
1607
|
+
Generate a ULID (Universally Unique Lexicographically Sortable Identifier).
|
|
1608
|
+
|
|
1609
|
+
Uses the [`ulid`](https://www.npmjs.com/package/ulid) package.
|
|
1610
|
+
|
|
1611
|
+
```javascript
|
|
1612
|
+
import { ulid } from "@tolki/str";
|
|
1613
|
+
|
|
1614
|
+
const result = ulid();
|
|
1615
|
+
|
|
1616
|
+
// result is "01F8MECHZX2D7J8F8C8D4B8F8C"
|
|
1617
|
+
```
|
|
1618
|
+
|
|
1619
|
+
During testing, you can use the `createUlidsUsing` function to provide a custom ULID generator for predictable results.
|
|
1620
|
+
|
|
1621
|
+
```javascript
|
|
1622
|
+
import { createUlidsUsing, ulid } from "@tolki/str";
|
|
1623
|
+
|
|
1624
|
+
createUlidsUsing(() => "custom-ulid");
|
|
1625
|
+
|
|
1626
|
+
const result = ulid();
|
|
1627
|
+
|
|
1628
|
+
// result is "custom-ulid"
|
|
1629
|
+
```
|
|
1630
|
+
|
|
1631
|
+
You can reset to the default ULID generator by calling the `createUlidsNormally` function.
|
|
1632
|
+
|
|
1633
|
+
```javascript
|
|
1634
|
+
import { createUlidsUsing, createUlidsNormally, ulid } from "@tolki/str";
|
|
1635
|
+
|
|
1636
|
+
createUlidsUsing(() => "custom-ulid");
|
|
1637
|
+
|
|
1638
|
+
const result = ulid();
|
|
1639
|
+
|
|
1640
|
+
// result is "custom-ulid"
|
|
1641
|
+
|
|
1642
|
+
createUlidsNormally();
|
|
1643
|
+
|
|
1644
|
+
const result2 = ulid();
|
|
1645
|
+
|
|
1646
|
+
// result is a randomly generated ULID
|
|
1647
|
+
```
|
|
1648
|
+
|
|
1649
|
+
#### unwrap
|
|
1650
|
+
|
|
1651
|
+
Unwrap the string with the given strings.
|
|
1652
|
+
|
|
1653
|
+
```javascript
|
|
1654
|
+
import { unwrap } from "@tolki/str";
|
|
1655
|
+
|
|
1656
|
+
const result = unwrap("-Laravel-", "-");
|
|
1657
|
+
|
|
1658
|
+
// result is "Laravel"
|
|
1659
|
+
|
|
1660
|
+
const result2 = unwrap('{framework: "Laravel"}', "{", "}");
|
|
1661
|
+
|
|
1662
|
+
// result2 is 'framework: "Laravel"'
|
|
1663
|
+
```
|
|
1664
|
+
|
|
1665
|
+
#### uuid
|
|
1666
|
+
|
|
1667
|
+
Generate a UUID (version 4).
|
|
1668
|
+
|
|
1669
|
+
Uses the [`uuid`](https://www.npmjs.com/package/uuid) package.
|
|
1670
|
+
|
|
1671
|
+
```javascript
|
|
1672
|
+
import { uuid } from "@tolki/str";
|
|
1673
|
+
|
|
1674
|
+
const result = uuid();
|
|
1675
|
+
|
|
1676
|
+
// result is a randomly generated UUID (version 4)
|
|
1677
|
+
```
|
|
1678
|
+
|
|
1679
|
+
During testing, you can use the `createUuidsUsing` function to provide a custom UUID generator for predictable results.
|
|
1680
|
+
|
|
1681
|
+
```javascript
|
|
1682
|
+
import { createUuidsUsing, uuid } from "@tolki/str";
|
|
1683
|
+
|
|
1684
|
+
createUuidsUsing(() => "custom-uuid");
|
|
1685
|
+
|
|
1686
|
+
const result = uuid();
|
|
1687
|
+
|
|
1688
|
+
// result is "custom-uuid"
|
|
1689
|
+
```
|
|
1690
|
+
|
|
1691
|
+
You can reset to the default UUID generator by calling the `createUuidsNormally` function.
|
|
1692
|
+
|
|
1693
|
+
```javascript
|
|
1694
|
+
import { createUuidsUsing, createUuidsNormally, uuid } from "@tolki/str";
|
|
1695
|
+
|
|
1696
|
+
createUuidsUsing(() => "custom-uuid");
|
|
1697
|
+
|
|
1698
|
+
const result = uuid();
|
|
1699
|
+
|
|
1700
|
+
// result is "custom-uuid"
|
|
1701
|
+
|
|
1702
|
+
createUuidsNormally();
|
|
1703
|
+
|
|
1704
|
+
const result2 = uuid();
|
|
1705
|
+
|
|
1706
|
+
// result is a randomly generated UUID
|
|
1707
|
+
```
|
|
1708
|
+
|
|
1709
|
+
#### uuid7
|
|
1710
|
+
|
|
1711
|
+
Generate a UUID (version 7).
|
|
1712
|
+
|
|
1713
|
+
Uses the [`uuid`](https://www.npmjs.com/package/uuid) package.
|
|
1714
|
+
|
|
1715
|
+
```javascript
|
|
1716
|
+
import { uuid7 } from "@tolki/str";
|
|
1717
|
+
|
|
1718
|
+
const result = uuid7();
|
|
1719
|
+
|
|
1720
|
+
// result is a randomly generated UUID (version 7)
|
|
1721
|
+
```
|
|
1722
|
+
|
|
1723
|
+
The `uuid7()` function also uses the `createUuidsUsing` and `createUuidsNormally` functions for testing purposes, as described in the `uuid` function above.
|
|
1724
|
+
|
|
1725
|
+
#### wordCount
|
|
1726
|
+
|
|
1727
|
+
Get the number of words a string contains.
|
|
1728
|
+
|
|
1729
|
+
```javascript
|
|
1730
|
+
import { wordCount } from "@tolki/str";
|
|
1731
|
+
|
|
1732
|
+
const result = wordCount("Hello, world!");
|
|
1733
|
+
|
|
1734
|
+
// result is 2
|
|
1735
|
+
```
|
|
1736
|
+
|
|
1737
|
+
#### wordWrap
|
|
1738
|
+
|
|
1739
|
+
Wrap a string to a given number of characters.
|
|
1740
|
+
|
|
1741
|
+
```javascript
|
|
1742
|
+
import { wordWrap } from "@tolki/str";
|
|
1743
|
+
|
|
1744
|
+
const result = wordWrap(
|
|
1745
|
+
"The quick brown fox jumped over the lazy dog.",
|
|
1746
|
+
20,
|
|
1747
|
+
"<br />\n",
|
|
1748
|
+
);
|
|
1749
|
+
|
|
1750
|
+
// result is:
|
|
1751
|
+
/*
|
|
1752
|
+
The quick brown fox<br />
|
|
1753
|
+
jumped over the lazy<br />
|
|
1754
|
+
dog.
|
|
1755
|
+
*/
|
|
1756
|
+
```
|
|
1757
|
+
|
|
1758
|
+
#### words
|
|
1759
|
+
|
|
1760
|
+
Limit the number of words in a string.
|
|
1761
|
+
|
|
1762
|
+
```javascript
|
|
1763
|
+
import { words } from "@tolki/str";
|
|
1764
|
+
|
|
1765
|
+
const result = words("Perfectly balanced, as all things should be.", 3, " >>>");
|
|
1766
|
+
|
|
1767
|
+
// result is "Perfectly balanced, as >>>"
|
|
1768
|
+
```
|
|
1769
|
+
|
|
1770
|
+
#### wrap
|
|
1771
|
+
|
|
1772
|
+
Wrap the string with the given strings.
|
|
1773
|
+
|
|
1774
|
+
```javascript
|
|
1775
|
+
import { wrap } from "@tolki/str";
|
|
1776
|
+
|
|
1777
|
+
const result = wrap("Laravel", '"');
|
|
1778
|
+
|
|
1779
|
+
// result is '"Laravel"'
|
|
1780
|
+
|
|
1781
|
+
const result2 = wrap("is", "This ", " Laravel!");
|
|
1782
|
+
|
|
1783
|
+
// result2 is 'This is Laravel!'
|
|
1784
|
+
```
|
|
1785
|
+
|
|
1786
|
+
#### str
|
|
1787
|
+
|
|
1788
|
+
Using the `str` or `of` functions is discouraged for frontend projects because it will import the entire `Stringable` class, all of its methods, and all 3rd party dependencies into your final bundle, which may significantly increase its size.
|
|
1789
|
+
|
|
1790
|
+
For frontend projects, it is recommended to use the individual string functions instead for better tree-shaking and smaller bundle sizes.
|
|
1791
|
+
|
|
1792
|
+
Get a new Stringable object from the given string.
|
|
1793
|
+
|
|
1794
|
+
```javascript
|
|
1795
|
+
import { str } from "@tolki/str";
|
|
1796
|
+
|
|
1797
|
+
const result = str("Laravel").append(" Otwell");
|
|
1798
|
+
|
|
1799
|
+
// result is a Stringable class instance representing "Laravel Otwell"
|
|
1800
|
+
```
|
|
1801
|
+
|
|
1802
|
+
If no string is provided, a Stringable class instance with an empty string will be returned.
|
|
1803
|
+
|
|
1804
|
+
```javascript
|
|
1805
|
+
import { str } from "@tolki/str";
|
|
1806
|
+
|
|
1807
|
+
const result = str().snake("FooBar");
|
|
1808
|
+
|
|
1809
|
+
// result is a Stringable class instance representing "foo_bar"
|
|
1810
|
+
```
|
|
1811
|
+
|
|
1812
|
+
#### of
|
|
1813
|
+
|
|
1814
|
+
The `of` function is an alias for the `str` function made for parity with Laravels' `Str::of` method. See the [str](#str) function documentation for details.
|
|
1815
|
+
|
|
1816
|
+
```javascript
|
|
1817
|
+
import { of } from "@tolki/str";
|
|
1818
|
+
|
|
1819
|
+
const result = of("Laravel").upper();
|
|
1820
|
+
|
|
1821
|
+
// result is a Stringable class instance representing "LARAVEL"
|
|
1822
|
+
```
|
|
1823
|
+
|
|
1824
|
+
## Tolki Stringable Utilities List
|
|
1825
|
+
|
|
1826
|
+
### Stringable utilities list
|
|
1827
|
+
|
|
1828
|
+
The documentation for fluent string utilities is in the works. In the meantime, I recommend checking out the Laravel documentation for [Fluent strings](https://laravel.com/docs/12.x/strings#fluent-strings-method-list) since the JavaScript utilities are designed to mirror the Laravel ones as closely as possible.
|
|
1829
|
+
|
|
1830
|
+
You're also welcomed to checkout the [source code](https://github.com/abetwothree/tolki/blob/master/packages/str/src/stringable/index.ts) for the list of available utilities.
|
|
1831
|
+
|
|
1832
|
+
<!-- AUTO-GENERATED-DOCS:END -->
|