From 4d28448920a6db9e06ed262b06caa00480a1b81c Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Sun, 24 Feb 2019 19:24:43 +0100 Subject: [PATCH] add compare method to SortKey and add tests --- src/matrix/storage/sortkey.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/matrix/storage/sortkey.js b/src/matrix/storage/sortkey.js index 25e45ce6..bfc15a5f 100644 --- a/src/matrix/storage/sortkey.js +++ b/src/matrix/storage/sortkey.js @@ -85,6 +85,15 @@ export default class SortKey { return minKey; } + compare(otherKey) { + const gapDiff = this.gapKey - otherKey.gapKey; + if (gapDiff === 0) { + return this.eventKey - otherKey.eventKey; + } else { + return gapDiff; + } + } + toString() { return `[${this.gapKey}/${this.eventKey}]`; } @@ -140,8 +149,7 @@ export function tests() { a.eventKey = 1; b.gapKey = 1; b.eventKey = 100000; - assert(b < a); - assert(a > b); + assert(a.compare(b) > 0); }, test_cmp_eventkey_second(assert) { @@ -151,12 +159,11 @@ export function tests() { a.eventKey = 100000; b.gapKey = 1; b.eventKey = 2; - assert(b.buffer < a.buffer); - assert(a.buffer > b.buffer); + assert(a.compare(b) > 0); }, test_cmp_max_larger_than_min(assert) { - assert(SortKey.minKey < SortKey.maxKey); + assert(SortKey.minKey.compare(SortKey.maxKey) < 0); }, test_cmp_gapkey_first_large(assert) { @@ -171,4 +178,4 @@ export function tests() { } }; } -//#endif \ No newline at end of file +//#endif