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.

34 lines
678 B

4 years ago
  1. 'use strict';
  2. var clone = require('../helpers/clone');
  3. module.exports = function pad(size, value) {
  4. var abs = Math.abs(size);
  5. var count = this.count();
  6. if (abs <= count) {
  7. return this;
  8. }
  9. var diff = abs - count;
  10. var items = clone(this.items);
  11. var isArray = Array.isArray(this.items);
  12. var prepend = size < 0;
  13. for (var iterator = 0; iterator < diff;) {
  14. if (!isArray) {
  15. if (items[iterator] !== undefined) {
  16. diff += 1;
  17. } else {
  18. items[iterator] = value;
  19. }
  20. } else if (prepend) {
  21. items.unshift(value);
  22. } else {
  23. items.push(value);
  24. }
  25. iterator += 1;
  26. }
  27. return new this.constructor(items);
  28. };