1
1
'use strict' ;
2
2
const execa = require ( 'execa' ) ;
3
3
const lcid = require ( 'lcid' ) ;
4
- const mem = require ( 'mem' ) ;
5
4
6
5
const defaultOptions = { spawn : true } ;
7
6
const defaultLocale = 'en-US' ;
@@ -86,7 +85,14 @@ function normalise(input) {
86
85
return input . replace ( / _ / , '-' ) ;
87
86
}
88
87
89
- const osLocale = mem ( async ( options = defaultOptions ) => {
88
+ // Uses a map as a simple memoization technique without having to import `mem`
89
+ const asyncCache = new Map ( ) ;
90
+
91
+ module . exports = async ( options = defaultOptions ) => {
92
+ if ( asyncCache . has ( options . spawn ) ) {
93
+ return asyncCache . get ( options . spawn ) ;
94
+ }
95
+
90
96
let locale ;
91
97
92
98
try {
@@ -103,12 +109,18 @@ const osLocale = mem(async (options = defaultOptions) => {
103
109
}
104
110
} catch { }
105
111
106
- return normalise ( locale || defaultLocale ) ;
107
- } , { cachePromiseRejection : false } ) ;
112
+ const normalised = normalise ( locale || defaultLocale ) ;
113
+ asyncCache . set ( options . spawn , normalised ) ;
114
+ return normalised ;
115
+ } ;
108
116
109
- module . exports = osLocale ;
117
+ const syncCache = new Map ( ) ;
118
+
119
+ module . exports . sync = ( options = defaultOptions ) => {
120
+ if ( syncCache . has ( options . spawn ) ) {
121
+ return syncCache . get ( options . spawn ) ;
122
+ }
110
123
111
- module . exports . sync = mem ( ( options = defaultOptions ) => {
112
124
let locale ;
113
125
try {
114
126
const envLocale = getEnvLocale ( ) ;
@@ -124,5 +136,7 @@ module.exports.sync = mem((options = defaultOptions) => {
124
136
}
125
137
} catch { }
126
138
127
- return normalise ( locale || defaultLocale ) ;
128
- } ) ;
139
+ const normalised = normalise ( locale || defaultLocale ) ;
140
+ syncCache . set ( options . spawn , normalised ) ;
141
+ return normalised ;
142
+ } ;
0 commit comments