Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

string-pad.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. // https://github.com/tc39/proposal-string-pad-start-end
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var toLength = require('../internals/to-length');
  5. var toString = require('../internals/to-string');
  6. var $repeat = require('../internals/string-repeat');
  7. var requireObjectCoercible = require('../internals/require-object-coercible');
  8. var repeat = uncurryThis($repeat);
  9. var stringSlice = uncurryThis(''.slice);
  10. var ceil = Math.ceil;
  11. // `String.prototype.{ padStart, padEnd }` methods implementation
  12. var createMethod = function (IS_END) {
  13. return function ($this, maxLength, fillString) {
  14. var S = toString(requireObjectCoercible($this));
  15. var intMaxLength = toLength(maxLength);
  16. var stringLength = S.length;
  17. var fillStr = fillString === undefined ? ' ' : toString(fillString);
  18. var fillLen, stringFiller;
  19. if (intMaxLength <= stringLength || fillStr === '') return S;
  20. fillLen = intMaxLength - stringLength;
  21. stringFiller = repeat(fillStr, ceil(fillLen / fillStr.length));
  22. if (stringFiller.length > fillLen) stringFiller = stringSlice(stringFiller, 0, fillLen);
  23. return IS_END ? S + stringFiller : stringFiller + S;
  24. };
  25. };
  26. module.exports = {
  27. // `String.prototype.padStart` method
  28. // https://tc39.es/ecma262/#sec-string.prototype.padstart
  29. start: createMethod(false),
  30. // `String.prototype.padEnd` method
  31. // https://tc39.es/ecma262/#sec-string.prototype.padend
  32. end: createMethod(true)
  33. };