From 0ac06dad2d6f7c04145cd3153f6d1bda94b02c36 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Tue, 10 Nov 2026 13:25:25 -0800 Subject: [PATCH] fix(shipwrecks): a reversed period is stored in the order the picker shows it, so changing one end keeps the other MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A period set as 1950 to 1900 (a link or the voice tool) showed as 1900–1950 but stayed reversed, so changing "From" to 1920 gave 1920 to 1900, shown as 1900–1920: the end the reader did not touch moved. --- src/layers/shipwrecks/period.js | 6 +++++- src/layers/shipwrecks/period.test.mjs | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/layers/shipwrecks/period.js b/src/layers/shipwrecks/period.js index f0cfa26e2..33b7885db 100644 --- a/src/layers/shipwrecks/period.js +++ b/src/layers/shipwrecks/period.js @@ -29,7 +29,7 @@ function yearParam(value) { /** * Merge a (partial) period request into the current one. `from: null` means * the first year in the data and `to: null` the latest; an invalid year keeps - * the current one. + * the current one, and a start after the end is stored as the reverse. * @param {{from?: number|string|null, to?: number|string|null}} params * @param {{from: number|null, to: number|null}} current * @returns {{from: number|null, to: number|null}} @@ -44,6 +44,10 @@ export function normalizePeriod( const year = yearParam(params[end]); if (year !== undefined) next[end] = year; } + // Stored in the order it is shown, so a later edit to one end (the picker + // sends only that end) changes the end the reader picked, not the other. + if (Number.isInteger(next.from) && Number.isInteger(next.to)) + if (next.from > next.to) [next.from, next.to] = [next.to, next.from]; return next; } diff --git a/src/layers/shipwrecks/period.test.mjs b/src/layers/shipwrecks/period.test.mjs index 7839a2840..80c9eb866 100644 --- a/src/layers/shipwrecks/period.test.mjs +++ b/src/layers/shipwrecks/period.test.mjs @@ -36,3 +36,23 @@ test('all years show every wreck; a narrower period hides undated ones', () => { assert.deepEqual(reversed, { from: 1900, to: 1950, all: false }); assert.equal(inPeriod(RECORDS[1], reversed), true); }); + +test('a reversed period is stored as shown, so editing one end keeps the other', () => { + const reversed = normalizePeriod({ from: 1950, to: 1900 }); + assert.deepEqual(reversed, { from: 1900, to: 1950 }); + // The picker shows 1900–1950; the reader changes "From" to 1920. + assert.deepEqual(normalizePeriod({ from: 1920 }, reversed), { + from: 1920, + to: 1950, + }); + // Moving "From" past "To" reads as the reverse, never an empty range. + assert.deepEqual(normalizePeriod({ from: 1960 }, { from: 1900, to: 1950 }), { + from: 1950, + to: 1960, + }); + // One open end stays open. + assert.deepEqual(normalizePeriod({ from: 1960 }, { from: null, to: null }), { + from: 1960, + to: null, + }); +});