1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 |
|
---|
5 | var callBound = require('call-bind/callBound');
|
---|
6 |
|
---|
7 | var ToLength = require('./ToLength');
|
---|
8 | var ToString = require('./ToString');
|
---|
9 |
|
---|
10 | var $strSlice = callBound('String.prototype.slice');
|
---|
11 |
|
---|
12 | // https://262.ecma-international.org/11.0/#sec-stringpad
|
---|
13 |
|
---|
14 | module.exports = function StringPad(O, maxLength, fillString, placement) {
|
---|
15 | if (placement !== 'start' && placement !== 'end') {
|
---|
16 | throw new $TypeError('Assertion failed: `placement` must be "start" or "end"');
|
---|
17 | }
|
---|
18 | var S = ToString(O);
|
---|
19 | var intMaxLength = ToLength(maxLength);
|
---|
20 | var stringLength = S.length;
|
---|
21 | if (intMaxLength <= stringLength) {
|
---|
22 | return S;
|
---|
23 | }
|
---|
24 | var filler = typeof fillString === 'undefined' ? ' ' : ToString(fillString);
|
---|
25 | if (filler === '') {
|
---|
26 | return S;
|
---|
27 | }
|
---|
28 | var fillLen = intMaxLength - stringLength;
|
---|
29 |
|
---|
30 | // the String value consisting of repeated concatenations of filler truncated to length fillLen.
|
---|
31 | var truncatedStringFiller = '';
|
---|
32 | while (truncatedStringFiller.length < fillLen) {
|
---|
33 | truncatedStringFiller += filler;
|
---|
34 | }
|
---|
35 | truncatedStringFiller = $strSlice(truncatedStringFiller, 0, fillLen);
|
---|
36 |
|
---|
37 | if (placement === 'start') {
|
---|
38 | return truncatedStringFiller + S;
|
---|
39 | }
|
---|
40 | return S + truncatedStringFiller;
|
---|
41 | };
|
---|