aboutsummaryrefslogtreecommitdiff
path: root/src/content/repositories/HintKeyRepository.ts
blob: 342b0032ec82c8ea306eb171e5d4fe70948dc674 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
export default interface HintKeyRepository {
  reset(charset: string): void;

  produce(): string;
}

const current: {
  charset: string;
  counter: number[];
} = {
  charset: "",
  counter: [],
};

export class HintKeyRepositoryImpl implements HintKeyRepository {
  reset(charset: string): void {
    if (charset.length === 0) {
      throw new TypeError("charset is empty");
    }
    current.charset = charset;
    current.counter = [];
  }

  produce(): string {
    if (current.charset === "") {
      throw new Error("charset is not set");
    }
    this.increment();

    return current.counter.map((x) => current.charset[x]).join("");
  }

  private increment(): void {
    const max = current.charset.length - 1;
    if (current.counter.every((x) => x === max)) {
      current.counter = new Array(current.counter.length + 1).fill(0);
      return;
    }

    current.counter.reverse();
    const len = current.charset.length;
    let num = current.counter.reduce((x, y, index) => x + y * len ** index) + 1;
    for (let i = 0; i < current.counter.length; ++i) {
      current.counter[i] = num % len;
      num = ~~(num / len);
    }
    current.counter.reverse();
  }
}