diff --git a/cjs/index.js b/cjs/index.js index 733f4ef..1e07367 100644 --- a/cjs/index.js +++ b/cjs/index.js @@ -49,12 +49,12 @@ let detector; /** * @param {string} l * @param {true} [mustPromise] - * @returns {NsOsLocale.TInternalLocaleDetectorResult} + * @returns {R} */ const withCache = (l, mustPromise) => { l = l.replace(/_/, "-"); cacheLocal = cache ? l : ""; - return /** @type {NsOsLocale.TInternalLocaleDetectorResult} */ (mustPromise ? Promise.resolve(l) : l); + return /** @type {R} */ (mustPromise ? Promise.resolve(l) : l); }; const envLocale = getEnvLocale(); if (envLocale || !options.spawn) { @@ -79,7 +79,7 @@ let detector; enumerable: false, }, version: { - value: "v1.0.22", + value: "v1.0.26", enumerable: true, }, }); diff --git a/cjs/lib.js b/cjs/lib.js index 3d98b45..d3e269d 100644 --- a/cjs/lib.js +++ b/cjs/lib.js @@ -47,13 +47,9 @@ const defaultLocale = "en_US"; function execCommand(options) { const { async, command, args } = options; if (async) { - /** @type {Promise} */ - const p = new Promise((resolve) => { - execFile(command, args, (err, stdout) => { - resolve(err || stdout); - }); - }); - return /** @type {R} */ (p); + return /** @type {R} */ (new Promise((resolve) => { + execFile(command, args, (err, stdout) => resolve(err || stdout)); + })); } try { return /** @type {R} */ (execFileSync(command, args, { encoding: "utf8" })); @@ -79,6 +75,7 @@ function validate(result, processor) { return processor ? processor(result) : result.trim(); } else { + // @ts-ignore console.info(result.message); } return defaultLocale; @@ -146,28 +143,32 @@ const [getAppleLocale, getAppleLocaleSync] = /** @type {TAppleLocaleFunctions} * /** * Locale detection for MAC OS */ - () => getSupportedLocale(validate(execCommand({ - command: cmd0, args: args0 - })), validate(execCommand({ - command: cmd1, args: args1 - }))) + () => getSupportedLocale(validate(execCommand({ command: cmd0, args: args0 })), validate(execCommand({ command: cmd1, args: args1 }))) ]; })("defaults", ["read", "-globalDomain", "AppleLocale"], "locale", ["-a"]); -const [getUnixLocale, getUnixLocaleSync] = /** @type {(cmd: TLocalCmdToken) => TAsyncSyncPair} */ ((cmd) => { +/** @type {(a: TLocalCmdToken, b: string[], p: (result: string) => string) => TAsyncSyncPair} */ +const emitGetters = (command, args, processor) => { return [ /** - * Locale detection for UNIX OS related + * Locale detection for windows or UNIX OS + * + * + `> locale` + * + `> wmic os get locale` + * * @async */ - async () => pet(parseLocale(await execCommand({ - async: true, command: cmd - }).then(validate))), + async () => validate(await execCommand({ command, args, async: true }), processor), /** - * Locale detection for UNIX OS related + * Locale detection for windows or UNIX OS */ - () => pet(parseLocale(validate(execCommand({ command: cmd })))) + () => validate(execCommand({ command, args }), processor) ]; -})("locale"); +}; +/** @type {Parameters[1]} */ +const unixProcessor = (result) => { + return pet(parseLocale(result)); +}; +const [getUnixLocale, getUnixLocaleSync] = emitGetters("locale", [], unixProcessor); /** * @param {string} result * @see {@link module:lcid} @@ -176,22 +177,7 @@ const parseLCID = (result) => { const lcidCode = +("0x" + result.replace(/Locale|\s/g, "")); return lcid.from(lcidCode) || /* istanbul ignore next */ defaultLocale; }; -const [getWinLocale, getWinLocaleSync] = /** @type {(a: TLocalCmdToken, b: string[]) => TAsyncSyncPair} */ ((command, args) => { - return [ - /** - * Locale detection for windows OS - * - * + `wmic os get locale` - * - * @async - */ - async () => validate(await execCommand({ command, args, async: true }), parseLCID), - /** - * Locale detection for windows OS - */ - () => validate(execCommand({ command, args }), parseLCID) - ]; -})("wmic", ["os", "get", "locale"]); +const [getWinLocale, getWinLocaleSync] = emitGetters("wmic", ["os", "get", "locale"], parseLCID); /** @type {[ TGetLocaleFunctions, TGetLocaleFunctions> ]} */ exports.localeGetters = [ { diff --git a/esm/index.mjs b/esm/index.mjs index 3ca0fa8..34caa8c 100644 --- a/esm/index.mjs +++ b/esm/index.mjs @@ -46,12 +46,12 @@ let detector; /** * @param {string} l * @param {true} [mustPromise] - * @returns {NsOsLocale.TInternalLocaleDetectorResult} + * @returns {R} */ const withCache = (l, mustPromise) => { l = l.replace(/_/, "-"); cacheLocal = cache ? l : ""; - return /** @type {NsOsLocale.TInternalLocaleDetectorResult} */ (mustPromise ? Promise.resolve(l) : l); + return /** @type {R} */ (mustPromise ? Promise.resolve(l) : l); }; const envLocale = getEnvLocale(); if (envLocale || !options.spawn) { @@ -76,7 +76,7 @@ let detector; enumerable: false, }, version: { - value: "v1.0.22", + value: "v1.0.26", enumerable: true, }, }); diff --git a/esm/lib.mjs b/esm/lib.mjs index d291f64..fa3b1f3 100644 --- a/esm/lib.mjs +++ b/esm/lib.mjs @@ -44,13 +44,9 @@ const defaultLocale = "en_US"; function execCommand(options) { const { async, command, args } = options; if (async) { - /** @type {Promise} */ - const p = new Promise((resolve) => { - execFile(command, args, (err, stdout) => { - resolve(err || stdout); - }); - }); - return /** @type {R} */ (p); + return /** @type {R} */ (new Promise((resolve) => { + execFile(command, args, (err, stdout) => resolve(err || stdout)); + })); } try { return /** @type {R} */ (execFileSync(command, args, { encoding: "utf8" })); @@ -76,6 +72,7 @@ function validate(result, processor) { return processor ? processor(result) : result.trim(); } else { + // @ts-ignore console.info(result.message); } return defaultLocale; @@ -143,28 +140,32 @@ const [getAppleLocale, getAppleLocaleSync] = /** @type {TAppleLocaleFunctions} * /** * Locale detection for MAC OS */ - () => getSupportedLocale(validate(execCommand({ - command: cmd0, args: args0 - })), validate(execCommand({ - command: cmd1, args: args1 - }))) + () => getSupportedLocale(validate(execCommand({ command: cmd0, args: args0 })), validate(execCommand({ command: cmd1, args: args1 }))) ]; })("defaults", ["read", "-globalDomain", "AppleLocale"], "locale", ["-a"]); -const [getUnixLocale, getUnixLocaleSync] = /** @type {(cmd: TLocalCmdToken) => TAsyncSyncPair} */ ((cmd) => { +/** @type {(a: TLocalCmdToken, b: string[], p: (result: string) => string) => TAsyncSyncPair} */ +const emitGetters = (command, args, processor) => { return [ /** - * Locale detection for UNIX OS related + * Locale detection for windows or UNIX OS + * + * + `> locale` + * + `> wmic os get locale` + * * @async */ - async () => pet(parseLocale(await execCommand({ - async: true, command: cmd - }).then(validate))), + async () => validate(await execCommand({ command, args, async: true }), processor), /** - * Locale detection for UNIX OS related + * Locale detection for windows or UNIX OS */ - () => pet(parseLocale(validate(execCommand({ command: cmd })))) + () => validate(execCommand({ command, args }), processor) ]; -})("locale"); +}; +/** @type {Parameters[1]} */ +const unixProcessor = (result) => { + return pet(parseLocale(result)); +}; +const [getUnixLocale, getUnixLocaleSync] = emitGetters("locale", [], unixProcessor); /** * @param {string} result * @see {@link module:lcid} @@ -173,22 +174,7 @@ const parseLCID = (result) => { const lcidCode = +("0x" + result.replace(/Locale|\s/g, "")); return lcid.from(lcidCode) || /* istanbul ignore next */ defaultLocale; }; -const [getWinLocale, getWinLocaleSync] = /** @type {(a: TLocalCmdToken, b: string[]) => TAsyncSyncPair} */ ((command, args) => { - return [ - /** - * Locale detection for windows OS - * - * + `wmic os get locale` - * - * @async - */ - async () => validate(await execCommand({ command, args, async: true }), parseLCID), - /** - * Locale detection for windows OS - */ - () => validate(execCommand({ command, args }), parseLCID) - ]; -})("wmic", ["os", "get", "locale"]); +const [getWinLocale, getWinLocaleSync] = emitGetters("wmic", ["os", "get", "locale"], parseLCID); /** @type {[ TGetLocaleFunctions, TGetLocaleFunctions> ]} */ export const localeGetters = [ { diff --git a/index.d.ts b/index.d.ts index fc75486..fbe0805 100644 --- a/index.d.ts +++ b/index.d.ts @@ -91,10 +91,10 @@ export declare interface LocaleDetector extends LocaleDetectorBase { export type TInternalLocaleDetectorSig = { bivarianceHack>>(async?: IsAsync): (options?: LocaleDetectorOptions) => R; }["bivarianceHack"]; -/** - * @internal - */ -export type TInternalLocaleDetectorResult = ReturnType>; +// /** +// * @internal +// */ +// export type TInternalLocaleDetectorResult = ReturnType>; export declare const osLocale: LocaleDetector; diff --git a/package.json b/package.json index 17b2051..be2d295 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "os-locale-s", - "version": "1.0.22", + "version": "1.0.26", "description": "Its a light weight version that minimizes the dependency module of `os-locale`", "private": false, "main": "./cjs/index.js", diff --git a/test.js b/test.js index 624e1c9..ad55b90 100644 --- a/test.js +++ b/test.js @@ -107,6 +107,7 @@ const printInfo = (prefix, locale, opt) => { * @param {TLocaleResultMap} localeResult * @param {TDetectorOptValues=} detectorOpt [spawn, cache] * @param {true=} async + * @returns {jest.ProvidesCallback} */ const emitCallback = (localeResult, detectorOpt, async) => async () => { const opt = makeOption(detectorOpt);