57979926f1b268c66be6d4d35d4f5f5335cccc73
[platform/framework/web/crosswalk-tizen.git] /
1 var repeat = require('../string/repeat');
2
3 /** Native method references. */
4 var ceil = Math.ceil;
5
6 /* Native method references for those with the same name as other `lodash` methods. */
7 var nativeIsFinite = global.isFinite;
8
9 /**
10  * Creates the padding required for `string` based on the given `length`.
11  * The `chars` string is truncated if the number of characters exceeds `length`.
12  *
13  * @private
14  * @param {string} string The string to create padding for.
15  * @param {number} [length=0] The padding length.
16  * @param {string} [chars=' '] The string used as padding.
17  * @returns {string} Returns the pad for `string`.
18  */
19 function createPadding(string, length, chars) {
20   var strLength = string.length;
21   length = +length;
22
23   if (strLength >= length || !nativeIsFinite(length)) {
24     return '';
25   }
26   var padLength = length - strLength;
27   chars = chars == null ? ' ' : (chars + '');
28   return repeat(chars, ceil(padLength / chars.length)).slice(0, padLength);
29 }
30
31 module.exports = createPadding;