1 | ;
|
2 |
|
3 | var utils = require('./utils');
|
4 |
|
5 | /**
|
6 | * camelCase the characters in `string`.
|
7 | *
|
8 | * ```js
|
9 | * <%= camelcase("foo bar baz") %>
|
10 | * //=> 'fooBarBaz'
|
11 | * ```
|
12 | *
|
13 | * @param {String} `string` The string to camelcase.
|
14 | * @return {String}
|
15 | * @api public
|
16 | */
|
17 |
|
18 | exports.camelcase = function camelcase(str) {
|
19 | if (!exports.isString(str)) return '';
|
20 | if (str.length === 1) {
|
21 | return str.toLowerCase();
|
22 | }
|
23 |
|
24 | var re = /[-_.\W\s]+(\w|$)/g;
|
25 | str = exports.chop(str).toLowerCase();
|
26 | return str.replace(re, function(_, ch) {
|
27 | return ch.toUpperCase();
|
28 | });
|
29 | };
|
30 |
|
31 | /**
|
32 | * Center align the characters in a string using
|
33 | * non-breaking spaces.
|
34 | *
|
35 | * ```js
|
36 | * <%= centerAlign("abc") %>
|
37 | * ```
|
38 | *
|
39 | * @param {String} `str` The string to reverse.
|
40 | * @return {String} Centered string.
|
41 | * @related rightAlign
|
42 | * @api public
|
43 | */
|
44 |
|
45 | exports.centerAlign = function centerAlign(str) {
|
46 | return exports.isString(str) ? utils.alignCenter(str, ' ') : '';
|
47 | };
|
48 |
|
49 | /**
|
50 | * Like trim, but removes both extraneous whitespace and
|
51 | * non-word characters from the beginning and end of a string.
|
52 | *
|
53 | * ```js
|
54 | * <%= chop("_ABC_") %>
|
55 | * //=> 'ABC'
|
56 | *
|
57 | * <%= chop("-ABC-") %>
|
58 | * //=> 'ABC'
|
59 | *
|
60 | * <%= chop(" ABC ") %>
|
61 | * //=> 'ABC'
|
62 | * ```
|
63 | *
|
64 | * @param {String} `string` The string to chop.
|
65 | * @return {String}
|
66 | * @api public
|
67 | */
|
68 |
|
69 | exports.chop = function chop(str) {
|
70 | if (!exports.isString(str)) return '';
|
71 | var re = /^[-_.\W\s]+|[-_.\W\s]+$/g;
|
72 | return str.trim().replace(re, '');
|
73 | };
|
74 |
|
75 | /**
|
76 | * Count the number of occurrances of a substring
|
77 | * within a string.
|
78 | *
|
79 | * ```js
|
80 | * <%= count("abcabcabc", "a") %>
|
81 | * //=> '3'
|
82 | * ```
|
83 | *
|
84 | * @param {String} `string`
|
85 | * @param {String} `substring`
|
86 | * @return {Number} The occurances of `substring` in `string`
|
87 | * @api public
|
88 | */
|
89 |
|
90 | exports.count = function count(str, sub) {
|
91 | if (exports.isString(str)) {
|
92 | return (sub ? (str.split(sub).length - 1) : '0');
|
93 | }
|
94 | return '';
|
95 | };
|
96 |
|
97 | /**
|
98 | * dot.case the characters in `string`.
|
99 | *
|
100 | * ```js
|
101 | * <%= dotcase("a-b-c d_e") %>
|
102 | * //=> 'a.b.c.d.e'
|
103 | * ```
|
104 | *
|
105 | * @param {String} `string`
|
106 | * @return {String}
|
107 | * @api public
|
108 | */
|
109 |
|
110 | exports.dotcase = function dotcase(str) {
|
111 | if (!exports.isString(str)) return '';
|
112 | if (str.length === 1) { return str.toLowerCase(); }
|
113 | var re = /[-_.\W\s]+(\w|$)/g;
|
114 | return exports.chop(str).replace(re, function(_, ch) {
|
115 | return '.' + ch;
|
116 | });
|
117 | };
|
118 |
|
119 | /**
|
120 | * Truncate a string to the specified `length`, and append
|
121 | * it with an elipsis, `…`.
|
122 | *
|
123 | * ```js
|
124 | * <%= ellipsis("<span>foo bar baz</span>", 7) %>
|
125 | * //=> 'foo bar…'
|
126 | * ```
|
127 | *
|
128 | * @param {String} `str`
|
129 | * @param {Number} `length` The desired length of the returned string.
|
130 | * @param {String} `ch` Optionally pass custom characters to append. Default is `…`.
|
131 | * @return {String} The truncated string.
|
132 | * @api public
|
133 | */
|
134 |
|
135 | exports.ellipsis = function ellipsis(str, limit, ch) {
|
136 | return exports.isString(str)
|
137 | ? (exports.truncate(str, limit) + (ch || '…'))
|
138 | : '';
|
139 | };
|
140 |
|
141 | /**
|
142 | * Returns true if the value is a string.
|
143 | *
|
144 | * ```js
|
145 | * <%= isString('abc') %>
|
146 | * //=> 'true'
|
147 | *
|
148 | * <%= isString(null) %>
|
149 | * //=> 'false'
|
150 | * ```
|
151 | *
|
152 | * @param {String} `val`
|
153 | * @return {Boolean} True if the value is a string.
|
154 | * @api public
|
155 | */
|
156 |
|
157 | exports.isString = function isString(str) {
|
158 | return str && typeof str === 'string';
|
159 | };
|
160 |
|
161 | /**
|
162 | * Lowercase the characters in the given `string`.
|
163 | *
|
164 | * ```js
|
165 | * <%= lowercase("ABC") %>
|
166 | * //=> 'abc'
|
167 | * ```
|
168 | *
|
169 | * @param {String} `string` The string to lowercase.
|
170 | * @return {String}
|
171 | * @api public
|
172 | */
|
173 |
|
174 | exports.lowercase = exports.lower = function lowercase(str) {
|
175 | return exports.isString(str) ? str.toLowerCase() : '';
|
176 | };
|
177 |
|
178 | /**
|
179 | * PascalCase the characters in `string`.
|
180 | *
|
181 | * ```js
|
182 | * <%= pascalcase("foo bar baz") %>
|
183 | * //=> 'FooBarBaz'
|
184 | * ```
|
185 | *
|
186 | * @param {String} `string`
|
187 | * @return {String}
|
188 | * @api public
|
189 | */
|
190 |
|
191 | exports.pascalcase = function pascalcase(str) {
|
192 | if (!exports.isString(str)) return '';
|
193 | if (str.length === 1) { return str.toUpperCase(); }
|
194 | str = exports.camelcase(str);
|
195 | return str[0].toUpperCase() + str.slice(1);
|
196 | };
|
197 |
|
198 | /**
|
199 | * snake_case the characters in `string`.
|
200 | *
|
201 | * ```js
|
202 | * <%= snakecase("a-b-c d_e") %>
|
203 | * //=> 'a_b_c_d_e'
|
204 | * ```
|
205 | *
|
206 | * @param {String} `string`
|
207 | * @return {String}
|
208 | * @api public
|
209 | */
|
210 |
|
211 | exports.snakecase = function snakecase(str) {
|
212 | if (!exports.isString(str)) return '';
|
213 | if (str.length === 1) { return str.toLowerCase(); }
|
214 | var re = /[-_.\W\s]+(\w|$)/g;
|
215 | return exports.chop(str).replace(re, function(_, ch) {
|
216 | return '_' + ch;
|
217 | });
|
218 | };
|
219 |
|
220 | /**
|
221 | * Split `string` by the given `character`.
|
222 | *
|
223 | * ```js
|
224 | * <%= split("a,b,c", ",") %>
|
225 | * //=> ['a', 'b', 'c']
|
226 | * ```
|
227 | *
|
228 | * @param {String} `string` The string to split.
|
229 | * @return {String} `character` Default is `,`
|
230 | * @api public
|
231 | */
|
232 |
|
233 | exports.split = function split(str, ch) {
|
234 | return exports.isString(str) ? str.split(ch || ',') : '';
|
235 | };
|
236 |
|
237 | /**
|
238 | * Strip `substring` from the given `string`.
|
239 | *
|
240 | * ```js
|
241 | * <%= strip("foo-bar", "foo-") %>
|
242 | * //=> 'bar'
|
243 | * ```
|
244 | *
|
245 | * @param {String|RegExp} `substring` The string or regex pattern of the substring to remove.
|
246 | * @param {String} `string` The target string.
|
247 | * @api public
|
248 | */
|
249 |
|
250 | exports.strip = function strip(val, str) {
|
251 | if (!exports.isString(val) && !(val instanceof RegExp)) return '';
|
252 | if (!exports.isString(str)) return '';
|
253 | return str.split(val).join('');
|
254 | };
|
255 |
|
256 | /**
|
257 | * Strip the indentation from a `string`.
|
258 | *
|
259 | * ```js
|
260 | * <%= stripIndent(" _ABC_") %>
|
261 | * //=> 'ABC'
|
262 | * ```
|
263 | *
|
264 | * @param {String} `string` The string to strip indentation from.
|
265 | * @return {String}
|
266 | * @api public
|
267 | */
|
268 |
|
269 | exports.stripIndent = function stripIndent(str) {
|
270 | if (!exports.isString(str)) return '';
|
271 |
|
272 | var matches = str.match(/^[ \t]+(?!\s)/gm);
|
273 | if (!matches) return str;
|
274 |
|
275 | var ws = matches.reverse().pop();
|
276 | var len = ws.length;
|
277 | if (len) {
|
278 | var re = new RegExp('^[ \\t]{' + len + '}', 'gm');
|
279 | return str.replace(re, '');
|
280 | }
|
281 | return str;
|
282 | };
|
283 |
|
284 | /**
|
285 | * Trim extraneous whitespace from the beginning and end
|
286 | * of a string.
|
287 | *
|
288 | * ```js
|
289 | * <%= trim(" ABC ") %>
|
290 | * //=> 'ABC'
|
291 | * ```
|
292 | *
|
293 | * @param {String} `string` The string to trim.
|
294 | * @return {String}
|
295 | * @api public
|
296 | */
|
297 |
|
298 | exports.trim = function trim(str) {
|
299 | return exports.isString(str) ? str.trim() : '';
|
300 | };
|
301 |
|
302 | /**
|
303 | * dash-case the characters in `string`. This is similar to [slugify],
|
304 | * but [slugify] makes the string compatible to be used as a URL slug.
|
305 | *
|
306 | * ```js
|
307 | * <%= dashcase("a b.c d_e") %>
|
308 | * //=> 'a-b-c-d-e'
|
309 | * ```
|
310 | *
|
311 | * @param {String} `string`
|
312 | * @return {String}
|
313 | * @tags dasherize, dashify, hyphenate, case
|
314 | * @api public
|
315 | */
|
316 |
|
317 | exports.dashcase = function dashcase(str) {
|
318 | if (!exports.isString(str)) return '';
|
319 | if (str.length === 1) { return str.toLowerCase(); }
|
320 | var re = /[-_.\W\s]+(\w|$)/g;
|
321 | return exports.chop(str).replace(re, function(_, ch) {
|
322 | return '-' + ch;
|
323 | });
|
324 | };
|
325 |
|
326 | /**
|
327 | * path/case the characters in `string`.
|
328 | *
|
329 | * ```js
|
330 | * <%= pathcase("a-b-c d_e") %>
|
331 | * //=> 'a/b/c/d/e'
|
332 | * ```
|
333 | *
|
334 | * @param {String} `string`
|
335 | * @return {String}
|
336 | * @api public
|
337 | */
|
338 |
|
339 | exports.pathcase = function pathcase(str) {
|
340 | if (!exports.isString(str)) return '';
|
341 | if (str.length === 1) { return str.toLowerCase(); }
|
342 | var re = /[_.-\W\s]+(\w|$)/g;
|
343 | return exports.chop(str).replace(re, function(_, ch) {
|
344 | return '/' + ch;
|
345 | });
|
346 | };
|
347 |
|
348 | /**
|
349 | * Sentence-case the characters in `string`.
|
350 | *
|
351 | * ```js
|
352 | * <%= sentencecase("foo bar baz.") %>
|
353 | * //=> 'Foo bar baz.'
|
354 | * ```
|
355 | *
|
356 | * @param {String} `string`
|
357 | * @return {String}
|
358 | * @api public
|
359 | */
|
360 |
|
361 | exports.sentencecase = function sentencecase(str) {
|
362 | if (!exports.isString(str)) return '';
|
363 | if (str.length === 1) { return str.toUpperCase(); }
|
364 | return str.charAt(0).toUpperCase() + str.slice(1);
|
365 | };
|
366 |
|
367 | /**
|
368 | * Replace spaces in a string with hyphens. This
|
369 | *
|
370 | * ```js
|
371 | * <%= hyphenate("a b c") %>
|
372 | * //=> 'a-b-c'
|
373 | * ```
|
374 | *
|
375 | * @param {String} `string`
|
376 | * @return {String}
|
377 | * @api public
|
378 | */
|
379 |
|
380 | exports.hyphenate = function hyphenate(str) {
|
381 | return exports.isString(str) ? exports.chop(str).split(' ').join('-') : '';
|
382 | };
|
383 |
|
384 | /**
|
385 | * TODO: Need to use a proper slugifier for this.
|
386 | * I don't want to use `node-slug`, it's way too
|
387 | * huge for this.
|
388 | *
|
389 | * ```js
|
390 | * <%= slugify('This is a post title.') %>
|
391 | * //=> 'this-is-a-post-title'
|
392 | * ```
|
393 | *
|
394 | * @param {String} `string` The string to slugify.
|
395 | * @return {String} Slug.
|
396 | * @api draft
|
397 | */
|
398 |
|
399 | exports.slugify = function slugify(str) {
|
400 | return exports.isString(str) ? exports.dashcase(str) : '';
|
401 | };
|
402 |
|
403 | /**
|
404 | * Reverse the characters in a string.
|
405 | *
|
406 | * ```js
|
407 | * <%= reverse("abc") %>
|
408 | * //=> 'cba'
|
409 | * ```
|
410 | *
|
411 | * @param {String} `str` The string to reverse.
|
412 | * @return {String}
|
413 | * @api public
|
414 | */
|
415 |
|
416 | exports.reverse = function reverse(str) {
|
417 | return exports.isString(str) ? str.split('').reverse().join('') : '';
|
418 | };
|
419 |
|
420 | /**
|
421 | * Right align the characters in a string using
|
422 | * non-breaking spaces.
|
423 | *
|
424 | * ```js
|
425 | * <%= rightAlign(str) %>
|
426 | * ```
|
427 | *
|
428 | * @param {String} `str` The string to reverse.
|
429 | * @return {String} Right-aligned string.
|
430 | * @related centerAlign
|
431 | * @api public
|
432 | */
|
433 |
|
434 | exports.rightAlign = function rightAlign(str) {
|
435 | return exports.isString(str) ? utils.alignRight(str) : '';
|
436 | };
|
437 |
|
438 | /**
|
439 | * Replace occurrences of `a` with `b`.
|
440 | *
|
441 | * ```js
|
442 | * <%= replace("abcabc", /a/, "z") %>
|
443 | * //=> 'zbczbc'
|
444 | * ```
|
445 | *
|
446 | * @param {String} `str`
|
447 | * @param {String|RegExp} `a` Can be a string or regexp.
|
448 | * @param {String} `b`
|
449 | * @return {String}
|
450 | * @api public
|
451 | */
|
452 |
|
453 | exports.replace = function replace(str, a, b) {
|
454 | if (!exports.isString(str)) return '';
|
455 | if (!a) { return str; }
|
456 | if (!b) { b = ''; }
|
457 | return str.split(a).join(b);
|
458 | };
|
459 |
|
460 | /**
|
461 | * Truncate a string by removing all HTML tags and limiting the result
|
462 | * to the specified `length`.
|
463 | *
|
464 | * ```js
|
465 | * <%= titlecase("big deal") %>
|
466 | * //=> 'foo bar'
|
467 | * ```
|
468 | *
|
469 | * @param {String} `str`
|
470 | * @param {Number} `length` The desired length of the returned string.
|
471 | * @return {String} The truncated string.
|
472 | * @api public
|
473 | */
|
474 |
|
475 | exports.titlecase = exports.titleize = function titlecase(str, length) {
|
476 | return exports.isString(str) ? utils.titlecase(str) : '';
|
477 | };
|
478 |
|
479 | /**
|
480 | * Truncate a string by removing all HTML tags and limiting the result
|
481 | * to the specified `length`.
|
482 | *
|
483 | * ```js
|
484 | * <%= truncate("<span>foo bar baz</span>", 7) %>
|
485 | * //=> 'foo bar'
|
486 | * ```
|
487 | *
|
488 | * @param {String} `str`
|
489 | * @param {Number} `length` The desired length of the returned string.
|
490 | * @return {String} The truncated string.
|
491 | * @api public
|
492 | */
|
493 |
|
494 | exports.truncate = function truncate(str, length) {
|
495 | return exports.isString(str) ? str.substr(0, length) : '';
|
496 | };
|
497 |
|
498 | /**
|
499 | * Uppercase the characters in a string.
|
500 | *
|
501 | * ```js
|
502 | * <%= uppercase("abc") %>
|
503 | * //=> 'ABC'
|
504 | * ```
|
505 | *
|
506 | * @param {String} `string` The string to uppercase.
|
507 | * @return {String}
|
508 | * @api public
|
509 | */
|
510 |
|
511 | exports.uppercase = exports.upper = function uppercase(str) {
|
512 | return exports.isString(str) ? str.toUpperCase() : '';
|
513 | };
|
514 | // ensure code conext picks up alias as a method
|
515 | exports.upper = function upper(str) {
|
516 | return exports.uppercase.apply(null, arguments);
|
517 | };
|
518 |
|
519 | /**
|
520 | * Wrap words to a specified width using [word-wrap].
|
521 | *
|
522 | * ```js
|
523 | * <%= wordwrap("a b c d e f", {width: 5, newline: '<br> '}) %>
|
524 | * //=> ' a b c <br> d e f'
|
525 | * ```
|
526 | *
|
527 | * @param {String} `string` The string with words to wrap.
|
528 | * @param {Options} `object` Options to pass to [word-wrap]
|
529 | * @return {String} Formatted string.
|
530 | * @api public
|
531 | */
|
532 |
|
533 | exports.wordwrap = function wordwrap(str) {
|
534 | return exports.isString(str) ? utils.wrap.apply(utils.wrap, arguments) : '';
|
535 | };
|