Skip to content

Commit

Permalink
Merge pull request #61 from jy95/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
jy95 authored May 4, 2024
2 parents a7762c3 + 874d196 commit df4336c
Showing 1 changed file with 52 additions and 52 deletions.
104 changes: 52 additions & 52 deletions src/utils/formatDatetimes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,33 @@ type MappedDate = {
hasMonths: boolean;
hasDays: boolean;
};
type RenderStrategy = "yearOnly" | "yearAndMonthOnly" | "dateOnly" | "dateTime";
type RenderFunction = (params: RenderParams) => string;
type RenderParams = {
config: Config;
date: Date;
};

const strategies = {
yearOnly: ({ config, date }) =>
new Intl.DateTimeFormat(config.language, { year: "numeric" }).format(date),
// e.g. 2024-03 => May 2024
yearAndMonthOnly: ({ config, date }) =>
new Intl.DateTimeFormat(config.language, {
year: "numeric",
month: "long",
}).format(date),
dateOnly: ({ config, date }) =>
new Intl.DateTimeFormat(config.language, {
...generateDateStyleFormatOptions(config.dateTimeFormatOptions),
}).format(date),
dateTime: ({ config, date }) =>
new Intl.DateTimeFormat(config.language, {
...generateDateStyleFormatOptions(config.dateTimeFormatOptions),
...generateTimeStyleFormatOptions(config.dateTimeFormatOptions),
}).format(date),
} satisfies Record<RenderStrategy, RenderFunction>;

// Function to clean up the params for dateStyle situation
// Note: dateStyle and timeStyle can be used with each other,
// but not with other date-time component options (e.g. weekday, hour, month, etc.).
function generateDateStyleFormatOptions(
options: Intl.DateTimeFormatOptions,
): Intl.DateTimeFormatOptions {
Expand All @@ -35,7 +58,6 @@ function generateDateStyleFormatOptions(
};
}

// Function to clean up the params for timeStyle situation
function generateTimeStyleFormatOptions(
options: Intl.DateTimeFormatOptions,
): Intl.DateTimeFormatOptions {
Expand All @@ -57,62 +79,40 @@ function generateTimeStyleFormatOptions(
};
}

function fromDateToMappedDate(datetime: string): MappedDate {
let date = new Date(datetime);

let hasTimePart = datetime.includes("T");
let hyphensCount = datetime.split("-").length - 1;
let hasMonths = hyphensCount >= 1;
let hasDays = hyphensCount >= 2;

return {
date,
hasTimePart,
hasMonths,
hasDays,
};
}

/**
* Generic function to map datetimes to user friendly date
* e.g. from 2018, 1973-06, 1905-08-23, 2015-02-07T13:28:17-05:00 or 2017-01-01T00:00:00.000Z
*/
export function formatDatetimes({ config, datetimes }: Args): string[] {
let options = config.dateTimeFormatOptions;

const entries: MappedDate[] = datetimes.map((datetime) => {
let date = new Date(datetime);

let hasTimePart = datetime.includes("T");
let hyphensCount = datetime.split("-").length - 1;
let hasMonths = hyphensCount >= 1;
let hasDays = hyphensCount >= 2;

return {
date,
hasTimePart,
hasMonths,
hasDays,
};
});
const entries = datetimes.map(fromDateToMappedDate);

// Time to do the magic
const result = entries.map(({ date, hasTimePart, hasMonths, hasDays }) => {
// If only year is defined, print it fully (e.g. 2024)
if (!hasMonths) {
let df1 = new Intl.DateTimeFormat(config.language, {
year: "numeric",
});
return df1.format(date);
}

// If only year and month are defined, print it nicely (e.g. 2024-03 => May 2024 )
if (!hasDays) {
let df2 = new Intl.DateTimeFormat(config.language, {
year: "numeric",
month: "long",
});
return df2.format(date);
}

// If only year / month and days are defined, print it according
if (!hasTimePart) {
let df3 = new Intl.DateTimeFormat(config.language, {
...generateDateStyleFormatOptions(options),
});
return df3.format(date);
}

// Otherwise, we have a full datetime
let df4 = new Intl.DateTimeFormat(config.language, {
...generateDateStyleFormatOptions(options),
...generateTimeStyleFormatOptions(options),
});
return df4.format(date);
const chosenStrategy: RenderStrategy = !hasMonths
? "yearOnly"
: !hasDays
? "yearAndMonthOnly"
: !hasTimePart
? "dateOnly"
: "dateTime";

return strategies[chosenStrategy]({ config, date });
});

return result;
Expand Down

0 comments on commit df4336c

Please sign in to comment.