aboutsummaryrefslogtreecommitdiff
path: root/api/sentence.ts
diff options
context:
space:
mode:
Diffstat (limited to 'api/sentence.ts')
-rw-r--r--api/sentence.ts44
1 files changed, 34 insertions, 10 deletions
diff --git a/api/sentence.ts b/api/sentence.ts
index 6b1a1e4..2311913 100644
--- a/api/sentence.ts
+++ b/api/sentence.ts
@@ -10,8 +10,8 @@ export default class Sentence extends APIBase {
protected breaks: Array<number> = [];
protected frozen = false;
- public ready: Promise<void>;
private _resolveReady: () => void = () => {};
+ public ready = new Promise<void>(res => this._resolveReady = res);
constructor(input: string) {
super();
@@ -20,7 +20,7 @@ export default class Sentence extends APIBase {
}
first(searchValue: RegExp | string): Word | undefined {
- return this.words[0]; // TODO: implement
+ return this.at(this.original.search(searchValue));
}
private async fetch() {
@@ -28,11 +28,15 @@ export default class Sentence extends APIBase {
}
private async updateWords() {
- this.words.clear();
+ this.words = [];
let token = 0;
let i = 0;
while (i < this.original.length) {
- this.words.push(new Word(this.query!.words[token]).withParent(await this.api));
+ 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;
@@ -42,7 +46,11 @@ export default class Sentence extends APIBase {
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).withParent(await this.api));
+ this.words.push(
+ new Word(remainder)
+ .withAPI(await this.api)
+ .withParent(this)
+ );
i += remainder.length;
}
}
@@ -53,8 +61,7 @@ export default class Sentence extends APIBase {
}, "");
}
- public async update() {
- if (this.frozen) return;
+ public async forceUpdate() {
// unresolve ready
this.ready = new Promise(res => this._resolveReady = res);
@@ -65,12 +72,29 @@ export default class Sentence extends APIBase {
// mark ready again
this._resolveReady();
+ }
+
+ public async update() {
+ if (this.frozen) return;
+ await this.forceUpdate();
}
- public at(term: string) {
- return this.original.indexOf(term);
+ 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();
@@ -82,6 +106,6 @@ export default class Sentence extends APIBase {
public async unfreeze() {
this.frozen = false;
- await this.update();
+ await this.forceUpdate();
}
}