@bhsd/common 0.4.6 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +4 -3
- package/dist/index.js +7 -4
- package/dist/index.mjs +77 -0
- package/package.json +15 -3
package/dist/index.d.ts
CHANGED
|
@@ -33,7 +33,8 @@ export declare const loadScript: (src: string, globalConst: string, amd?: boolea
|
|
|
33
33
|
export declare const getObject: (key: string) => any;
|
|
34
34
|
export declare const setObject: (key: string, value: unknown) => void;
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
37
|
-
* @param
|
|
36
|
+
* 比较版本号
|
|
37
|
+
* @param version 版本号
|
|
38
|
+
* @param baseVersion 基础版本号
|
|
38
39
|
*/
|
|
39
|
-
export declare const
|
|
40
|
+
export declare const compareVersion: (version: string, baseVersion: string) => boolean;
|
package/dist/index.js
CHANGED
|
@@ -19,18 +19,17 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var src_exports = {};
|
|
20
20
|
__export(src_exports, {
|
|
21
21
|
CDN: () => CDN,
|
|
22
|
+
compareVersion: () => compareVersion,
|
|
22
23
|
getObject: () => getObject,
|
|
23
24
|
loadScript: () => loadScript,
|
|
24
25
|
normalizeTitle: () => normalizeTitle,
|
|
25
26
|
numToHex: () => numToHex,
|
|
26
|
-
parseVersion: () => parseVersion,
|
|
27
27
|
rawurldecode: () => rawurldecode,
|
|
28
28
|
setObject: () => setObject,
|
|
29
29
|
splitColors: () => splitColors
|
|
30
30
|
});
|
|
31
31
|
module.exports = __toCommonJS(src_exports);
|
|
32
32
|
const CDN = "https://testingcf.jsdelivr.net";
|
|
33
|
-
const hexColor = String.raw`#(?:[\da-f]{3,4}|(?:[\da-f]{2}){3,4})(?![\p{L}\d_])`, rgbValue = String.raw`(?:\d*\.)?\d+%?`, hue = String.raw`(?:\d*\.)?\d+(?:deg|grad|rad|turn)?`, rgbColor = String.raw`rgba?\(\s*(?:${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s+`)}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s*,\s*`)}(?:\s*,\s*${rgbValue})?`})\s*\)`, hslColor = String.raw`hsla?\(\s*(?:${String.raw`${hue}\s+${rgbValue}\s+${rgbValue}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${hue}${String.raw`\s*,\s*(?:\d*\.)?\d+%`.repeat(2)}(?:\s*,\s*${rgbValue})?`})\s*\)`, reFull = new RegExp(String.raw`(^|[^\p{L}\d_])(${hexColor}|${rgbColor}|${hslColor})`, "giu"), reRGB = new RegExp(String.raw`(^|[^\p{L}\d_])(${hexColor}|${rgbColor})`, "giu");
|
|
34
33
|
let span;
|
|
35
34
|
if (typeof document === "object") {
|
|
36
35
|
span = document.createElement("span");
|
|
@@ -46,7 +45,7 @@ const normalizeTitle = (title) => {
|
|
|
46
45
|
};
|
|
47
46
|
const numToHex = (d) => Math.round(d * 255).toString(16).padStart(2, "0");
|
|
48
47
|
const splitColors = (str, hsl = true) => {
|
|
49
|
-
const pieces = [], re = hsl ? reFull : reRGB;
|
|
48
|
+
const hexColor = String.raw`#(?:[\da-f]{3,4}|(?:[\da-f]{2}){3,4})(?![\p{L}\d_])`, rgbValue = String.raw`(?:\d*\.)?\d+%?`, hue = String.raw`(?:\d*\.)?\d+(?:deg|grad|rad|turn)?`, rgbColor = String.raw`rgba?\(\s*(?:${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s+`)}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s*,\s*`)}(?:\s*,\s*${rgbValue})?`})\s*\)`, hslColor = String.raw`hsla?\(\s*(?:${String.raw`${hue}\s+${rgbValue}\s+${rgbValue}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${hue}${String.raw`\s*,\s*(?:\d*\.)?\d+%`.repeat(2)}(?:\s*,\s*${rgbValue})?`})\s*\)`, reFull = new RegExp(String.raw`(^|[^\p{L}\d_])(${hexColor}|${rgbColor}|${hslColor})`, "giu"), reRGB = new RegExp(String.raw`(^|[^\p{L}\d_])(${hexColor}|${rgbColor})`, "giu"), pieces = [], re = hsl ? reFull : reRGB;
|
|
50
49
|
re.lastIndex = 0;
|
|
51
50
|
let mt = re.exec(str), lastIndex = 0;
|
|
52
51
|
while (mt) {
|
|
@@ -91,4 +90,8 @@ const getObject = (key) => JSON.parse(String(localStorage.getItem(key)));
|
|
|
91
90
|
const setObject = (key, value) => {
|
|
92
91
|
localStorage.setItem(key, JSON.stringify(value));
|
|
93
92
|
};
|
|
94
|
-
const parseVersion = (
|
|
93
|
+
const parseVersion = (version) => version.split(".", 3).map(Number);
|
|
94
|
+
const compareVersion = (version, baseVersion) => {
|
|
95
|
+
const [major, minor] = parseVersion(version), [baseMajor, baseMinor] = parseVersion(baseVersion);
|
|
96
|
+
return major > baseMajor || major === baseMajor && minor >= baseMinor;
|
|
97
|
+
};
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
const CDN = "https://testingcf.jsdelivr.net";
|
|
2
|
+
let span;
|
|
3
|
+
if (typeof document === "object") {
|
|
4
|
+
span = document.createElement("span");
|
|
5
|
+
}
|
|
6
|
+
const rawurldecode = (str) => decodeURIComponent(str.replace(/%(?![\da-f]{2})/giu, "%25"));
|
|
7
|
+
const normalizeTitle = (title) => {
|
|
8
|
+
const decoded = rawurldecode(title);
|
|
9
|
+
if (/[<>[\]|{}]/u.test(decoded)) {
|
|
10
|
+
return decoded;
|
|
11
|
+
}
|
|
12
|
+
span.innerHTML = decoded;
|
|
13
|
+
return span.textContent;
|
|
14
|
+
};
|
|
15
|
+
const numToHex = (d) => Math.round(d * 255).toString(16).padStart(2, "0");
|
|
16
|
+
const splitColors = (str, hsl = true) => {
|
|
17
|
+
const hexColor = String.raw`#(?:[\da-f]{3,4}|(?:[\da-f]{2}){3,4})(?![\p{L}\d_])`, rgbValue = String.raw`(?:\d*\.)?\d+%?`, hue = String.raw`(?:\d*\.)?\d+(?:deg|grad|rad|turn)?`, rgbColor = String.raw`rgba?\(\s*(?:${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s+`)}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${new Array(3).fill(rgbValue).join(String.raw`\s*,\s*`)}(?:\s*,\s*${rgbValue})?`})\s*\)`, hslColor = String.raw`hsla?\(\s*(?:${String.raw`${hue}\s+${rgbValue}\s+${rgbValue}(?:\s*\/\s*${rgbValue})?`}|${String.raw`${hue}${String.raw`\s*,\s*(?:\d*\.)?\d+%`.repeat(2)}(?:\s*,\s*${rgbValue})?`})\s*\)`, reFull = new RegExp(String.raw`(^|[^\p{L}\d_])(${hexColor}|${rgbColor}|${hslColor})`, "giu"), reRGB = new RegExp(String.raw`(^|[^\p{L}\d_])(${hexColor}|${rgbColor})`, "giu"), pieces = [], re = hsl ? reFull : reRGB;
|
|
18
|
+
re.lastIndex = 0;
|
|
19
|
+
let mt = re.exec(str), lastIndex = 0;
|
|
20
|
+
while (mt) {
|
|
21
|
+
const index = mt.index + mt[1].length;
|
|
22
|
+
if (index > lastIndex) {
|
|
23
|
+
pieces.push([str.slice(lastIndex, index), lastIndex, index, false]);
|
|
24
|
+
}
|
|
25
|
+
({ lastIndex } = re);
|
|
26
|
+
pieces.push([mt[2], index, lastIndex, true]);
|
|
27
|
+
mt = re.exec(str);
|
|
28
|
+
}
|
|
29
|
+
if (str.length > lastIndex) {
|
|
30
|
+
pieces.push([str.slice(lastIndex), lastIndex, str.length, false]);
|
|
31
|
+
}
|
|
32
|
+
return pieces;
|
|
33
|
+
};
|
|
34
|
+
const loadScript = (src, globalConst, amd) => new Promise((resolve) => {
|
|
35
|
+
const path = `${CDN}/${src}`;
|
|
36
|
+
let obj = globalThis;
|
|
37
|
+
for (const prop of globalConst.split(".")) {
|
|
38
|
+
obj = obj?.[prop];
|
|
39
|
+
}
|
|
40
|
+
if (obj) {
|
|
41
|
+
resolve();
|
|
42
|
+
} else if (amd && typeof define === "function" && "amd" in define) {
|
|
43
|
+
const requirejs = globalThis.require;
|
|
44
|
+
requirejs.config({ paths: { [globalConst]: path } });
|
|
45
|
+
requirejs([globalConst], (exports) => {
|
|
46
|
+
Object.assign(globalThis, { [globalConst]: exports });
|
|
47
|
+
resolve();
|
|
48
|
+
});
|
|
49
|
+
} else {
|
|
50
|
+
const script = document.createElement("script");
|
|
51
|
+
script.src = path;
|
|
52
|
+
script.onload = () => {
|
|
53
|
+
resolve();
|
|
54
|
+
};
|
|
55
|
+
document.head.append(script);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
const getObject = (key) => JSON.parse(String(localStorage.getItem(key)));
|
|
59
|
+
const setObject = (key, value) => {
|
|
60
|
+
localStorage.setItem(key, JSON.stringify(value));
|
|
61
|
+
};
|
|
62
|
+
const parseVersion = (version) => version.split(".", 3).map(Number);
|
|
63
|
+
const compareVersion = (version, baseVersion) => {
|
|
64
|
+
const [major, minor] = parseVersion(version), [baseMajor, baseMinor] = parseVersion(baseVersion);
|
|
65
|
+
return major > baseMajor || major === baseMajor && minor >= baseMinor;
|
|
66
|
+
};
|
|
67
|
+
export {
|
|
68
|
+
CDN,
|
|
69
|
+
compareVersion,
|
|
70
|
+
getObject,
|
|
71
|
+
loadScript,
|
|
72
|
+
normalizeTitle,
|
|
73
|
+
numToHex,
|
|
74
|
+
rawurldecode,
|
|
75
|
+
setObject,
|
|
76
|
+
splitColors
|
|
77
|
+
};
|
package/package.json
CHANGED
|
@@ -1,24 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bhsd/common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"license": "MIT",
|
|
5
|
+
"author": "Bhsd",
|
|
5
6
|
"files": [
|
|
6
7
|
"/dist/",
|
|
7
8
|
"*.cjs"
|
|
8
9
|
],
|
|
9
|
-
"
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.mjs",
|
|
14
|
+
"require": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./*.cjs": "./*.cjs",
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
10
19
|
"types": "./dist/index.d.ts",
|
|
11
20
|
"bin": {
|
|
12
21
|
"copy-dev": "bin/dev.cjs"
|
|
13
22
|
},
|
|
14
23
|
"scripts": {
|
|
15
|
-
"
|
|
24
|
+
"prepublishOnly": "npm run build",
|
|
25
|
+
"build": "tsc --emitDeclarationOnly && esbuild src/index.ts --charset=utf8 --target=es2023 --format=cjs --outfile=dist/index.js && esbuild src/index.ts --charset=utf8 --target=es2023 --format=esm --outfile=dist/index.mjs",
|
|
16
26
|
"lint:ts": "tsc --noEmit && eslint --cache .",
|
|
17
27
|
"lint": "npm run lint:ts"
|
|
18
28
|
},
|
|
19
29
|
"devDependencies": {
|
|
20
30
|
"@stylistic/eslint-plugin": "^2.11.0",
|
|
21
31
|
"@stylistic/stylelint-plugin": "^3.1.1",
|
|
32
|
+
"@types/mocha": "^10.0.10",
|
|
22
33
|
"@typescript-eslint/eslint-plugin": "^8.16.0",
|
|
23
34
|
"@typescript-eslint/parser": "^8.16.0",
|
|
24
35
|
"esbuild": "^0.24.0",
|
|
@@ -33,6 +44,7 @@
|
|
|
33
44
|
"eslint-plugin-regexp": "^2.6.0",
|
|
34
45
|
"eslint-plugin-unicorn": "^56.0.1",
|
|
35
46
|
"http-server": "^14.1.0",
|
|
47
|
+
"mocha": "^10.8.2",
|
|
36
48
|
"stylelint": "^16.11.0",
|
|
37
49
|
"stylelint-config-recommended": "^14.0.0",
|
|
38
50
|
"typescript": "^5.7.2"
|