source: imaps-frontend/node_modules/es-abstract/2024/StringPad.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 1.5 KB
RevLine 
[d565449]1'use strict';
2
3var $TypeError = require('es-errors/type');
[79a0317]4var callBound = require('call-bound');
5var isInteger = require('math-intrinsics/isInteger');
[d565449]6
7var $strSlice = callBound('String.prototype.slice');
8
9// https://262.ecma-international.org/15.0/#sec-stringpad
10
11module.exports = function StringPad(S, maxLength, fillString, placement) {
12 if (typeof S !== 'string') {
13 throw new $TypeError('Assertion failed: `S` must be a String');
14 }
15 if (!isInteger(maxLength) || maxLength < 0) {
16 throw new $TypeError('Assertion failed: `maxLength` must be a non-negative integer');
17 }
18 if (typeof fillString !== 'string') {
19 throw new $TypeError('Assertion failed: `fillString` must be a String');
20 }
21 if (placement !== 'start' && placement !== 'end' && placement !== 'START' && placement !== 'END') {
22 throw new $TypeError('Assertion failed: `placement` must be ~START~ or ~END~');
23 }
24
25 var stringLength = S.length; // step 1
26
27 if (maxLength <= stringLength) { return S; } // step 2
28
29 if (fillString === '') { return S; } // step 3
30
31 var fillLen = maxLength - stringLength; // step 4
32
33 // 5. Let _truncatedStringFiller_ be the String value consisting of repeated concatenations of _fillString_ truncated to length _fillLen_.
34 var truncatedStringFiller = '';
35 while (truncatedStringFiller.length < fillLen) {
36 truncatedStringFiller += fillString;
37 }
38 truncatedStringFiller = $strSlice(truncatedStringFiller, 0, fillLen);
39
40 if (placement === 'start' || placement === 'START') { return truncatedStringFiller + S; } // step 6
41
42 return S + truncatedStringFiller; // step 7
43};
Note: See TracBrowser for help on using the repository browser.