1 var baseToString = require('../internal/baseToString'),
2 createPadding = require('../internal/createPadding');
4 /** Native method references. */
8 /* Native method references for those with the same name as other `lodash` methods. */
9 var nativeIsFinite = global.isFinite;
12 * Pads `string` on the left and right sides if it is shorter than `length`.
13 * Padding characters are truncated if they can't be evenly divided by `length`.
18 * @param {string} [string=''] The string to pad.
19 * @param {number} [length=0] The padding length.
20 * @param {string} [chars=' '] The string used as padding.
21 * @returns {string} Returns the padded string.
27 * _.pad('abc', 8, '_-');
33 function pad(string, length, chars) {
34 string = baseToString(string);
37 var strLength = string.length;
38 if (strLength >= length || !nativeIsFinite(length)) {
41 var mid = (length - strLength) / 2,
42 leftLength = floor(mid),
43 rightLength = ceil(mid);
45 chars = createPadding('', rightLength, chars);
46 return chars.slice(0, leftLength) + string + chars;