aboutsummaryrefslogtreecommitdiff
path: root/api/sentence.ts
blob: 231191339b8f9f4e8179e4d4c235977c90cd01af (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { SearchSentenceResult } from "../search/types.ts";
import APIBase from "./base.ts";
import { JapaneseFormatter } from "./japanese.ts";
import Word from "./word.ts";

export default class Sentence extends APIBase {
  public words: Array<Word> = [];
	protected query?: SearchSentenceResult;
	protected original: string = "";
  protected breaks: Array<number> = [];
  protected frozen = false;

	private _resolveReady: () => void = () => {};
	public ready = new Promise<void>(res => this._resolveReady = res);

  constructor(input: string) {
    super();
    this.original = input;
    this.update();
  }

  first(searchValue: RegExp | string): Word | undefined {
		return this.at(this.original.search(searchValue));
  }

	private async fetch() {
		this.query = await (await this.api)["core"].search.sentence(this.original, { breaks: this.breaks });
	}

	private async updateWords() {
		this.words = [];
		let token = 0;
		let i = 0;
		while (i < this.original.length) {
			this.words.push(
				new Word(this.query!.words[token])
					.withAPI(await this.api)
					.withParent(this)
			);

			i += this.query!.words[token].source.length;
			if (i == this.original.length) break;
			token++;

			// continue if there are no unrecognized gaps between words
			if (this.query!.words[token]?.start == i) continue;
			var remainder = this.original.substring(i, this.query!.words[token]?.start);

			this.words.push(
				new Word(remainder)
					.withAPI(await this.api)
					.withParent(this)
			);
			i += remainder.length;
		}
	}

	furigana(format: JapaneseFormatter = "HTML"): string {
		return this.words.reduce((out, word) => {
			return out + word.furigana(format);
		}, "");
	}

	public async forceUpdate() {
    // unresolve ready
		this.ready = new Promise(res => this._resolveReady = res);

    // fetch sentence from DB
		await this.fetch();
    // parse words into Word
    await this.updateWords();

    // mark ready again
		this._resolveReady();
	}

  public async update() {
    if (this.frozen) return;
		await this.forceUpdate();
  }

  public indexOf(searchString: string, position: number = 0) {
    return this.original.indexOf(searchString, position);
  }

	public at(indentifier: number | string): Word | undefined {
		var index = typeof indentifier === "number" ? indentifier : this.indexOf(indentifier);
		if (index == -1) return;
		let wordIndex = 0;
		for (let i = 0; wordIndex < this.words.length; wordIndex++) {
			var length = this.words[wordIndex].length;
			if (i + length > index) break;
			i += length;
		}
		return this.words[wordIndex];
	}

  public async break(location: number) {
    this.breaks.push(location);
    await this.update();
  }

  public async freeze() {
    this.frozen = true;
  }

  public async unfreeze() {
    this.frozen = false;
    await this.forceUpdate();
  }
}