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/11/06 21:29:15 UTC

[incubator-annotator] 02/03: Create Chunk(Range) equality tests

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 c0138b1a4f870ca39fc7790e4e2a36ced36a3df4
Author: Gerben <ge...@treora.com>
AuthorDate: Fri Nov 6 22:18:47 2020 +0100

    Create Chunk(Range) equality tests
---
 packages/dom/src/chunker.ts          | 21 +++++++++++++++++++++
 packages/dom/src/text-quote/match.ts |  9 +--------
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/packages/dom/src/chunker.ts b/packages/dom/src/chunker.ts
index 82a72e3..c8e3015 100644
--- a/packages/dom/src/chunker.ts
+++ b/packages/dom/src/chunker.ts
@@ -25,6 +25,27 @@ import { ownerDocument } from "./owner-document";
 // data structure it came from (e.g. a DOM node).
 export interface Chunk<TData extends any> {
   readonly data: TData;
+  equals?(otherChunk: this): boolean;
+}
+
+export interface ChunkRange<TChunk extends Chunk<any>> {
+  startChunk: TChunk;
+  startIndex: number;
+  endChunk: TChunk;
+  endIndex: number;
+}
+
+export function chunkEquals(chunk1: Chunk<any>, chunk2: Chunk<any>): boolean {
+  return chunk1.equals ? chunk1.equals(chunk2) : chunk1 === chunk2;
+}
+
+export function chunkRangeEquals(range1: ChunkRange<any>, range2: ChunkRange<any>) {
+  return (
+    chunkEquals(range1.startChunk, range2.startChunk)
+    && chunkEquals(range1.endChunk, range2.endChunk)
+    && range1.startIndex === range2.startIndex
+    && range1.endIndex === range2.endIndex
+  );
 }
 
 // A Chunker lets one walk through the chunks of a document.
diff --git a/packages/dom/src/text-quote/match.ts b/packages/dom/src/text-quote/match.ts
index d18fbf8..112e054 100644
--- a/packages/dom/src/text-quote/match.ts
+++ b/packages/dom/src/text-quote/match.ts
@@ -19,14 +19,7 @@
  */
 
 import type { Matcher, TextQuoteSelector } from '@annotator/selector';
-import { TextNodeChunker, Chunk, Chunker } from '../chunker';
-
-export interface ChunkRange<TChunk extends Chunk<any>> {
-  startChunk: TChunk;
-  startIndex: number;
-  endChunk: TChunk;
-  endIndex: number;
-}
+import { TextNodeChunker, Chunk, Chunker, ChunkRange } from '../chunker';
 
 export function createTextQuoteSelectorMatcher(
   selector: TextQuoteSelector,