-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathcreatePad.js
34 lines (29 loc) · 1.06 KB
/
createPad.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import baseToString from './baseToString';
import repeat from '../string/repeat';
import root from './root';
/** Native method references. */
var ceil = Math.ceil;
/* Native method references for those with the same name as other `lodash` methods. */
var nativeIsFinite = root.isFinite;
/**
* Creates the pad required for `string` based on the given padding length.
* The `chars` string may be truncated if the number of padding characters
* exceeds the padding length.
*
* @private
* @param {string} string The string to create padding for.
* @param {number} [length=0] The padding length.
* @param {string} [chars=' '] The string used as padding.
* @returns {string} Returns the pad for `string`.
*/
function createPad(string, length, chars) {
var strLength = string.length;
length = +length;
if (strLength >= length || !nativeIsFinite(length)) {
return '';
}
var padLength = length - strLength;
chars = chars == null ? ' ' : baseToString(chars);
return repeat(chars, ceil(padLength / chars.length)).slice(0, padLength);
}
export default createPad;