Skip to content

Commit

Permalink
refactoring - v1.0.26
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffy-g committed Jan 12, 2024
1 parent d9d01c9 commit ca70134
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 83 deletions.
6 changes: 3 additions & 3 deletions cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -79,7 +79,7 @@ let detector;
enumerable: false,
},
version: {
value: "v1.0.22",
value: "v1.0.26",
enumerable: true,
},
});
Expand Down
58 changes: 22 additions & 36 deletions cjs/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,9 @@ const defaultLocale = "en_US";
function execCommand(options) {
const { async, command, args } = options;
if (async) {
/** @type {Promise<string | ExecFileException>} */
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" }));
Expand All @@ -79,6 +75,7 @@ function validate(result, processor) {
return processor ? processor(result) : result.trim();
}
else {
// @ts-ignore
console.info(result.message);
}
return defaultLocale;
Expand Down Expand Up @@ -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<typeof validate>[1]} */
const unixProcessor = (result) => {
return pet(parseLocale(result));
};
const [getUnixLocale, getUnixLocaleSync] = emitGetters("locale", [], unixProcessor);
/**
* @param {string} result
* @see {@link module:lcid}
Expand All @@ -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<string>, TGetLocaleFunctions<Promise<string>> ]} */
exports.localeGetters = [
{
Expand Down
6 changes: 3 additions & 3 deletions esm/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -76,7 +76,7 @@ let detector;
enumerable: false,
},
version: {
value: "v1.0.22",
value: "v1.0.26",
enumerable: true,
},
});
Expand Down
58 changes: 22 additions & 36 deletions esm/lib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,9 @@ const defaultLocale = "en_US";
function execCommand(options) {
const { async, command, args } = options;
if (async) {
/** @type {Promise<string | ExecFileException>} */
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" }));
Expand All @@ -76,6 +72,7 @@ function validate(result, processor) {
return processor ? processor(result) : result.trim();
}
else {
// @ts-ignore
console.info(result.message);
}
return defaultLocale;
Expand Down Expand Up @@ -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<typeof validate>[1]} */
const unixProcessor = (result) => {
return pet(parseLocale(result));
};
const [getUnixLocale, getUnixLocaleSync] = emitGetters("locale", [], unixProcessor);
/**
* @param {string} result
* @see {@link module:lcid}
Expand All @@ -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<string>, TGetLocaleFunctions<Promise<string>> ]} */
export const localeGetters = [
{
Expand Down
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ export declare interface LocaleDetector extends LocaleDetectorBase {
export type TInternalLocaleDetectorSig = {
bivarianceHack<IsAsync extends true | void, R extends Conditional<IsAsync, string, Promise<string>>>(async?: IsAsync): (options?: LocaleDetectorOptions) => R;
}["bivarianceHack"];
/**
* @internal
*/
export type TInternalLocaleDetectorResult = ReturnType<ReturnType<TInternalLocaleDetectorSig>>;
// /**
// * @internal
// */
// export type TInternalLocaleDetectorResult = ReturnType<ReturnType<TInternalLocaleDetectorSig>>;

export declare const osLocale: LocaleDetector;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ca70134

Please sign in to comment.