mirror of https://github.com/codefeathers/window
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.
36 lines
602 B
36 lines
602 B
7 years ago
|
class Deck extends Array {
|
||
7 years ago
|
|
||
|
constructor (...args) {
|
||
|
if(args.length === 1) return args;
|
||
|
return super(...args);
|
||
|
}
|
||
|
|
||
|
recurse (fn) {
|
||
|
let index = 0;
|
||
|
const call = (arr, acc = []) => {
|
||
|
if(arr[0]) {
|
||
|
const res = fn(index++, arr[0]);
|
||
|
return call(arr.slice(1), [ res, ...acc ]);
|
||
|
}
|
||
|
return acc;
|
||
|
};
|
||
|
return call(this);
|
||
|
}
|
||
|
|
||
|
findIndexAfter(index, predicate) {
|
||
|
const self = this;
|
||
|
let i = index + 1;
|
||
|
while(i < self.length) {
|
||
|
if(predicate(self[i])) return i;
|
||
|
i++;
|
||
|
}
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
empty () {
|
||
|
this.recurse(i => this.splice(i));
|
||
|
return this;
|
||
|
}
|
||
|
};
|
||
|
|
||
7 years ago
|
module.exports = Deck;
|