You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@annotator.apache.org by ge...@apache.org on 2020/10/15 22:36:42 UTC
[incubator-annotator] 02/04: use generic types
This is an automated email from the ASF dual-hosted git repository.
gerben pushed a commit to branch import-dom-seek
in repository https://gitbox.apache.org/repos/asf/incubator-annotator.git
commit 08a77bb5b0878150af72b7a0a0f2f5b578fa71cf
Author: Gerben <ge...@treora.com>
AuthorDate: Thu Oct 15 22:31:11 2020 +0200
use generic types
---
packages/dom/src/seek.ts | 31 +++++++++++++------------------
1 file changed, 13 insertions(+), 18 deletions(-)
diff --git a/packages/dom/src/seek.ts b/packages/dom/src/seek.ts
index 78102fa..455168c 100644
--- a/packages/dom/src/seek.ts
+++ b/packages/dom/src/seek.ts
@@ -12,18 +12,18 @@ interface TextBoundaryPointer extends BoundaryPointer{
readonly offsetInReferenceNode: number;
}
-interface Chunker {
- read1(): string;
+interface Chunker<T extends Iterable<any>> {
+ read1(): T;
}
-interface Seeker extends Chunker {
+interface Seeker<T extends Iterable<any>> extends Chunker<T> {
readonly position: number;
- read(length: number): string;
+ read(length: number): T;
seekBy(length: number): void;
seekTo(target: number): void;
}
-export class Seeker_ implements Seeker, TextBoundaryPointer {
+export class Seeker_ implements Seeker<string>, TextBoundaryPointer {
// The node containing our current text position.
get referenceNode(): Text {
// The NodeFilter will guarantee this is a Text node (except before the
@@ -177,8 +177,8 @@ function isText(node: Node): node is Text {
return node.nodeType === Node.TEXT_NODE;
}
-class CharSeeker implements Seeker, TextBoundaryPointer {
- constructor(public readonly raw: Seeker & TextBoundaryPointer) {
+class CharSeeker implements Seeker<String[]>, TextBoundaryPointer {
+ constructor(public readonly raw: Seeker<String> & TextBoundaryPointer) {
}
position = 0;
@@ -201,25 +201,20 @@ class CharSeeker implements Seeker, TextBoundaryPointer {
}
read1() {
- return this._read1().nextChunk;
- }
-
- private _read1() {
const nextChunk = this.raw.read1();
const characters = [...nextChunk];
this.position += characters.length;
- return { nextChunk, characters };
+ return characters;
}
- private _readOrSeekTo(target: number, read: true): string;
+ private _readOrSeekTo(target: number, read: true): string[];
private _readOrSeekTo(target: number, read: false): void;
- private _readOrSeekTo(target: number, read: boolean): string | void {
+ private _readOrSeekTo(target: number, read: boolean): string[] | void {
let characters: string[] = [];
- let result = '';
- let nextChunk;
+ let result: string[] = [];
while (this.position < target) {
- ({ nextChunk, characters } = this._read1());
- if (read) result += nextChunk;
+ characters = this.read1();
+ if (read) result = result.concat(characters);
}
const overshootInCodePoints = this.position - target;
const overshootInCodeUnits = characters.slice(overshootInCodePoints).join('').length;