fix: sort stock and bond events by payment date
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { isValidPeriod, moscowDate } from "./calendar";
|
||||
import { compareDateDesc, isValidPeriod, moscowDate } from "./calendar";
|
||||
import { calendarDate } from "./calendar.schema";
|
||||
|
||||
describe("calendar dates", () => {
|
||||
@@ -32,4 +32,22 @@ describe("calendar dates", () => {
|
||||
expect(moscowDate(new Date("2026-12-31T20:59:59Z"))).toBe("2026-12-31");
|
||||
expect(moscowDate(new Date("2026-12-31T21:00:00Z"))).toBe("2027-01-01");
|
||||
});
|
||||
|
||||
it("compares dates newest first and keeps dateless rows last", () => {
|
||||
const dates = [null, "2026-01-01", "2026-03-01", "2026-02-01"];
|
||||
|
||||
expect(dates.toSorted(compareDateDesc)).toEqual([
|
||||
"2026-03-01",
|
||||
"2026-02-01",
|
||||
"2026-01-01",
|
||||
null,
|
||||
]);
|
||||
|
||||
expect(dates.toSorted((a, b) => compareDateDesc(b, a))).toEqual([
|
||||
null,
|
||||
"2026-01-01",
|
||||
"2026-02-01",
|
||||
"2026-03-01",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -16,6 +16,12 @@ const moscowFormatter = new Intl.DateTimeFormat("en-CA", {
|
||||
year: "numeric",
|
||||
});
|
||||
|
||||
// Newest first. Missing dates sort last in both directions, so a row without a
|
||||
// date never jumps to the top of a "past" list.
|
||||
function compareDateDesc(a: null | string, b: null | string) {
|
||||
return (b ?? "").localeCompare(a ?? "");
|
||||
}
|
||||
|
||||
function moscowDate(value: Date): string {
|
||||
const parts = moscowFormatter.formatToParts(value);
|
||||
|
||||
@@ -25,4 +31,4 @@ function moscowDate(value: Date): string {
|
||||
return `${part("year")}-${part("month")}-${part("day")}`;
|
||||
}
|
||||
|
||||
export { isValidPeriod, moscowDate };
|
||||
export { compareDateDesc, isValidPeriod, moscowDate };
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export { isValidPeriod, moscowDate } from "./calendar";
|
||||
export { compareDateDesc, isValidPeriod, moscowDate } from "./calendar";
|
||||
export { calendarDate } from "./calendar.schema";
|
||||
export {
|
||||
formatChartValue,
|
||||
|
||||
Reference in New Issue
Block a user