StringPaddingBuiltinsImpl.js 876 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var StringPad = require('./StringPad');
  4. var ToLength = require('./ToLength');
  5. var ToString = require('./ToString');
  6. // https://262.ecma-international.org/15.0/#sec-stringpaddingbuiltinsimpl
  7. module.exports = function StringPaddingBuiltinsImpl(O, maxLength, fillString, placement) {
  8. if (placement !== 'start' && placement !== 'end' && placement !== 'START' && placement !== 'END') {
  9. throw new $TypeError('Assertion failed: `placement` must be ~START~ or ~END~');
  10. }
  11. var S = ToString(O); // step 1
  12. var intMaxLength = ToLength(maxLength); // step 2
  13. var stringLength = S.length; // step 3
  14. if (intMaxLength <= stringLength) { return S; } // step 4
  15. var filler = typeof fillString === 'undefined' ? ' ' : ToString(fillString); // steps 5-6
  16. return StringPad(S, intMaxLength, filler, placement); // step 7
  17. };