add Completion class

This commit is contained in:
Shin'ya Ueoka 2017-09-05 12:07:02 +09:00
parent 40dce0ca6e
commit 5e124e7561
2 changed files with 74 additions and 0 deletions

26
src/console/completion.js Normal file
View file

@ -0,0 +1,26 @@
export default class Completion {
constructor(completions) {
if (typeof completions.length !== 'number') {
throw new TypeError('completions does not have a length in number');
}
this.completions = completions
this.index = 0;
}
prev() {
if (this.completions.length === 0) {
return null;
}
this.index = (this.index + this.completions.length - 1) % this.completions.length
return this.completions[this.index];
}
next() {
if (this.completions.length === 0) {
return null;
}
let item = this.completions[this.index];
this.index = (this.index + 1) % this.completions.length
return item;
}
}