pad.js 580 B

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