UNPKG

1.15 kBJavaScriptView Raw
1import baseRepeat from './_baseRepeat.js';
2import baseToString from './_baseToString.js';
3import castSlice from './_castSlice.js';
4import hasUnicode from './_hasUnicode.js';
5import stringSize from './_stringSize.js';
6import stringToArray from './_stringToArray.js';
7
8/* Built-in method references for those with the same name as other `lodash` methods. */
9var nativeCeil = Math.ceil;
10
11/**
12 * Creates the padding for `string` based on `length`. The `chars` string
13 * is truncated if the number of characters exceeds `length`.
14 *
15 * @private
16 * @param {number} length The padding length.
17 * @param {string} [chars=' '] The string used as padding.
18 * @returns {string} Returns the padding for `string`.
19 */
20function createPadding(length, chars) {
21 chars = chars === undefined ? ' ' : baseToString(chars);
22
23 var charsLength = chars.length;
24 if (charsLength < 2) {
25 return charsLength ? baseRepeat(chars, length) : chars;
26 }
27 var result = baseRepeat(chars, nativeCeil(length / stringSize(chars)));
28 return hasUnicode(chars)
29 ? castSlice(stringToArray(result), 0, length).join('')
30 : result.slice(0, length);
31}
32
33export default createPadding;