Skip to content

Commit

Permalink
refactoring - v1.0.20
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffy-g committed Jan 4, 2024
1 parent c82ad8b commit 9ec20aa
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 135 deletions.
15 changes: 9 additions & 6 deletions cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ let detector;
const isPromise = (o) => typeof o.then === "function";
let cacheLocal = "";
/**
* @type {NsOsLocale.TInternalLocaleDetectorSig}
* @template {true | void} IsAsync
* @template {Conditional<IsAsync, string, Promise<string>>} R
* @param {IsAsync=} async
* @returns {(options?: NsOsLocale.LocaleDetectorOptions) => R}
*/
const detectorBase = (async) => (options = {}) => {
/* eslint-disable indent */
Expand All @@ -42,7 +45,7 @@ let detector;
return (async ? Promise.resolve(cacheLocal) : cacheLocal);
}
const functions = localeGetters[+(!!async)];
/** @type {NsOsLocale.TInternalLocaleDetectorResult} */
/** @type {R} */
let locale;
/**
* @param {string} l
Expand All @@ -52,18 +55,18 @@ let detector;
const withCache = (l, mustPromise) => {
l = l.replace(/_/, "-");
cacheLocal = cache ? l : "";
return (mustPromise ? Promise.resolve(l) : l);
return /** @type {NsOsLocale.TInternalLocaleDetectorResult} */ (mustPromise ? Promise.resolve(l) : l);
};
const envLocale = getEnvLocale();
if (envLocale || !options.spawn) {
locale = purgeExtraToken(envLocale);
locale = /** @type {R} */ (purgeExtraToken(envLocale));
}
else {
let { platform } = process;
if (platform !== "win32" && platform !== "darwin") {
platform = "linux";
}
locale = functions[platform]();
locale = /** @type {R} */ (functions[platform]());
}
return (isPromise(locale) ? locale.then(withCache) : withCache(locale, async === true || void 0));
};
Expand All @@ -77,7 +80,7 @@ let detector;
enumerable: false,
},
version: {
value: "v1.0.19",
value: "v1.0.20",
enumerable: true,
},
});
Expand Down
4 changes: 2 additions & 2 deletions cjs/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ const [getUnixLocale, getUnixLocaleSync] = /** @type {(cmd: TLocalCmdToken) => T
* @see {@link module:lcid}
*/
const parseLCID = (result) => {
const lcidCode = parseInt(result.replace("Locale", ""), 16);
return lcid.from(lcidCode) || defaultLocale;
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 [
Expand Down
17 changes: 10 additions & 7 deletions esm/index.js → esm/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// @ts-ignore
///<reference path="../index.d.ts"/>
///<reference types="basic-types"/>
import * as lib from "./lib";
import * as lib from "./lib.mjs";
/**
* @typedef {(o: any) => o is Promise<any>} TPromiseChecker
*/
Expand All @@ -29,7 +29,10 @@ let detector;
const isPromise = (o) => typeof o.then === "function";
let cacheLocal = "";
/**
* @type {NsOsLocale.TInternalLocaleDetectorSig}
* @template {true | void} IsAsync
* @template {Conditional<IsAsync, string, Promise<string>>} R
* @param {IsAsync=} async
* @returns {(options?: NsOsLocale.LocaleDetectorOptions) => R}
*/
const detectorBase = (async) => (options = {}) => {
/* eslint-disable indent */
Expand All @@ -39,7 +42,7 @@ let detector;
return (async ? Promise.resolve(cacheLocal) : cacheLocal);
}
const functions = localeGetters[+(!!async)];
/** @type {NsOsLocale.TInternalLocaleDetectorResult} */
/** @type {R} */
let locale;
/**
* @param {string} l
Expand All @@ -49,18 +52,18 @@ let detector;
const withCache = (l, mustPromise) => {
l = l.replace(/_/, "-");
cacheLocal = cache ? l : "";
return (mustPromise ? Promise.resolve(l) : l);
return /** @type {NsOsLocale.TInternalLocaleDetectorResult} */ (mustPromise ? Promise.resolve(l) : l);
};
const envLocale = getEnvLocale();
if (envLocale || !options.spawn) {
locale = purgeExtraToken(envLocale);
locale = /** @type {R} */ (purgeExtraToken(envLocale));
}
else {
let { platform } = process;
if (platform !== "win32" && platform !== "darwin") {
platform = "linux";
}
locale = functions[platform]();
locale = /** @type {R} */ (functions[platform]());
}
return (isPromise(locale) ? locale.then(withCache) : withCache(locale, async === true || void 0));
};
Expand All @@ -74,7 +77,7 @@ let detector;
enumerable: false,
},
version: {
value: "v1.0.19",
value: "v1.0.20",
enumerable: true,
},
});
Expand Down
4 changes: 2 additions & 2 deletions esm/lib.js → esm/lib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ const [getUnixLocale, getUnixLocaleSync] = /** @type {(cmd: TLocalCmdToken) => T
* @see {@link module:lcid}
*/
const parseLCID = (result) => {
const lcidCode = parseInt(result.replace("Locale", ""), 16);
return lcid.from(lcidCode) || defaultLocale;
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 [
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "os-locale-s",
"version": "1.0.19",
"version": "1.0.20",
"description": "Its a light weight version that minimizes the dependency module of `os-locale`",
"private": false,
"main": "./cjs/index.js",
"module": "./esm/index.js",
"module": "./esm/index.mjs",
"types": "./index.d.ts",
"sideEffects": false,
"author": {
Expand Down Expand Up @@ -51,6 +51,7 @@
"lcid": "^4.0.1"
},
"devDependencies": {
"@types/node": "^20.10.6",
"jest": "29.7.0"
},
"scripts": {
Expand Down
Loading

0 comments on commit 9ec20aa

Please sign in to comment.