File tree 2 files changed +40
-2
lines changed
2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change 1
1
import getUserLocale from 'get-user-locale' ;
2
2
3
+ const formatterCache = new Map ( ) ;
4
+
3
5
function getFormatter ( options ) {
4
- return ( locale , date ) => date . toLocaleString ( locale || getUserLocale ( ) , options ) ;
6
+ return ( locale , date ) => {
7
+ const localeWithDefault = locale || getUserLocale ( ) ;
8
+
9
+ if ( ! formatterCache . has ( localeWithDefault ) ) {
10
+ formatterCache . set ( localeWithDefault , new Map ( ) ) ;
11
+ }
12
+
13
+ const formatterCacheLocale = formatterCache . get ( localeWithDefault ) ;
14
+
15
+ if ( ! formatterCacheLocale . has ( options ) ) {
16
+ formatterCacheLocale . set (
17
+ options ,
18
+ new Intl . DateTimeFormat ( localeWithDefault , options ) . format ,
19
+ ) ;
20
+ }
21
+
22
+ return formatterCacheLocale . get ( options ) ( date ) ;
23
+ } ;
5
24
}
6
25
7
26
/**
Original file line number Diff line number Diff line change 1
1
import getUserLocale from 'get-user-locale' ;
2
2
3
+ const formatterCache = new Map ( ) ;
4
+
3
5
function getFormatter ( options ) {
4
- return ( locale , date ) => date . toLocaleString ( locale || getUserLocale ( ) , options ) ;
6
+ return ( locale , date ) => {
7
+ const localeWithDefault = locale || getUserLocale ( ) ;
8
+
9
+ if ( ! formatterCache . has ( localeWithDefault ) ) {
10
+ formatterCache . set ( localeWithDefault , new Map ( ) ) ;
11
+ }
12
+
13
+ const formatterCacheLocale = formatterCache . get ( localeWithDefault ) ;
14
+
15
+ if ( ! formatterCacheLocale . has ( options ) ) {
16
+ formatterCacheLocale . set (
17
+ options ,
18
+ new Intl . DateTimeFormat ( localeWithDefault , options ) . format ,
19
+ ) ;
20
+ }
21
+
22
+ return formatterCacheLocale . get ( options ) ( date ) ;
23
+ } ;
5
24
}
6
25
7
26
/**
You can’t perform that action at this time.
0 commit comments