UNPKG

10.9 kBJavaScriptView Raw
1'use strict';
2
3var 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
18exports.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
45exports.centerAlign = function centerAlign(str) {
46 return exports.isString(str) ? utils.alignCenter(str, '&nbsp;') : '';
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
69exports.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
90exports.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
110exports.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
135exports.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
157exports.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
174exports.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
191exports.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
211exports.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
233exports.split = function split(str, ch) {
234 return exports.isString(str) ? str.split(ch || ',') : '';
235};
236
237/**
238 * Strip the indentation from a `string`.
239 *
240 * ```js
241 * <%= stripIndent(" _ABC_") %>
242 * //=> 'ABC'
243 * ```
244 *
245 * @param {String} `string` The string to strip indentation from.
246 * @return {String}
247 * @api public
248 */
249
250exports.stripIndent = function stripIndent(str) {
251 if (!exports.isString(str)) return '';
252
253 var matches = str.match(/^[ \t]+(?!\s)/gm);
254 if (!matches) return str;
255
256 var ws = matches.reverse().pop();
257 var len = ws.length;
258 if (len) {
259 var re =new RegExp('^[ \\t]{' + len + '}', 'gm');
260 return str.replace(re, '');
261 }
262 return str;
263};
264
265/**
266 * Trim extraneous whitespace from the beginning and end
267 * of a string.
268 *
269 * ```js
270 * <%= trim(" ABC ") %>
271 * //=> 'ABC'
272 * ```
273 *
274 * @param {String} `string` The string to trim.
275 * @return {String}
276 * @api public
277 */
278
279exports.trim = function trim(str) {
280 return exports.isString(str) ? str.trim() : '';
281};
282
283/**
284 * dash-case the characters in `string`. This is similar to [slugify],
285 * but [slugify] makes the string compatible to be used as a URL slug.
286 *
287 * ```js
288 * <%= dashcase("a b.c d_e") %>
289 * //=> 'a-b-c-d-e'
290 * ```
291 *
292 * @param {String} `string`
293 * @return {String}
294 * @tags dasherize, dashify, hyphenate, case
295 * @api public
296 */
297
298exports.dashcase = function dashcase(str) {
299 if (!exports.isString(str)) return '';
300 if (str.length === 1) { return str.toLowerCase(); }
301 var re = /[-_.\W\s]+(\w|$)/g;
302 return exports.chop(str).replace(re, function(_, ch) {
303 return '-' + ch;
304 });
305};
306
307/**
308 * path/case the characters in `string`.
309 *
310 * ```js
311 * <%= pathcase("a-b-c d_e") %>
312 * //=> 'a/b/c/d/e'
313 * ```
314 *
315 * @param {String} `string`
316 * @return {String}
317 * @api public
318 */
319
320exports.pathcase = function pathcase(str) {
321 if (!exports.isString(str)) return '';
322 if (str.length === 1) { return str.toLowerCase(); }
323 var re = /[_.-\W\s]+(\w|$)/g;
324 return exports.chop(str).replace(re, function(_, ch) {
325 return '/' + ch;
326 });
327};
328
329/**
330 * Sentence-case the characters in `string`.
331 *
332 * ```js
333 * <%= sentencecase("foo bar baz.") %>
334 * //=> 'Foo bar baz.'
335 * ```
336 *
337 * @param {String} `string`
338 * @return {String}
339 * @api public
340 */
341
342exports.sentencecase = function sentencecase(str) {
343 if (!exports.isString(str)) return '';
344 if (str.length === 1) { return str.toUpperCase(); }
345 return str.charAt(0).toUpperCase() + str.slice(1);
346};
347
348/**
349 * Replace spaces in a string with hyphens. This
350 *
351 * ```js
352 * <%= hyphenate("a b c") %>
353 * //=> 'a-b-c'
354 * ```
355 *
356 * @param {String} `string`
357 * @return {String}
358 * @api public
359 */
360
361exports.hyphenate = function hyphenate(str) {
362 return exports.isString(str) ? exports.chop(str).split(' ').join('-') : '';
363};
364
365/**
366 * TODO: Need to use a proper slugifier for this.
367 * I don't want to use `node-slug`, it's way too
368 * huge for this.
369 *
370 * ```js
371 * <%= slugify('This is a post title.') %>
372 * //=> 'this-is-a-post-title'
373 * ```
374 *
375 * @param {String} `string` The string to slugify.
376 * @return {String} Slug.
377 * @api draft
378 */
379
380exports.slugify = function slugify(str) {
381 return exports.isString(str) ? exports.dashcase(str) : '';
382};
383
384/**
385 * Reverse the characters in a string.
386 *
387 * ```js
388 * <%= reverse("abc") %>
389 * //=> 'cba'
390 * ```
391 *
392 * @param {String} `str` The string to reverse.
393 * @return {String}
394 * @api public
395 */
396
397exports.reverse = function reverse(str) {
398 return exports.isString(str) ? str.split('').reverse().join('') : '';
399};
400
401/**
402 * Right align the characters in a string using
403 * non-breaking spaces.
404 *
405 * ```js
406 * <%= rightAlign(str) %>
407 * ```
408 *
409 * @param {String} `str` The string to reverse.
410 * @return {String} Right-aligned string.
411 * @related centerAlign
412 * @api public
413 */
414
415exports.rightAlign = function rightAlign(str) {
416 return exports.isString(str) ? utils.alignRight(str) : '';
417};
418
419/**
420 * Replace occurrences of `a` with `b`.
421 *
422 * ```js
423 * <%= replace("abcabc", /a/, "z") %>
424 * //=> 'zbczbc'
425 * ```
426 *
427 * @param {String} `str`
428 * @param {String|RegExp} `a` Can be a string or regexp.
429 * @param {String} `b`
430 * @return {String}
431 * @api public
432 */
433
434exports.replace = function replace(str, a, b) {
435 if (!exports.isString(str)) return '';
436 if (!a) { return str; }
437 if (!b) { b = ''; }
438 return str.split(a).join(b);
439};
440
441/**
442 * Truncate a string by removing all HTML tags and limiting the result
443 * to the specified `length`.
444 *
445 * ```js
446 * <%= titlecase("big deal") %>
447 * //=> 'foo bar'
448 * ```
449 *
450 * @param {String} `str`
451 * @param {Number} `length` The desired length of the returned string.
452 * @return {String} The truncated string.
453 * @api public
454 */
455
456exports.titlecase = function titlecase(str, length) {
457 return exports.isString(str) ? utils.titlecase(str) : '';
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 * <%= truncate("<span>foo bar baz</span>", 7) %>
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
475exports.truncate = function truncate(str, length) {
476 return exports.isString(str) ? str.substr(0, length) : '';
477};
478
479/**
480 * Uppercase the characters in a string.
481 *
482 * ```js
483 * <%= uppercase("abc") %>
484 * //=> 'ABC'
485 * ```
486 *
487 * @param {String} `string` The string to uppercase.
488 * @return {String}
489 * @api public
490 */
491
492exports.uppercase = exports.upper = function uppercase(str) {
493 return exports.isString(str) ? str.toUpperCase() : '';
494};
495// ensure code conext picks up alias as a method
496exports.upper = function upper(str) {
497 return exports.uppercase.apply(null, arguments);
498};
499
500/**
501 * Wrap words to a specified width using [word-wrap].
502 *
503 * ```js
504 * <%= wordwrap("a b c d e f", {width: 5, newline: '<br> '}) %>
505 * //=> ' a b c <br> d e f'
506 * ```
507 *
508 * @param {String} `string` The string with words to wrap.
509 * @param {Options} `object` Options to pass to [word-wrap]
510 * @return {String} Formatted string.
511 * @api public
512 */
513
514exports.wordwrap = function wordwrap(str) {
515 return exports.isString(str) ? utils.wrap.apply(utils.wrap, arguments) : '';
516};