Skip to content

Commit 487d729

Browse files
authored
Drop mem dependency (#47)
1 parent 1e06515 commit 487d729

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

index.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
const execa = require('execa');
33
const lcid = require('lcid');
4-
const mem = require('mem');
54

65
const defaultOptions = {spawn: true};
76
const defaultLocale = 'en-US';
@@ -86,7 +85,14 @@ function normalise(input) {
8685
return input.replace(/_/, '-');
8786
}
8887

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+
9096
let locale;
9197

9298
try {
@@ -103,12 +109,18 @@ const osLocale = mem(async (options = defaultOptions) => {
103109
}
104110
} catch {}
105111

106-
return normalise(locale || defaultLocale);
107-
}, {cachePromiseRejection: false});
112+
const normalised = normalise(locale || defaultLocale);
113+
asyncCache.set(options.spawn, normalised);
114+
return normalised;
115+
};
108116

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+
}
110123

111-
module.exports.sync = mem((options = defaultOptions) => {
112124
let locale;
113125
try {
114126
const envLocale = getEnvLocale();
@@ -124,5 +136,7 @@ module.exports.sync = mem((options = defaultOptions) => {
124136
}
125137
} catch {}
126138

127-
return normalise(locale || defaultLocale);
128-
});
139+
const normalised = normalise(locale || defaultLocale);
140+
syncCache.set(options.spawn, normalised);
141+
return normalised;
142+
};

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
],
3737
"dependencies": {
3838
"execa": "^4.0.0",
39-
"lcid": "^3.0.0",
40-
"mem": "^5.0.0"
39+
"lcid": "^3.0.0"
4140
},
4241
"devDependencies": {
4342
"ava": "^2.1.0",

0 commit comments

Comments
 (0)