npm-lodash

Description
A modern JavaScript utility library delivering modularity, performance & extras
Author
tessl
Last updated

How to use

npx @tessl/cli registry install tessl/npm-lodash@4.6.0

string.md docs/

1
# String Processing
2
3
String manipulation including case conversion, trimming, padding, and template utilities. These methods provide comprehensive string processing capabilities.
4
5
## Capabilities
6
7
### Case Conversion
8
9
Convert strings between different case formats.
10
11
```javascript { .api }
12
/**
13
* Converts `string` to camel case.
14
*
15
* @param {string} [string=''] The string to convert
16
* @returns {string} Returns the camel cased string
17
*/
18
function camelCase(string?: string): string;
19
20
/**
21
* Converts `string` to kebab case.
22
*
23
* @param {string} [string=''] The string to convert
24
* @returns {string} Returns the kebab cased string
25
*/
26
function kebabCase(string?: string): string;
27
28
/**
29
* Converts `string` to snake case.
30
*
31
* @param {string} [string=''] The string to convert
32
* @returns {string} Returns the snake cased string
33
*/
34
function snakeCase(string?: string): string;
35
36
/**
37
* Converts `string` to start case.
38
*
39
* @param {string} [string=''] The string to convert
40
* @returns {string} Returns the start cased string
41
*/
42
function startCase(string?: string): string;
43
44
/**
45
* Converts the first character of `string` to upper case and the remaining to lower case.
46
*
47
* @param {string} [string=''] The string to capitalize
48
* @returns {string} Returns the capitalized string
49
*/
50
function capitalize(string?: string): string;
51
52
/**
53
* Converts the first character of `string` to lower case.
54
*
55
* @param {string} [string=''] The string to convert
56
* @returns {string} Returns the converted string
57
*/
58
function lowerFirst(string?: string): string;
59
60
/**
61
* Converts the first character of `string` to upper case.
62
*
63
* @param {string} [string=''] The string to convert
64
* @returns {string} Returns the converted string
65
*/
66
function upperFirst(string?: string): string;
67
```
68
69
**Usage Examples:**
70
71
```javascript
72
import { camelCase, kebabCase, snakeCase, startCase, capitalize } from "lodash";
73
74
camelCase('hello world'); // 'helloWorld'
75
camelCase('--foo-bar--'); // 'fooBar'
76
camelCase('__FOO_BAR__'); // 'fooBar'
77
78
kebabCase('helloWorld'); // 'hello-world'
79
kebabCase('hello world'); // 'hello-world'
80
81
snakeCase('helloWorld'); // 'hello_world'
82
snakeCase('hello world'); // 'hello_world'
83
84
startCase('helloWorld'); // 'Hello World'
85
startCase('--foo-bar--'); // 'Foo Bar'
86
87
capitalize('HELLO'); // 'Hello'
88
capitalize('hello'); // 'Hello'
89
```
90
91
### Case Transformation
92
93
Transform entire strings to upper or lower case.
94
95
```javascript { .api }
96
/**
97
* Converts `string`, as space separated words, to lower case.
98
*
99
* @param {string} [string=''] The string to convert
100
* @returns {string} Returns the lower cased string
101
*/
102
function lowerCase(string?: string): string;
103
104
/**
105
* Converts `string`, as space separated words, to upper case.
106
*
107
* @param {string} [string=''] The string to convert
108
* @returns {string} Returns the upper cased string
109
*/
110
function upperCase(string?: string): string;
111
112
/**
113
* Converts `string`, as a whole, to lower case.
114
*
115
* @param {string} [string=''] The string to convert
116
* @returns {string} Returns the lower cased string
117
*/
118
function toLower(string?: string): string;
119
120
/**
121
* Converts `string`, as a whole, to upper case.
122
*
123
* @param {string} [string=''] The string to convert
124
* @returns {string} Returns the upper cased string
125
*/
126
function toUpper(string?: string): string;
127
```
128
129
### String Padding
130
131
Add padding to strings.
132
133
```javascript { .api }
134
/**
135
* Pads `string` on the left and right sides if it's shorter than `length`.
136
* Padding characters are truncated if they can't be evenly divided by `length`.
137
*
138
* @param {string} [string=''] The string to pad
139
* @param {number} [length=0] The padding length
140
* @param {string} [chars=' '] The string used as padding
141
* @returns {string} Returns the padded string
142
*/
143
function pad(string?: string, length?: number, chars?: string): string;
144
145
/**
146
* Pads `string` on the right side if it's shorter than `length`.
147
*
148
* @param {string} [string=''] The string to pad
149
* @param {number} [length=0] The padding length
150
* @param {string} [chars=' '] The string used as padding
151
* @returns {string} Returns the padded string
152
*/
153
function padEnd(string?: string, length?: number, chars?: string): string;
154
155
/**
156
* Pads `string` on the left side if it's shorter than `length`.
157
*
158
* @param {string} [string=''] The string to pad
159
* @param {number} [length=0] The padding length
160
* @param {string} [chars=' '] The string used as padding
161
* @returns {string} Returns the padded string
162
*/
163
function padStart(string?: string, length?: number, chars?: string): string;
164
```
165
166
**Usage Examples:**
167
168
```javascript
169
import { pad, padEnd, padStart } from "lodash";
170
171
pad('abc', 8); // ' abc '
172
pad('abc', 8, '_-'); // '_-abc_-_'
173
174
padEnd('abc', 6); // 'abc '
175
padEnd('abc', 6, '_-'); // 'abc_-_'
176
177
padStart('abc', 6); // ' abc'
178
padStart('abc', 6, '_-'); // '_-_abc'
179
```
180
181
### String Trimming
182
183
Remove whitespace or specified characters from strings.
184
185
```javascript { .api }
186
/**
187
* Removes leading and trailing whitespace or specified characters from `string`.
188
*
189
* @param {string} [string=''] The string to trim
190
* @param {string} [chars=whitespace] The characters to trim
191
* @returns {string} Returns the trimmed string
192
*/
193
function trim(string?: string, chars?: string): string;
194
195
/**
196
* Removes trailing whitespace or specified characters from `string`.
197
*
198
* @param {string} [string=''] The string to trim
199
* @param {string} [chars=whitespace] The characters to trim
200
* @returns {string} Returns the trimmed string
201
*/
202
function trimEnd(string?: string, chars?: string): string;
203
204
/**
205
* Removes leading whitespace or specified characters from `string`.
206
*
207
* @param {string} [string=''] The string to trim
208
* @param {string} [chars=whitespace] The characters to trim
209
* @returns {string} Returns the trimmed string
210
*/
211
function trimStart(string?: string, chars?: string): string;
212
```
213
214
### String Testing
215
216
Test strings for specific patterns.
217
218
```javascript { .api }
219
/**
220
* Checks if `string` starts with the given target string.
221
*
222
* @param {string} [string=''] The string to inspect
223
* @param {string} [target] The string to search for
224
* @param {number} [position=0] The position to search from
225
* @returns {boolean} Returns `true` if `string` starts with `target`, else `false`
226
*/
227
function startsWith(string?: string, target?: string, position?: number): boolean;
228
229
/**
230
* Checks if `string` ends with the given target string.
231
*
232
* @param {string} [string=''] The string to inspect
233
* @param {string} [target] The string to search for
234
* @param {number} [position=string.length] The position to search up to
235
* @returns {boolean} Returns `true` if `string` ends with `target`, else `false`
236
*/
237
function endsWith(string?: string, target?: string, position?: number): boolean;
238
```
239
240
### String Repetition
241
242
Repeat strings multiple times.
243
244
```javascript { .api }
245
/**
246
* Repeats the given string `n` times.
247
*
248
* @param {string} [string=''] The string to repeat
249
* @param {number} [n=1] The number of times to repeat the string
250
* @returns {string} Returns the repeated string
251
*/
252
function repeat(string?: string, n?: number): string;
253
```
254
255
### String Replacement
256
257
Replace parts of strings.
258
259
```javascript { .api }
260
/**
261
* Replaces matches for `pattern` in `string` with `replacement`.
262
*
263
* @param {string} [string=''] The string to modify
264
* @param {RegExp|string} pattern The pattern to replace
265
* @param {Function|string} replacement The match replacement
266
* @returns {string} Returns the modified string
267
*/
268
function replace(string?: string, pattern: RegExp|string, replacement: Function|string): string;
269
```
270
271
### String Truncation
272
273
Truncate strings to specified lengths.
274
275
```javascript { .api }
276
/**
277
* Truncates `string` if it's longer than the given maximum string length.
278
* The last characters of the truncated string are replaced with the omission string.
279
*
280
* @param {string} [string=''] The string to truncate
281
* @param {Object} [options] The options object
282
* @returns {string} Returns the truncated string
283
*/
284
function truncate(string?: string, options?: Object): string;
285
```
286
287
**Usage Examples:**
288
289
```javascript
290
import { truncate } from "lodash";
291
292
truncate('hi-diddly-ho there, neighborino');
293
// 'hi-diddly-ho there, neighbo...'
294
295
truncate('hi-diddly-ho there, neighborino', {
296
'length': 24,
297
'separator': ' '
298
});
299
// 'hi-diddly-ho there,...'
300
301
truncate('hi-diddly-ho there, neighborino', {
302
'length': 24,
303
'separator': /,? +/
304
});
305
// 'hi-diddly-ho there...'
306
307
truncate('hi-diddly-ho there, neighborino', {
308
'omission': ' [...]'
309
});
310
// 'hi-diddly-ho there, neig [...]'
311
```
312
313
### String Splitting
314
315
Split strings into arrays.
316
317
```javascript { .api }
318
/**
319
* Splits `string` by `separator`.
320
*
321
* @param {string} [string=''] The string to split
322
* @param {RegExp|string} separator The separator pattern to split by
323
* @param {number} [limit] The length to truncate results to
324
* @returns {Array} Returns the string segments
325
*/
326
function split(string?: string, separator: RegExp|string, limit?: number): Array;
327
328
/**
329
* Splits `string` into an array of its words.
330
*
331
* @param {string} [string=''] The string to inspect
332
* @param {RegExp|string} [pattern] The pattern to match words
333
* @returns {Array} Returns the words of `string`
334
*/
335
function words(string?: string, pattern?: RegExp|string): Array;
336
```
337
338
### String Encoding
339
340
Encode and decode strings for safety.
341
342
```javascript { .api }
343
/**
344
* Converts the characters "&", "<", ">", '"', and "'" in `string` to their
345
* corresponding HTML entities.
346
*
347
* @param {string} [string=''] The string to escape
348
* @returns {string} Returns the escaped string
349
*/
350
function escape(string?: string): string;
351
352
/**
353
* The inverse of `escape`; this method converts the HTML entities
354
* `&amp;`, `&lt;`, `&gt;`, `&quot;`, and `&#39;` in `string` to
355
* their corresponding characters.
356
*
357
* @param {string} [string=''] The string to unescape
358
* @returns {string} Returns the unescaped string
359
*/
360
function unescape(string?: string): string;
361
362
/**
363
* Escapes the `RegExp` special characters "^", "$", "\", ".", "*", "+",
364
* "?", "(", ")", "[", "]", "{", "}", and "|" in `string`.
365
*
366
* @param {string} [string=''] The string to escape
367
* @returns {string} Returns the escaped string
368
*/
369
function escapeRegExp(string?: string): string;
370
```
371
372
### String Cleaning
373
374
Clean and normalize strings.
375
376
```javascript { .api }
377
/**
378
* Deburrs `string` by converting Latin-1 Supplement and Latin Extended-A
379
* letters to basic Latin letters and removing combining diacritical marks.
380
*
381
* @param {string} [string=''] The string to deburr
382
* @returns {string} Returns the deburred string
383
*/
384
function deburr(string?: string): string;
385
```
386
387
**Usage Examples:**
388
389
```javascript
390
import { deburr, escape, unescape } from "lodash";
391
392
deburr('déjà vu'); // 'deja vu'
393
394
escape('fred, barney, & pebbles');
395
// 'fred, barney, &amp; pebbles'
396
397
unescape('fred, barney, &amp; pebbles');
398
// 'fred, barney, & pebbles'
399
```
400
401
### String Parsing
402
403
Parse strings into other types.
404
405
```javascript { .api }
406
/**
407
* Converts `string` to an integer of the specified radix. If `radix` is
408
* `undefined` or `0`, a `radix` of `10` is used unless `value` is a
409
* hexadecimal, in which case a `radix` of `16` is used.
410
*
411
* @param {string} string The string to convert
412
* @param {number} [radix=10] The radix to interpret `value` by
413
* @returns {number} Returns the converted integer
414
*/
415
function parseInt(string: string, radix?: number): number;
416
```
417
418
### Template Processing
419
420
Create and process string templates.
421
422
```javascript { .api }
423
/**
424
* Creates a compiled template function that can interpolate data properties
425
* in "interpolate" delimiters, HTML-escape interpolated data properties in
426
* "escape" delimiters, and execute JavaScript in "evaluate" delimiters.
427
*
428
* @param {string} [string=''] The template string
429
* @param {Object} [options] The options object
430
* @returns {Function} Returns the compiled template function
431
*/
432
function template(string?: string, options?: Object): Function;
433
```
434
435
**Usage Examples:**
436
437
```javascript
438
import { template } from "lodash";
439
440
// Basic interpolation
441
const compiled = template('hello <%= user %>!');
442
compiled({ 'user': 'fred' }); // 'hello fred!'
443
444
// Using "escape" delimiter to escape data
445
const compiled2 = template('<b><%- value %></b>');
446
compiled2({ 'value': '<script>' }); // '<b>&lt;script&gt;</b>'
447
448
// Using "evaluate" delimiter to execute JavaScript
449
const compiled3 = template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
450
compiled3({ 'users': ['fred', 'barney'] }); // '<li>fred</li><li>barney</li>'
451
452
// Custom template settings
453
template.templateSettings = {
454
'interpolate': /{{([\s\S]+?)}}/g
455
};
456
457
const compiled4 = template('hello {{ user }}!');
458
compiled4({ 'user': 'mustache' }); // 'hello mustache!'
459
```