Skip to content

Commit

Permalink
Drop mem dependency (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Mar 9, 2021
1 parent 1e06515 commit 487d729
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
30 changes: 22 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const execa = require('execa');
const lcid = require('lcid');
const mem = require('mem');

const defaultOptions = {spawn: true};
const defaultLocale = 'en-US';
Expand Down Expand Up @@ -86,7 +85,14 @@ function normalise(input) {
return input.replace(/_/, '-');
}

const osLocale = mem(async (options = defaultOptions) => {
// Uses a map as a simple memoization technique without having to import `mem`
const asyncCache = new Map();

module.exports = async (options = defaultOptions) => {
if (asyncCache.has(options.spawn)) {
return asyncCache.get(options.spawn);
}

let locale;

try {
Expand All @@ -103,12 +109,18 @@ const osLocale = mem(async (options = defaultOptions) => {
}
} catch {}

return normalise(locale || defaultLocale);
}, {cachePromiseRejection: false});
const normalised = normalise(locale || defaultLocale);
asyncCache.set(options.spawn, normalised);
return normalised;
};

module.exports = osLocale;
const syncCache = new Map();

module.exports.sync = (options = defaultOptions) => {
if (syncCache.has(options.spawn)) {
return syncCache.get(options.spawn);
}

module.exports.sync = mem((options = defaultOptions) => {
let locale;
try {
const envLocale = getEnvLocale();
Expand All @@ -124,5 +136,7 @@ module.exports.sync = mem((options = defaultOptions) => {
}
} catch {}

return normalise(locale || defaultLocale);
});
const normalised = normalise(locale || defaultLocale);
syncCache.set(options.spawn, normalised);
return normalised;
};
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
],
"dependencies": {
"execa": "^4.0.0",
"lcid": "^3.0.0",
"mem": "^5.0.0"
"lcid": "^3.0.0"
},
"devDependencies": {
"ava": "^2.1.0",
Expand Down

0 comments on commit 487d729

Please sign in to comment.