Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / string / pad.js
1 var baseToString = require('../internal/baseToString'),
2     createPadding = require('../internal/createPadding');
3
4 /* Native method references for those with the same name as other `lodash` methods. */
5 var nativeCeil = Math.ceil,
6     nativeFloor = Math.floor,
7     nativeIsFinite = global.isFinite;
8
9 /**
10  * Pads `string` on the left and right sides if it's shorter than `length`.
11  * Padding characters are truncated if they can't be evenly divided by `length`.
12  *
13  * @static
14  * @memberOf _
15  * @category String
16  * @param {string} [string=''] The string to pad.
17  * @param {number} [length=0] The padding length.
18  * @param {string} [chars=' '] The string used as padding.
19  * @returns {string} Returns the padded string.
20  * @example
21  *
22  * _.pad('abc', 8);
23  * // => '  abc   '
24  *
25  * _.pad('abc', 8, '_-');
26  * // => '_-abc_-_'
27  *
28  * _.pad('abc', 3);
29  * // => 'abc'
30  */
31 function pad(string, length, chars) {
32   string = baseToString(string);
33   length = +length;
34
35   var strLength = string.length;
36   if (strLength >= length || !nativeIsFinite(length)) {
37     return string;
38   }
39   var mid = (length - strLength) / 2,
40       leftLength = nativeFloor(mid),
41       rightLength = nativeCeil(mid);
42
43   chars = createPadding('', rightLength, chars);
44   return chars.slice(0, leftLength) + string + chars;
45 }
46
47 module.exports = pad;