my-idlers/node_modules/collect.js/dist/methods/pad.js

35 lines
678 B
JavaScript
Raw Normal View History

'use strict';
var clone = require('../helpers/clone');
module.exports = function pad(size, value) {
var abs = Math.abs(size);
var count = this.count();
if (abs <= count) {
return this;
}
var diff = abs - count;
var items = clone(this.items);
var isArray = Array.isArray(this.items);
var prepend = size < 0;
for (var iterator = 0; iterator < diff;) {
if (!isArray) {
if (items[iterator] !== undefined) {
diff += 1;
} else {
items[iterator] = value;
}
} else if (prepend) {
items.unshift(value);
} else {
items.push(value);
}
iterator += 1;
}
return new this.constructor(items);
};