39 lines
670 B
JavaScript
39 lines
670 B
JavaScript
|
|
||
|
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;
|
||
|
|