UNPKG

1.59 kBJavaScriptView Raw
1import baseRepeat from './_baseRepeat';
2import stringSize from './_stringSize';
3import stringToArray from './_stringToArray';
4
5/** Used to compose unicode character classes. */
6var rsAstralRange = '\\ud800-\\udfff',
7 rsComboMarksRange = '\\u0300-\\u036f\\ufe20-\\ufe23',
8 rsComboSymbolsRange = '\\u20d0-\\u20f0',
9 rsVarRange = '\\ufe0e\\ufe0f';
10
11/** Used to compose unicode capture groups. */
12var rsZWJ = '\\u200d';
13
14/** Used to detect strings with [zero-width joiners or code points from the astral planes](http://eev.ee/blog/2015/09/12/dark-corners-of-unicode/). */
15var reHasComplexSymbol = RegExp('[' + rsZWJ + rsAstralRange + rsComboMarksRange + rsComboSymbolsRange + rsVarRange + ']');
16
17/* Built-in method references for those with the same name as other `lodash` methods. */
18var nativeCeil = Math.ceil;
19
20/**
21 * Creates the padding for `string` based on `length`. The `chars` string
22 * is truncated if the number of characters exceeds `length`.
23 *
24 * @private
25 * @param {number} length The padding length.
26 * @param {string} [chars=' '] The string used as padding.
27 * @returns {string} Returns the padding for `string`.
28 */
29function createPadding(length, chars) {
30 chars = chars === undefined ? ' ' : (chars + '');
31
32 var charsLength = chars.length;
33 if (charsLength < 2) {
34 return charsLength ? baseRepeat(chars, length) : chars;
35 }
36 var result = baseRepeat(chars, nativeCeil(length / stringSize(chars)));
37 return reHasComplexSymbol.test(chars)
38 ? stringToArray(result).slice(0, length).join('')
39 : result.slice(0, length);
40}
41
42export default createPadding;