diff --git a/sort-stories.js b/sort-stories.js index d7e703e..7ea875c 100644 --- a/sort-stories.js +++ b/sort-stories.js @@ -1,9 +1,78 @@ // Sort synthetic news stories newest first by their publication time. // A story is { id, title, published_at }, where published_at is an ISO 8601 timestamp with an explicit UTC offset. + +// ISO 8601 extended-format date-time with a mandatory UTC offset ("Z" or ±hh:mm). +// Seconds and fractional seconds are optional. +const ISO_WITH_OFFSET = + /^(\d{4})-(\d{2})-(\d{2})[Tt](\d{2}):(\d{2})(?::(\d{2})(?:[.,](\d+))?)?(?:([Zz])|([+-])(\d{2}):(\d{2}))$/; + +const DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + +function daysInMonth(year, month) { + const isLeap = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; + return month === 2 && isLeap ? 29 : DAYS_IN_MONTH[month - 1]; +} + +// Parses published_at into a comparable instant, or returns null if it is missing or invalid. +// The instant is { seconds, fraction }: whole seconds since the Unix epoch, plus the fractional-second +// digits with trailing zeros removed, so sub-millisecond precision is not lost. +// Date.parse is not used directly because it silently accepts offset-less (local-time) strings, +// non-ISO formats and impossible dates such as February 30. +function parseInstant(value) { + if (typeof value !== "string") return null; + const m = ISO_WITH_OFFSET.exec(value); + if (!m) return null; + + const [, y, mo, d, h, mi, s = "0", frac = "", zulu, sign, oh, om] = m; + const year = Number(y); + const month = Number(mo); + const day = Number(d); + const hour = Number(h); + const minute = Number(mi); + const second = Number(s); + const offsetHours = zulu ? 0 : Number(oh); + const offsetMinutes = zulu ? 0 : Number(om); + + if (month < 1 || month > 12) return null; + if (day < 1 || day > daysInMonth(year, month)) return null; + if (hour > 23 || minute > 59 || second > 59) return null; + if (offsetHours > 23 || offsetMinutes > 59) return null; + + const offsetSeconds = (sign === "-" ? -1 : 1) * (offsetHours * 3600 + offsetMinutes * 60); + // Date.UTC treats years 0-99 as 1900-1999, so set the full year explicitly. + const date = new Date(Date.UTC(2000, month - 1, day, hour, minute, second)); + date.setUTCFullYear(year); + const seconds = date.getTime() / 1000 - offsetSeconds; + + return { seconds, fraction: frac.replace(/0+$/, "") }; +} + +// Negative if instant a is earlier than b, positive if later, 0 if the same instant. +function compareInstants(a, b) { + if (a.seconds !== b.seconds) return a.seconds - b.seconds; + // Fraction digit strings without trailing zeros compare correctly lexicographically. + if (a.fraction === b.fraction) return 0; + return a.fraction < b.fraction ? -1 : 1; +} + export function sortStoriesNewestFirst(stories) { - return stories.slice().sort((a, b) => { - if (a.published_at < b.published_at) return 1; - if (a.published_at > b.published_at) return -1; - return 0; + const decorated = stories.map((story, index) => ({ + story, + index, + instant: parseInstant(story?.published_at), + })); + + decorated.sort((a, b) => { + if (a.instant && b.instant) { + const byInstant = compareInstants(b.instant, a.instant); // newest first + if (byInstant !== 0) return byInstant; + } else if (a.instant) { + return -1; // valid dates before missing/invalid ones + } else if (b.instant) { + return 1; + } + return a.index - b.index; // ties keep input order }); + + return decorated.map((entry) => entry.story); }