limitedArray/lib/limitedarray.js

39 lines
670 B
JavaScript
Raw Normal View History

2017-08-18 10:11:44 +00:00
class LimitedArray {
constructor(size = 100) {
let _array = [];
let _limit = size;
this.push = item => {
_array.push(item);
if (_array.length > _limit)
_array.shift();
};
this.get = () => _array;
this.limit = size => {
_limit = size;
if (_array.length > _limit)
_array = _array.slice(_array.length - _limit);
};
this.length = () => _array.length;
this.add = items => {
_array = _array.concat(items);
if (_array.length > _limit)
_array = _array.slice(_array.length - _limit);
};
}
}
module.exports = LimitedArray;