source: imaps-frontend/node_modules/es-abstract/2023/StringPad.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.2 KB
RevLine 
[d565449]1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var callBound = require('call-bind/callBound');
6
7var ToLength = require('./ToLength');
8var ToString = require('./ToString');
9
10var $strSlice = callBound('String.prototype.slice');
11
12// https://262.ecma-international.org/11.0/#sec-stringpad
13
14module.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};
Note: See TracBrowser for help on using the repository browser.