Skip to content

Commit

Permalink
fix: make filters and transforms more performant by avoiding re-creat…
Browse files Browse the repository at this point in the history
…ing objects
  • Loading branch information
ca057 committed Jan 4, 2025
1 parent 4b0ce26 commit 00bcd64
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,27 @@ export default async function (eleventyConfig) {
eleventyConfig.addPassthroughCopy("src/_fonts");

eleventyConfig.addFilter("blurhashColorRgb", (blurhash) => `rgb(${getBlurHashAverageColor(blurhash).join(",")})`);
eleventyConfig.addFilter("formatDate", (date, locale = "en-UK") => {
return Temporal.PlainDate.from(date).toLocaleString(locale, { calendar: "gregory", dateStyle: "long" });
});
eleventyConfig.addFilter("formatOrdinals", (count) => {
// copied from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules
const enOrdinalRules = new Intl.PluralRules("en-EN", { type: "ordinal" });

const suffixes = new Map([
["one", "st"],
["two", "nd"],
["few", "rd"],
["other", "th"],
]);
const formatOrdinals = (n) => {
const rule = enOrdinalRules.select(n);
const suffix = suffixes.get(rule);
return `${n}${suffix}`;
};
const dateCache = {};
eleventyConfig.addFilter(
"formatDate",
(date, locale = "en-UK") =>
dateCache[date] ||
(dateCache[date] = Temporal.PlainDate.from(date).toLocaleString(locale, {
calendar: "gregory",
dateStyle: "long",
})),
);

return formatOrdinals(count);
});
// copied from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules
const enOrdinalRules = new Intl.PluralRules("en-EN", { type: "ordinal" });
const suffixes = new Map([
["one", "st"],
["two", "nd"],
["few", "rd"],
["other", "th"],
]);
eleventyConfig.addFilter("formatOrdinals", (count) => `${count}${suffixes.get(enOrdinalRules.select(count))}`);

eleventyConfig.addPlugin(VentoPlugin);
await eleventyConfig.addPlugin(pluginMultipleFavicons, { configNamePattern: /_favicon\.json/ });
Expand Down

0 comments on commit 00bcd64

Please sign in to comment.