_string-pad.js 744 B

12345678910111213141516
  1. // https://github.com/tc39/proposal-string-pad-start-end
  2. var toLength = require('./_to-length');
  3. var repeat = require('./_string-repeat');
  4. var defined = require('./_defined');
  5. module.exports = function (that, maxLength, fillString, left) {
  6. var S = String(defined(that));
  7. var stringLength = S.length;
  8. var fillStr = fillString === undefined ? ' ' : String(fillString);
  9. var intMaxLength = toLength(maxLength);
  10. if (intMaxLength <= stringLength || fillStr == '') return S;
  11. var fillLen = intMaxLength - stringLength;
  12. var stringFiller = repeat.call(fillStr, Math.ceil(fillLen / fillStr.length));
  13. if (stringFiller.length > fillLen) stringFiller = stringFiller.slice(0, fillLen);
  14. return left ? stringFiller + S : S + stringFiller;
  15. };