From 5e76c5ea2f2c73983a6c6e04fb98f4f19a3372dd Mon Sep 17 00:00:00 2001 From: James Hadfield Date: Mon, 1 Mar 2021 16:23:08 +1300 Subject: [PATCH] Correctly format BCE dates BCE dates were correctly interpreted but incorrectly rendered due to a bug in the final string-prettying step. This would result in a tree with the correct layout and positioning, but incorrect labels ("-undefined"). This commit remedies this and adds a test. --- src/util/dateHelpers.js | 8 +++++++- test/dates.test.js | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/util/dateHelpers.js b/src/util/dateHelpers.js index 1d7fe5998..77b5a9727 100644 --- a/src/util/dateHelpers.js +++ b/src/util/dateHelpers.js @@ -153,7 +153,13 @@ export const prettifyDate = (unit, date) => { const stringDate = typeof date ==="number" ? numericToCalendar(date) : date instanceof Date ? dateToString(date) : date; - const [year, month, day] = stringDate.split("-"); + let year, month, day; + if (!stringDate.startsWith("-")) { + [year, month, day] = stringDate.split("-"); + } else { + [year, month, day] = stringDate.slice(1).split("-"); + year = `-${year}`; + } switch (unit) { case "CENTURY": // falls through case "DECADE": // falls through diff --git a/test/dates.test.js b/test/dates.test.js index 8ae5e9154..463a57f56 100644 --- a/test/dates.test.js +++ b/test/dates.test.js @@ -91,4 +91,5 @@ test("dates are prettified as expected", () => { expect(prettifyDate("YEAR", "2020-01-01")).toStrictEqual("2020"); expect(prettifyDate("MONTH", "2020-01-05")).toStrictEqual("2020-Jan-05"); expect(prettifyDate("MONTH", "2020-01-01")).toStrictEqual("2020-Jan"); + expect(prettifyDate("CENTURY", "-3000-01-01")).toStrictEqual("-3000"); // BCE });