index.js 489 B

1234567891011121314151617181920
  1. 'use strict';
  2. var repeat = require('repeat-string');
  3. module.exports = function padLeft(val, num, str) {
  4. var padding = '';
  5. var diff = num - val.length;
  6. // Breakpoints based on benchmarks to use the fastest approach
  7. // for the given number of zeros
  8. if (diff <= 5 && !str) {
  9. padding = '00000';
  10. } else if (diff <= 25 && !str) {
  11. padding = '000000000000000000000000000';
  12. } else {
  13. return val + repeat(str || '0', diff);
  14. }
  15. return val + padding.slice(0, diff);
  16. };