pad.js 582 B

12345678910111213141516
  1. "use strict";
  2. var toInteger = require("../../number/to-integer")
  3. , value = require("../../object/valid-value")
  4. , repeat = require("./repeat")
  5. , abs = Math.abs
  6. , max = Math.max;
  7. module.exports = function (fill/*, length*/) {
  8. var self = String(value(this)), sLength = self.length, length = arguments[1];
  9. length = isNaN(length) ? 1 : toInteger(length);
  10. fill = repeat.call(String(fill), abs(length));
  11. if (length >= 0) return fill.slice(0, max(0, length - sLength)) + self;
  12. return self + (sLength + length >= 0 ? "" : fill.slice(length + sLength));
  13. };