UNPKG

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