forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
underscore.string.d.ts
573 lines (504 loc) · 15.5 KB
/
underscore.string.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Type definitions for underscore.string
// Project: https://github.com/epeli/underscore.string
// Definitions by: Ry Racherbaumer <http://github.com/rygine>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../underscore/underscore.d.ts" />
interface UnderscoreStatic {
str: UnderscoreStringStatic;
string: UnderscoreStringStatic;
}
declare var s : UnderscoreStringStatic;
interface UnderscoreStringStatic extends UnderscoreStringStaticExports {
/**
* Tests if string contains a substring.
* ('foobar', 'ob') => true
* @param str
* @param needle
*/
include(str: string, needle: string): boolean;
/**
* Tests if string contains a substring.
* ('foobar', 'ob') => true
* @param str
* @param needle
*/
contains(str: string, needle: string): boolean;
/**
* Return reversed string.
* ('foobar') => 'raboof'
* @param str
*/
reverse(str: string): string;
}
/**
* Functions exported for mixing with underscore object.
*
* Usage:
* _.mixin(_.string.exports());
* interface UnderscoreStatic extends UnderscoreStringStaticExports { }
*/
interface UnderscoreStringStaticExports {
exports(): UnderscoreStringStaticExports;
/**
* Determine if a string is 'blank.'
* @param str
*/
isBlank(str: string): boolean;
/**
* Removes all html tags from string.
* @param str
*/
stripTags(str: string): string;
/**
* Converts first letter of the string to uppercase.
* ('foo Bar') => 'Foo Bar'
* @param str
*/
capitalize(str: string): string;
/**
* Chop a string into pieces.
* ('whitespace', 3) => ['whi','tes','pac','e']
* @param str String to chop
* @param step Size of the pieces
*/
chop(str: string, step: number): any[];
/**
* Compress some whitespaces to one.
* (' foo bar ') => 'foo bar'
* @param str
*/
clean(str: string): string;
/**
* Count occurences of a sub string.
* ('Hello world', 'l') => 3
* @param str
* @param substr
*/
count(str: string, substr: string): number;
/**
* Convert string to an array of characters.
* ('Hello') => ['H','e','l','l','o']
* @param str
*/
chars(str: string): any[];
/**
* Returns a copy of the string in which all the case-based characters have had their case swapped.
* ('hELLO') => 'Hello'
* @param str
*/
swapCase(str: string): string;
/**
* Converts HTML special characters to their entity equivalents.
* ('<div>Blah blah blah</div>') => '<div>Blah blah blah</div>'
* @param str
*/
escapeHTML(str: string): string;
/**
* Converts entity characters to HTML equivalents.
* ('<div>Blah blah blah</div>') => '<div>Blah blah blah</div>'
* @param str
*/
unescapeHTML(str: string): string;
/**
* Escape a string for use in a regular expression.
* @param str
*/
escapeRegExp(str: string): string;
/**
* Splice a string like an array.
* @param str
* @param i
* @param howmany
* @param substr
*/
splice(str: string, i: number, howmany: number, substr?: string): string;
/**
* Insert a string at index.
* @param str
* @param i
* @param substr
*/
insert(str: string, i: number, substr: string): string;
/**
* Joins strings together with given separator.
* (' ', 'foo', 'bar') => 'foo bar'
* @param separator
* @param args
*/
join(separator: string, ...args: string[]): string;
/**
* Split string by newlines character.
* ('Hello\nWorld') => ['Hello', 'World']
* @param str
*/
lines(str: string): any[];
/**
* Checks if string starts with another string.
* ('image.gif', 'image') => true
* @param str
* @param starts
*/
startsWith(str: string, starts: string): boolean;
/**
* Checks if string ends with another string.
* ('image.gif', 'gif') => true
* @param value
* @param starts
*/
endsWith(value: string, starts: string): boolean;
/**
* Returns the successor to passed string.
* ('a') => 'b'
* @param str
*/
succ(str: string): string;
/**
* Capitalize first letter of every word in the string.
* ('my name is epeli') => 'My Name Is Epeli'
* @param str
*/
titleize(str: string): string;
/**
* Converts underscored or dasherized string to a camelized one.
* ('-moz-transform') => 'MozTransform'
* @param str
*/
camelize(str: string): string;
/**
* Converts a camelized or dasherized string into an underscored one.
* ('MozTransform') => 'moz_transform'
* @param str
*/
underscored(str: string): string;
/**
* Converts a underscored or camelized string into an dasherized one.
* ('MozTransform') => '-moz-transform'
* @param str
*/
dasherize(str: string): string;
/**
* Converts string to camelized class name.
* ('some_class_name') => 'SomeClassName'
* @param str
*/
classify(str: string): string;
/**
* Converts an underscored, camelized, or dasherized string into a humanized one.
* Also removes beginning and ending whitespace, and removes the postfix '_id'.
* (' capitalize dash-CamelCase_underscore trim ') => 'Capitalize dash camel case underscore trim'
* @param str
*/
humanize(str: string): string;
/**
* Trims defined characters from begining and ending of the string.
* Defaults to whitespace characters.
* (' foobar ') => 'foobar'
* ('_-foobar-_', '_-') => 'foobar'
* @param str
* @param characters
*/
trim(str: string, characters?: string): string;
/**
* Trims defined characters from begining and ending of the string.
* Defaults to whitespace characters.
* (' foobar ') => 'foobar'
* ('_-foobar-_', '_-') => 'foobar'
* @param str
* @param characters
*/
strip(str: string, characters?: string): string;
/**
* Left trim. Similar to trim, but only for left side.
* @param str
* @param characters
*/
ltrim(str: string, characters?: string): string;
/**
* Left trim. Similar to trim, but only for left side.
* @param str
* @param characters
*/
lstrip(str: string, characters?: string): string;
/**
* Right trim. Similar to trim, but only for right side.
* @param str
* @param characters
*/
rtrim(str: string, characters?: string): string;
/**
* Right trim. Similar to trim, but only for right side.
* @param str
* @param characters
*/
rstrip(str: string, characters?: string): string;
/**
* Truncate string to specified length.
* ('Hello world').truncate(5) => 'Hello...'
* ('Hello').truncate(10) => 'Hello'
* @param str
* @param length
* @param truncateStr
*/
truncate(str: string, length: number, truncateStr?: string): string;
/**
* Elegant version of truncate.
* Makes sure the pruned string does not exceed the original length.
* Avoid half-chopped words when truncating.
* ('Hello, cruel world', 15) => 'Hello, cruel...'
* @param str
* @param length
* @param pruneStr
*/
prune(str: string, length: number, pruneStr?: string): string;
/**
* Split string by delimiter (String or RegExp).
* /\s+/ by default.
* (' I love you ') => ['I','love','you']
* ('I_love_you', '_') => ['I','love','you']
* @param str
* @param delimiter
*/
words(str: string): string[];
/**
* Split string by delimiter (String or RegExp).
* /\s+/ by default.
* (' I love you ') => ['I','love','you']
* ('I_love_you', '_') => ['I','love','you']
* @param str
* @param delimiter
*/
words(str: string, delimiter: string): string[];
/**
* Split string by delimiter (String or RegExp).
* /\s+/ by default.
* (' I love you ') => ['I','love','you']
* ('I_love_you', '_') => ['I','love','you']
* @param str
* @param delimiter
*/
words(str: string, delimiter: RegExp): string[];
/**
* Pads a string with characters until the total string length is equal to the passed length parameter.
* By default, pads on the left with the space char (' ').
* padStr is truncated to a single character if necessary.
* ('1', 8) => ' 1'
* ('1', 8, '0') => '00000001'
* ('1', 8, '0', 'right') => '10000000'
* ('1', 8, '0', 'both') => '00001000'
* ('1', 8, 'bleepblorp', 'both') => 'bbbb1bbb'
* @param str
* @param length
* @param padStr
* @param type
*/
pad(str: string, length: number, padStr?:string, type?: string): string;
/**
* Left-pad a string.
* Alias for pad(str, length, padStr, 'left')
* ('1', 8, '0') => '00000001'
* @param str
* @param length
* @param padStr
*/
lpad(str: string, length: number, padStr?: string): string;
/**
* Left-pad a string.
* Alias for pad(str, length, padStr, 'left')
* ('1', 8, '0') => '00000001'
* @param str
* @param length
* @param padStr
*/
rjust(str: string, length: number, padStr?: string): string;
/**
* Right-pad a string.
* Alias for pad(str, length, padStr, 'right')
* ('1', 8, '0') => '10000000'
* @param str
* @param length
* @param padStr
*/
rpad(str: string, length: number, padStr?: string): string;
/**
* Right-pad a string.
* Alias for pad(str, length, padStr, 'right')
* ('1', 8, '0') => '10000000'
* @param str
* @param length
* @param padStr
*/
ljust(str: string, length: number, padStr?: string): string;
/**
* Left/right-pad a string.
* Alias for pad(str, length, padStr, 'both')
* ('1', 8, '0') => '00001000'
* @param str
* @param length
* @param padStr
*/
lrpad(str: string, length: number, padStr?: string): string;
/**
* Left/right-pad a string.
* Alias for pad(str, length, padStr, 'both')
* ('1', 8, '0') => '00001000'
* @param str
* @param length
* @param padStr
*/
center(str: string, length: number, padStr?: string): string;
/**
* C like string formatting.
* _.sprintf('%.1f', 1.17) => '1.2'
* @param format
* @param args
*/
sprintf(format: string, ...args: any[]): string;
/**
* Parse string to number.
* Returns NaN if string can't be parsed to number.
* ('2.556').toNumber() => 3
* ('2.556').toNumber(1) => 2.6
* @param str
* @param decimals
*/
toNumber(str: string, decimals?: number): number;
/**
* Formats the numbers.
* (1000, 2) => '1,000.00'
* (123456789.123, 5, '.', ',') => '123,456,789.12300'
* @param number
* @param dec
* @param dsep
* @param tsep
*/
numberFormat(number: number, dec?: number, dsep?: string, tsep?: string): string;
/**
* Searches a string from left to right for a pattern.
* Returns a substring consisting of the characters in the string that are to the right of the pattern.
* If no match found, returns entire string.
* ('This_is_a_test_string').strRight('_') => 'is_a_test_string'
* @param str
* @param sep
*/
strRight(str: string, sep: string): string;
/**
* Searches a string from right to left for a pattern.
* Returns a substring consisting of the characters in the string that are to the right of the pattern.
* If no match found, returns entire string.
* ('This_is_a_test_string').strRightBack('_') => 'string'
* @param str
* @param sep
*/
strRightBack(str: string, sep: string): string;
/**
* Searches a string from left to right for a pattern.
* Returns a substring consisting of the characters in the string that are to the left of the pattern.
* If no match found, returns entire string.
* ('This_is_a_test_string').strLeft('_') => 'This'
* @param str
* @param sep
*/
strLeft(str: string, sep: string): string;
/**
* Searches a string from right to left for a pattern.
* Returns a substring consisting of the characters in the string that are to the left of the pattern.
* If no match found, returns entire string.
* ('This_is_a_test_string').strLeftBack('_') => 'This_is_a_test'
* @param str
* @param sep
*/
strLeftBack(str: string, sep: string): string;
/**
* Join an array into a human readable sentence.
* (['jQuery', 'Mootools', 'Prototype']) => 'jQuery, Mootools and Prototype'
* (['jQuery', 'Mootools', 'Prototype'], ', ', ' unt ') => 'jQuery, Mootools unt Prototype'
* @param array
* @param separator
* @param lastSeparator
* @param serial
*/
toSentence(array: any[], separator?: string, lastSeparator?: string, serial?: boolean): string;
/**
* The same as toSentence, but uses ', ' as default for lastSeparator.
* @param array
* @param separator
* @param lastSeparator
*/
toSentenceSerial(array: any[], separator?: string, lastSeparator?: string): string;
/**
* Transform text into a URL slug. Replaces whitespaces, accentuated, and special characters with a dash.
* ('Un éléphant à l'orée du bois') => 'un-elephant-a-loree-du-bois'
* @param str
*/
slugify(str: string): string;
/**
* Surround a string with another string.
* ('foo', 'ab') => 'abfooab'
* @param str
* @param wrapper
*/
surround(str: string, wrapper: string): string;
/**
* Quotes a string.
* quoteChar defaults to "
* ('foo') => '"foo"'
* @param str
*/
quote(str: string, quoteChar?: string): string;
/**
* Quotes a string.
* quoteChar defaults to "
* ('foo') => '"foo"'
* @param str
*/
q(str: string, quoteChar?: string): string;
/**
* Unquotes a string.
* quoteChar defaults to "
* ('"foo"') => 'foo'
* ("'foo'", "'") => 'foo'
* @param str
*/
unquote(str: string, quoteChar?: string): string;
/**
* Repeat a string with an optional separator.
* ('foo', 3) => 'foofoofoo'
* ('foo', 3, 'bar') => 'foobarfoobarfoo'
* @param value
* @param count
* @param separator
*/
repeat(value: string, count: number, separator?:string): string;
/**
* Naturally sort strings like humans would do.
* Caution: this function is charset dependent.
* @param str1
* @param str2
*/
naturalCmp(str1: string, str2: string): number;
/**
* Calculates Levenshtein distance between two strings.
* ('kitten', 'kittah') => 2
* @param str1
* @param str2
*/
levenshtein(str1: string, str2: string): number;
/**
* Turn strings that can be commonly considered as booleans to real booleans.
* Such as "true", "false", "1" and "0". This function is case insensitive.
* ('true') => true
* ('FALSE') => false
* ('random') => undefined
* ('truthy', ['truthy'], ['falsy']) => true
* ('true only at start', [/^true/]) => true
* @param str
* @param trueValues
* @param falseValues
*/
toBoolean(str: string, trueValues?: any[], falseValues?: any[]): boolean;
}
declare module 'underscore.string' {
var underscoreString: UnderscoreStringStatic;
export = underscoreString;
}
// TODO interface UnderscoreString extends Underscore<string>