Skip to content

Commit

Permalink
NumberFormatUtils ios platform variant
Browse files Browse the repository at this point in the history
  • Loading branch information
kacper-mikolajczak committed Jul 10, 2024
1 parent baf7a5f commit 50b98d8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/libs/NumberFormatUtils/index.ios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type {ValueOf} from 'type-fest';
import intlPolyfill from '@libs/IntlPolyfill';
import memoize from '@libs/memoize';
import type CONST from '@src/CONST';

// On iOS, polyfills from `additionalSetup` are applied after memoization, which results in incorrect cache entry of `Intl.NumberFormat` (e.g. lacking `formatToParts` method).
// To fix this, we need to apply the polyfill manually before memoization.
// For further information, see: https://github.com/Expensify/App/pull/43868#issuecomment-2217637217
intlPolyfill();

const MemoizedNumberFormat = memoize(Intl.NumberFormat, {maxSize: 10});

function format(locale: ValueOf<typeof CONST.LOCALES>, number: number, options?: Intl.NumberFormatOptions): string {
return new MemoizedNumberFormat(locale, options).format(number);
}

function formatToParts(locale: ValueOf<typeof CONST.LOCALES>, number: number, options?: Intl.NumberFormatOptions): Intl.NumberFormatPart[] {
return new MemoizedNumberFormat(locale, options).formatToParts(number);
}

export {format, formatToParts};
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type {ValueOf} from 'type-fest';
import memoize from '@libs/memoize';
import type CONST from '@src/CONST';
import intlPolyfill from './IntlPolyfill';
import memoize from './memoize';

intlPolyfill();

const MemoizedNumberFormat = memoize(Intl.NumberFormat, {maxSize: 10});

Expand Down

0 comments on commit 50b98d8

Please sign in to comment.