Skip to content

Instantly share code, notes, and snippets.

@reyronald
Created October 19, 2023 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reyronald/f42a92ce256cea910ca55e9c4966ab76 to your computer and use it in GitHub Desktop.
Save reyronald/f42a92ce256cea910ca55e9c4966ab76 to your computer and use it in GitHub Desktop.
import { convertLongMonthDayToShort } from "server/domain/client/convertLongMonthDayToShort";
describe("convertLongMonthDayToShort", () => {
it.each([
[undefined, undefined],
[null, null],
["1/1", "1/1"],
["8/31", "8/31"],
["3/31", "3/31"],
["4/30", "4/30"],
["12/31", "12/31"],
["01/31", "01/31"],
["02/28", "02/28"],
["08/31", "08/31"],
["07/31", "07/31"],
["10.31", "10/31"],
["09/30", "09/30"],
["03/31", "03/31"],
["07/01", "07/01"],
["10/31", "10/31"],
["06/30", "06/30"],
["05/31", "05/31"],
// ======
["04/30/2024", "04/30"],
["8/31/24", "8/31"],
["7/31/2024", "7/31"],
["7/31/2023", "7/31"],
["09/30/2023", "09/30"],
["09/30/2024", "09/30"],
["12/31/2023", "12/31"],
["12/12/2023", "12/12"],
// ======
["5-31-2024", "5/31"],
["12-31-2023", "12/31"],
// ======
["May 31", "5/31"],
["Oct 31", "10/31"],
["Dec 31", "12/31"],
// // ======
["Dec. 31", "12/31"],
// // ======
["January 31", "1/31"],
["February 28", "2/28"],
["March 30", "3/30"],
["March 31", "3/31"],
["April 30", "4/30"],
["June 30", "6/30"],
["July 31", "7/31"],
["August 31", "8/31"],
["September 30", "9/30"],
["October 31", "10/31"],
["November 30", "11/30"],
["December 31", "12/31"],
// // ======
["August 31st", "8/31"],
["December 31st ", "12/31"],
// // ======
["December31", "12/31"],
// // ======
["December, 31", "12/31"],
])("%s -> %s", (input, expected) => {
const actual = convertLongMonthDayToShort(input);
expect(actual).toBe(expected);
});
it.failing.each([
// ======
["9/31/2024", ""],
["11/31", ""],
["December", ""],
["April 2024", ""],
["December 2023", ""],
["September 30th", ""],
])("invalid: %s -> %s", (input, expected) => {
const actual = convertLongMonthDayToShort(input);
expect(actual).toBe(expected);
});
});
import { monthDaySchema } from "shared/validation/client";
export function convertLongMonthDayToShort(monthDayRaw: string | null | undefined) {
if (monthDayRaw == null) return monthDayRaw;
let monthDay = monthDayRaw.trim();
// Mar 31
// Mar. 31
// March 31
// March 31st
// March, 31
// March, 31st
// March31
const regex1 = /^(?<month>[A-Za-z]+)(\.?|,?)\s*(?<day>\d+)(\w)*$/;
if (regex1.test(monthDay)) {
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- guaranteed to not be null
const groups = monthDay.match(regex1)!.groups!;
const monthShort = groups.month.slice(0, 3);
const monthIndex = months.indexOf(monthShort) + 1;
monthDay = `${monthIndex}/${groups.day}`;
}
// 3/30
// 03/30
// 3.30
// 03.30
const regex2 = /^(?<month>\d{1,2})(\/|\.|-)(?<day>\d{1,2})\/?/;
if (regex2.test(monthDay)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- guaranteed to not be null
const groups = monthDay.match(regex2)!.groups!;
monthDay = `${groups.month}/${groups.day}`;
const isValid = monthDaySchema.isValidSync(monthDay);
if (isValid) return monthDay;
}
return "???";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment