os-locale
Advanced tools
Comparing version 5.0.0 to 6.0.0
@@ -1,38 +0,38 @@ | ||
declare namespace osLocale { | ||
interface Options { | ||
/** | ||
Set to `false` to avoid spawning subprocesses and instead only resolve the locale from environment variables. | ||
export interface Options { | ||
/** | ||
Set to `false` to avoid spawning subprocesses and instead only resolve the locale from environment variables. | ||
@default true | ||
*/ | ||
readonly spawn?: boolean; | ||
} | ||
@default true | ||
*/ | ||
readonly spawn?: boolean; | ||
} | ||
declare const osLocale: { | ||
/** | ||
Get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)). | ||
/** | ||
Get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)). | ||
@returns The locale. | ||
@returns The locale. | ||
@example | ||
``` | ||
import osLocale = require('os-locale'); | ||
@example | ||
``` | ||
import {osLocale} from 'os-locale'; | ||
(async () => { | ||
console.log(await osLocale()); | ||
//=> 'en-US' | ||
})(); | ||
``` | ||
*/ | ||
(options?: osLocale.Options): Promise<string>; | ||
console.log(await osLocale()); | ||
//=> 'en-US' | ||
``` | ||
*/ | ||
export function osLocale(options?: Options): Promise<string>; | ||
/** | ||
Synchronously get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)). | ||
/** | ||
Synchronously get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)). | ||
@returns The locale. | ||
*/ | ||
sync(options?: osLocale.Options): string; | ||
}; | ||
@returns The locale. | ||
export = osLocale; | ||
@example | ||
``` | ||
import {osLocaleSync} from 'os-locale'; | ||
console.log(osLocaleSync()); | ||
//=> 'en-US' | ||
``` | ||
*/ | ||
export function osLocaleSync(options?: Options): string; |
54
index.js
@@ -1,5 +0,3 @@ | ||
'use strict'; | ||
const execa = require('execa'); | ||
const lcid = require('lcid'); | ||
const mem = require('mem'); | ||
import lcid from 'lcid'; | ||
import {exec, execSync} from './exec.js'; | ||
@@ -10,7 +8,7 @@ const defaultOptions = {spawn: true}; | ||
async function getStdOut(command, args) { | ||
return (await execa(command, args)).stdout; | ||
return (await exec(command, args)).stdout; | ||
} | ||
function getStdOutSync(command, args) { | ||
return execa.sync(command, args).stdout; | ||
return execSync(command, args).stdout; | ||
} | ||
@@ -23,7 +21,7 @@ | ||
function parseLocale(string) { | ||
const env = string.split('\n').reduce((env, definition) => { | ||
const env = {}; | ||
for (const definition of string.split('\n')) { | ||
const [key, value] = definition.split('='); | ||
env[key] = value.replace(/^"|"$/g, ''); | ||
return env; | ||
}, {}); | ||
} | ||
@@ -52,3 +50,3 @@ return getEnvLocale(env); | ||
getStdOut('defaults', ['read', '-globalDomain', 'AppleLocale']), | ||
getLocales() | ||
getLocales(), | ||
]); | ||
@@ -62,3 +60,3 @@ | ||
getStdOutSync('defaults', ['read', '-globalDomain', 'AppleLocale']), | ||
getLocalesSync() | ||
getLocalesSync(), | ||
); | ||
@@ -77,3 +75,3 @@ } | ||
const stdout = await getStdOut('wmic', ['os', 'get', 'locale']); | ||
const lcidCode = parseInt(stdout.replace('Locale', ''), 16); | ||
const lcidCode = Number.parseInt(stdout.replace('Locale', ''), 16); | ||
@@ -85,3 +83,3 @@ return lcid.from(lcidCode); | ||
const stdout = getStdOutSync('wmic', ['os', 'get', 'locale']); | ||
const lcidCode = parseInt(stdout.replace('Locale', ''), 16); | ||
const lcidCode = Number.parseInt(stdout.replace('Locale', ''), 16); | ||
@@ -95,3 +93,9 @@ return lcid.from(lcidCode); | ||
const osLocale = mem(async (options = defaultOptions) => { | ||
const cache = new Map(); | ||
export async function osLocale(options = defaultOptions) { | ||
if (cache.has(options.spawn)) { | ||
return cache.get(options.spawn); | ||
} | ||
let locale; | ||
@@ -111,10 +115,14 @@ | ||
} | ||
} catch (_) {} | ||
} catch {} | ||
return normalise(locale || defaultLocale); | ||
}, {cachePromiseRejection: false}); | ||
const normalised = normalise(locale || defaultLocale); | ||
cache.set(options.spawn, normalised); | ||
return normalised; | ||
} | ||
module.exports = osLocale; | ||
export function osLocaleSync(options = defaultOptions) { | ||
if (cache.has(options.spawn)) { | ||
return cache.get(options.spawn); | ||
} | ||
module.exports.sync = mem((options = defaultOptions) => { | ||
let locale; | ||
@@ -133,5 +141,7 @@ try { | ||
} | ||
} catch (_) {} | ||
} catch {} | ||
return normalise(locale || defaultLocale); | ||
}); | ||
const normalised = normalise(locale || defaultLocale); | ||
cache.set(options.spawn, normalised); | ||
return normalised; | ||
} |
{ | ||
"name": "os-locale", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "Get the system locale", | ||
@@ -13,15 +13,18 @@ "license": "MIT", | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=10" | ||
"node": ">=12.20" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd" | ||
"//test": "xo && ava && tsd", | ||
"test": "xo && tsd" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
"index.d.ts", | ||
"exec.js" | ||
], | ||
"keywords": [ | ||
"locale", | ||
"lang", | ||
"language", | ||
@@ -31,3 +34,2 @@ "system", | ||
"string", | ||
"str", | ||
"user", | ||
@@ -40,12 +42,10 @@ "country", | ||
"dependencies": { | ||
"execa": "^4.0.0", | ||
"lcid": "^3.0.0", | ||
"mem": "^5.0.0" | ||
"lcid": "^3.1.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^2.1.0", | ||
"ava": "^3.15.0", | ||
"proxyquire": "^2.1.3", | ||
"tsd": "^0.11.0", | ||
"xo": "^0.28.0" | ||
"tsd": "^0.17.0", | ||
"xo": "^0.42.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# os-locale [![Build Status](https://travis-ci.org/sindresorhus/os-locale.svg?branch=master)](https://travis-ci.org/sindresorhus/os-locale) | ||
# os-locale | ||
@@ -18,8 +18,6 @@ > Get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)) | ||
```js | ||
const osLocale = require('os-locale'); | ||
import {osLocale} from 'os-locale'; | ||
(async () => { | ||
console.log(await osLocale()); | ||
//=> 'en-US' | ||
})(); | ||
console.log(await osLocale()); | ||
//=> 'en-US' | ||
``` | ||
@@ -26,0 +24,0 @@ ## API |
8061
1
6
159
Yes
49
- Removedexeca@^4.0.0
- Removedmem@^5.0.0
- Removedcross-spawn@7.0.3(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedexeca@4.1.0(transitive)
- Removedget-stream@5.2.0(transitive)
- Removedhuman-signals@1.1.1(transitive)
- Removedis-stream@2.0.1(transitive)
- Removedisexe@2.0.0(transitive)
- Removedmap-age-cleaner@0.1.3(transitive)
- Removedmem@5.1.1(transitive)
- Removedmerge-stream@2.0.0(transitive)
- Removedmimic-fn@2.1.0(transitive)
- Removednpm-run-path@4.0.1(transitive)
- Removedonce@1.4.0(transitive)
- Removedonetime@5.1.2(transitive)
- Removedp-defer@1.0.0(transitive)
- Removedp-is-promise@2.1.0(transitive)
- Removedpath-key@3.1.1(transitive)
- Removedpump@3.0.0(transitive)
- Removedshebang-command@2.0.0(transitive)
- Removedshebang-regex@3.0.0(transitive)
- Removedsignal-exit@3.0.7(transitive)
- Removedstrip-final-newline@2.0.0(transitive)
- Removedwhich@2.0.2(transitive)
- Removedwrappy@1.0.2(transitive)
Updatedlcid@^3.1.1