From 52ed6b3a3b4ebff04941ab83d5e08255dd77c075 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 20 Mar 2023 12:16:57 -0400 Subject: [PATCH] Delays initialization of Intl.Collator until used --- src/system/string.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/system/string.ts b/src/system/string.ts index 188c895..effee71 100644 --- a/src/system/string.ts +++ b/src/system/string.ts @@ -4,8 +4,12 @@ import { CharCode } from '../constants'; export { fromBase64, base64 } from '@env/base64'; -const compareCollator = new Intl.Collator(undefined, { sensitivity: 'accent' }); +let compareCollator: Intl.Collator | undefined; export function compareIgnoreCase(a: string, b: string): 0 | -1 | 1 { + if (compareCollator == null) { + compareCollator = new Intl.Collator(undefined, { sensitivity: 'accent' }); + } + const result = compareCollator.compare(a, b); // Intl.Collator.compare isn't guaranteed to always return 1 or -1 on all platforms so normalize it return result === 0 ? 0 : result > 0 ? 1 : -1; @@ -18,8 +22,13 @@ export function equalsIgnoreCase(a: string | null | undefined, b: string | null return compareIgnoreCase(a, b) === 0; } -export const sortCollator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }); -export const sortCompare = sortCollator.compare; +let sortCollator: Intl.Collator | undefined; +export function sortCompare(x: string, y: string): number { + if (sortCollator == null) { + sortCollator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }); + } + return sortCollator.compare(x, y); +} export function compareSubstring( a: string,