Skip to content

Commit

Permalink
Remove intl-format-cache dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer committed Oct 17, 2024
1 parent 12da01b commit 7635c10
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 28 deletions.
19 changes: 3 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
"engines": {
"node": ">=18"
},
"dependencies": {
"intl-format-cache": "^4.2.27"
},
"devDependencies": {
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.5",
Expand Down
7 changes: 2 additions & 5 deletions src/lib/date-time-format/intl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
*/

import { Intl as IntlWithTemporal } from 'temporal-polyfill';
import memoizeFormatConstructor from 'intl-format-cache';

import { Locale } from '../../types/index.js';
import { memoize } from '../memoize.js';

/**
* Whether the `Intl` and `Intl.DateTimeFormat` APIs
Expand Down Expand Up @@ -61,10 +61,7 @@ export const isDateTimeStyleSupported = (() => {
}
})();

// @ts-expect-error intl-format-cache is bundled in a non-standard way.
export const getDateTimeFormat = memoizeFormatConstructor(
IntlWithTemporal.DateTimeFormat,
) as (
export const getDateTimeFormat = memoize(IntlWithTemporal.DateTimeFormat) as (
locales?: Locale | Locale[],
options?: Intl.DateTimeFormatOptions,
) => IntlWithTemporal.DateTimeFormat;
64 changes: 64 additions & 0 deletions src/lib/memoize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright 2024, SumUp Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Constructor<T> = new (...args: any[]) => T;

export function memoize<
Format,
FormatConstructor extends Constructor<Format>,
Args extends ConstructorParameters<FormatConstructor>,
>(IntlFormatConstructor: FormatConstructor) {
const cache = new Map<string, Format>();

return (...args: Args): Format => {
const cacheKey = getCacheKey(args);
if (!cacheKey) {
return new IntlFormatConstructor(...args);
}

const cachedFormat = cache.get(cacheKey);

if (cachedFormat) {
return cachedFormat;
}

const format = new IntlFormatConstructor(...args);

cache.set(cacheKey, format);

return format;
};
}

function getCacheKey(inputs: unknown[]) {
return JSON.stringify(
inputs.map((input) =>
isObject(input) ? sortObjectProperties(input) : input,
),
);
}

function isObject<T extends Record<string, unknown>>(
value: unknown,
): value is T {
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
}

function sortObjectProperties(obj: Record<string, unknown>) {
return Object.keys(obj)
.sort()
.map((key) => [key, obj[key]]);
}
6 changes: 2 additions & 4 deletions src/lib/number-format/intl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
* limitations under the License.
*/

import memoizeFormatConstructor from 'intl-format-cache';

import type { Locale } from '../../types/index.js';
import { memoize } from '../memoize.js';

/**
* Whether the `Intl` and `Intl.NumberFormat` APIs
Expand Down Expand Up @@ -44,8 +43,7 @@ export const isNumberFormatToPartsSupported = (() => {
}
})();

// @ts-expect-error intl-format-cache is bundled in a non-standard way.
export const getNumberFormat = memoizeFormatConstructor(Intl.NumberFormat) as (
export const getNumberFormat = memoize(Intl.NumberFormat) as (
locales?: Locale | Locale[],
options?: Intl.NumberFormatOptions,
) => Intl.NumberFormat;
Expand Down

0 comments on commit 7635c10

Please sign in to comment.