From 34b9079a1b7a325febfb3728f632e636d402a372 Mon Sep 17 00:00:00 2001 From: John Factotum <50942278+johnfactotum@users.noreply.github.com> Date: Sat, 21 Dec 2024 04:55:35 +0800 Subject: [PATCH] Fix comparing CFIs with different number of `!`s Fixes #45 --- epubcfi.js | 2 +- tests/epubcfi-tests.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/epubcfi.js b/epubcfi.js index cd16d2b..1736091 100644 --- a/epubcfi.js +++ b/epubcfi.js @@ -167,7 +167,7 @@ export const compare = (a, b) => { || compare(collapse(a, true), collapse(b, true)) for (let i = 0; i < Math.max(a.length, b.length); i++) { - const p = a[i], q = b[i] + const p = a[i] ?? [], q = b[i] ?? [] const maxIndex = Math.max(p.length, q.length) - 1 for (let i = 0; i <= maxIndex; i++) { const x = p[i], y = q[i] diff --git a/tests/epubcfi-tests.js b/tests/epubcfi-tests.js index ad322b6..84d4d20 100644 --- a/tests/epubcfi-tests.js +++ b/tests/epubcfi-tests.js @@ -227,6 +227,8 @@ const XHTML = str => parser.parseFromString(str, 'application/xhtml+xml') '/6/4!/4/12', -1, ], + ['/6/4', '/6/4!/2', -1], + ['/6/4!/2', '/6/4!/2!/2', -1], ]) { const x = CFI.compare(a, b) console.assert(x === c, `compare ${a} and ${b}, expected ${c}, got ${x}`)