This repository has been archived on 2020-04-04. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Vim-Vixen/src/content/hint-key-producer.js
2017-09-17 08:59:12 +09:00

33 lines
790 B
JavaScript

export default class HintKeyProducer {
constructor(charset) {
if (charset.length === 0) {
throw new TypeError('charset is empty');
}
this.charset = charset;
this.counter = [];
}
produce() {
this.increment();
return this.counter.map(x => this.charset[x]).join('');
}
increment() {
let max = this.charset.length - 1;
if (this.counter.every(x => x === max)) {
this.counter = new Array(this.counter.length + 1).fill(0);
return;
}
this.counter.reverse();
let len = this.charset.length;
let num = this.counter.reduce((x, y, index) => x + y * len ** index) + 1;
for (let i = 0; i < this.counter.length; ++i) {
this.counter[i] = num % len;
num = ~~(num / len);
}
this.counter.reverse();
}
}