SIGN IN SIGN UP

A modern JavaScript utility library delivering modularity, performance, & extras.

0 0 3 JavaScript
2016-08-11 23:27:31 -07:00
define(['./_baseRepeat', './_baseToString', './_castSlice', './_hasUnicode', './_stringSize', './_stringToArray'], function(baseRepeat, baseToString, castSlice, hasUnicode, stringSize, stringToArray) {
2015-01-08 00:37:01 -08:00
2015-12-16 17:53:20 -08:00
/** Used as a safe reference for `undefined` in pre-ES5 environments. */
var undefined;
/* Built-in method references for those with the same name as other `lodash` methods. */
var nativeCeil = Math.ceil;
2015-01-08 00:37:01 -08:00
/**
2015-12-16 17:53:20 -08:00
* Creates the padding for `string` based on `length`. The `chars` string
* is truncated if the number of characters exceeds `length`.
2015-01-08 00:37:01 -08:00
*
* @private
2016-03-26 00:00:01 -07:00
* @param {number} length The padding length.
2015-01-08 00:37:01 -08:00
* @param {string} [chars=' '] The string used as padding.
2015-12-16 17:53:20 -08:00
* @returns {string} Returns the padding for `string`.
2015-01-08 00:37:01 -08:00
*/
2016-03-26 00:00:01 -07:00
function createPadding(length, chars) {
2016-04-19 22:17:16 -07:00
chars = chars === undefined ? ' ' : baseToString(chars);
2015-12-16 17:53:20 -08:00
2016-03-26 00:00:01 -07:00
var charsLength = chars.length;
if (charsLength < 2) {
return charsLength ? baseRepeat(chars, length) : chars;
}
var result = baseRepeat(chars, nativeCeil(length / stringSize(chars)));
2016-08-11 23:27:31 -07:00
return hasUnicode(chars)
2016-04-10 22:53:08 -07:00
? castSlice(stringToArray(result), 0, length).join('')
2016-03-26 00:00:01 -07:00
: result.slice(0, length);
2015-01-08 00:37:01 -08:00
}
2015-12-16 17:49:07 -08:00
return createPadding;
2015-01-08 00:37:01 -08:00
});