aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2017-08-17 22:33:46 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2017-08-17 22:33:46 +0900
commitf1c920e0003238a8f319fd29cd7aea068fd4f231 (patch)
tree558bd4781dc943811e1d38405840b8279d7ee1ef /src
parent13fb726332e9638cb3fafc477cf9fe641cb906ce (diff)
add HintKeyProducer
Diffstat (limited to 'src')
-rw-r--r--src/content/hint-key-producer.js33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/content/hint-key-producer.js b/src/content/hint-key-producer.js
new file mode 100644
index 0000000..8064afb
--- /dev/null
+++ b/src/content/hint-key-producer.js
@@ -0,0 +1,33 @@
+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();
+ }
+}