Skip to content

Commit

Permalink
Fix comparing CFIs with different number of !s
Browse files Browse the repository at this point in the history
Fixes #45
  • Loading branch information
johnfactotum committed Dec 20, 2024
1 parent 63a5dbf commit 34b9079
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion epubcfi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions tests/epubcfi-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down

0 comments on commit 34b9079

Please sign in to comment.