Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,28 @@
|
||||
const MAX_FORMATTER_CACHE_SIZE = 128;
|
||||
|
||||
export function getCachedFormatter<TFormatter>(
|
||||
cache: Map<string, TFormatter>,
|
||||
locale: string,
|
||||
options: Record<string, unknown>,
|
||||
create: (locale: string, options: Record<string, unknown>) => TFormatter
|
||||
): TFormatter {
|
||||
const key = `${locale}|${stableOptionsKey(options)}`;
|
||||
const cached = cache.get(key);
|
||||
if (cached) return cached;
|
||||
|
||||
const formatter = create(locale, options);
|
||||
if (cache.size >= MAX_FORMATTER_CACHE_SIZE) {
|
||||
const oldestKey = cache.keys().next().value;
|
||||
if (oldestKey) cache.delete(oldestKey);
|
||||
}
|
||||
cache.set(key, formatter);
|
||||
return formatter;
|
||||
}
|
||||
|
||||
function stableOptionsKey(options: Record<string, unknown>): string {
|
||||
return JSON.stringify(
|
||||
Object.entries(options)
|
||||
.filter(([, value]) => value !== undefined)
|
||||
.sort(([left], [right]) => left.localeCompare(right))
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user