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.
35 lines
678 B
35 lines
678 B
4 years ago
|
'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);
|
||
|
};
|