add Completion class
This commit is contained in:
parent
40dce0ca6e
commit
5e124e7561
2 changed files with 74 additions and 0 deletions
26
src/console/completion.js
Normal file
26
src/console/completion.js
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
48
test/console/completion.test.js
Normal file
48
test/console/completion.test.js
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
import { expect } from "chai";
|
||||||
|
import Completion from '../../src/console/completion';
|
||||||
|
|
||||||
|
describe('Completion class', () => {
|
||||||
|
describe('#constructor', () => {
|
||||||
|
it('creates new object by iterable items', () => {
|
||||||
|
new Completion([1,2,3,4,5]);
|
||||||
|
new Completion([]);
|
||||||
|
new Completion('hello');
|
||||||
|
new Completion('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('creates new object by iterable items', () => {
|
||||||
|
expect(() => new Completion({ key: 'value' })).to.throw(TypeError);
|
||||||
|
expect(() => new Completion(12345)).to.throw(TypeError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#next', () => {
|
||||||
|
it('complete next items', () => {
|
||||||
|
let completion = new Completion([3, 4, 5]);
|
||||||
|
expect(completion.next()).to.equal(3);
|
||||||
|
expect(completion.next()).to.equal(4);
|
||||||
|
expect(completion.next()).to.equal(5);
|
||||||
|
expect(completion.next()).to.equal(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns null when empty completions', () => {
|
||||||
|
let completion = new Completion([]);
|
||||||
|
expect(completion.next()).to.be.null;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#prev', () => {
|
||||||
|
it('complete prev items', () => {
|
||||||
|
let completion = new Completion([3, 4, 5]);
|
||||||
|
expect(completion.prev()).to.equal(5);
|
||||||
|
expect(completion.prev()).to.equal(4);
|
||||||
|
expect(completion.prev()).to.equal(3);
|
||||||
|
expect(completion.prev()).to.equal(5);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns null when empty completions', () => {
|
||||||
|
let completion = new Completion([]);
|
||||||
|
expect(completion.prev()).to.be.null;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Reference in a new issue