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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

34 lines
790 B

export default class HintKeyProducer {
constructor(charset) {
if (charset.length === 0) {
throw new TypeError('charset is empty');
}
this.charset = charset;
this.counter = [];
}
produce() {
this.increment();
7 years ago
return this.counter.map(x => this.charset[x]).join('');
}
increment() {
let max = this.charset.length - 1;
7 years ago
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;
7 years ago
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();
}
}