1 | // https://github.com/tc39/proposal-string-pad-start-end
|
---|
2 | var toLength = require('../internals/to-length');
|
---|
3 | var toString = require('../internals/to-string');
|
---|
4 | var repeat = require('../internals/string-repeat');
|
---|
5 | var requireObjectCoercible = require('../internals/require-object-coercible');
|
---|
6 |
|
---|
7 | var ceil = Math.ceil;
|
---|
8 |
|
---|
9 | // `String.prototype.{ padStart, padEnd }` methods implementation
|
---|
10 | var createMethod = function (IS_END) {
|
---|
11 | return function ($this, maxLength, fillString) {
|
---|
12 | var S = toString(requireObjectCoercible($this));
|
---|
13 | var stringLength = S.length;
|
---|
14 | var fillStr = fillString === undefined ? ' ' : toString(fillString);
|
---|
15 | var intMaxLength = toLength(maxLength);
|
---|
16 | var fillLen, stringFiller;
|
---|
17 | if (intMaxLength <= stringLength || fillStr == '') return S;
|
---|
18 | fillLen = intMaxLength - stringLength;
|
---|
19 | stringFiller = repeat.call(fillStr, ceil(fillLen / fillStr.length));
|
---|
20 | if (stringFiller.length > fillLen) stringFiller = stringFiller.slice(0, fillLen);
|
---|
21 | return IS_END ? S + stringFiller : stringFiller + S;
|
---|
22 | };
|
---|
23 | };
|
---|
24 |
|
---|
25 | module.exports = {
|
---|
26 | // `String.prototype.padStart` method
|
---|
27 | // https://tc39.es/ecma262/#sec-string.prototype.padstart
|
---|
28 | start: createMethod(false),
|
---|
29 | // `String.prototype.padEnd` method
|
---|
30 | // https://tc39.es/ecma262/#sec-string.prototype.padend
|
---|
31 | end: createMethod(true)
|
---|
32 | };
|
---|