@bhsd/common 2.2.1 → 4.0.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/README.md +1 -1
- package/dist/color.d.ts +11 -0
- package/dist/color.js +28 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.js +15 -9
- package/dist/json_parse.d.ts +18 -0
- package/dist/json_parse.js +1 -1
- package/package.json +21 -20
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
# @bhsd/common
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@bhsd/common)
|
|
4
|
-
[](https://github.com/bhsd-harry/common/actions/workflows/codeql.yml)
|
package/dist/color.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { colorsNamed } from 'culori/fn';
|
|
2
|
+
/**
|
|
3
|
+
* 将颜色字符串转换为 RGBA 数组
|
|
4
|
+
* @param color 颜色字符串
|
|
5
|
+
*/
|
|
6
|
+
export declare const rgba: (color: string) => [number, number, number, number] | [];
|
|
7
|
+
/**
|
|
8
|
+
* 将颜色字符串转换为 HSLA 数组
|
|
9
|
+
* @param color 颜色字符串
|
|
10
|
+
*/
|
|
11
|
+
export declare const hsla: (color: string) => [number, number, number, number] | [];
|
package/dist/color.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useMode, modeRgb, modeHsl } from 'culori/fn';
|
|
2
|
+
export { colorsNamed } from 'culori/fn';
|
|
3
|
+
const parseRGB = /* #__PURE__ */ useMode(modeRgb), parseHSL = /* #__PURE__ */ useMode(modeHsl);
|
|
4
|
+
const normalizeRGB = (component) => Math.min(255, Math.max(0, Math.round(component * 255))), normalizeAlpha = (alpha) => Math.round(alpha * 1e3) / 1e3;
|
|
5
|
+
/**
|
|
6
|
+
* 将颜色字符串转换为 RGBA 数组
|
|
7
|
+
* @param color 颜色字符串
|
|
8
|
+
*/
|
|
9
|
+
export const rgba = (color) => {
|
|
10
|
+
const result = parseRGB(color.trim().toLowerCase());
|
|
11
|
+
if (!result) {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
const { r, g, b, alpha = 1 } = result;
|
|
15
|
+
return [...[r, g, b].map(normalizeRGB), normalizeAlpha(alpha)];
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* 将颜色字符串转换为 HSLA 数组
|
|
19
|
+
* @param color 颜色字符串
|
|
20
|
+
*/
|
|
21
|
+
export const hsla = (color) => {
|
|
22
|
+
const result = parseHSL(color.trim().toLowerCase());
|
|
23
|
+
if (!result) {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
const { h = 0, s, l, alpha = 1 } = result;
|
|
27
|
+
return [...[h, s * 100, l * 100].map(Math.round), normalizeAlpha(alpha)];
|
|
28
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -20,8 +20,9 @@ export declare const rawurldecode: (str: string) => string;
|
|
|
20
20
|
/**
|
|
21
21
|
* 将0~255之间的整数转换为十六进制
|
|
22
22
|
* @param d 0~255之间的整数
|
|
23
|
+
* @param len 十六进制数的最小长度,默认为2
|
|
23
24
|
*/
|
|
24
|
-
export declare const intToHex: (d: number) => string;
|
|
25
|
+
export declare const intToHex: (d: number, len?: number) => string;
|
|
25
26
|
/**
|
|
26
27
|
* 将0~1之间的数字转换为十六进制
|
|
27
28
|
* @param d 0~1之间的数字
|
|
@@ -30,9 +31,9 @@ export declare const numToHex: (d: number) => string;
|
|
|
30
31
|
/**
|
|
31
32
|
* 包含颜色时断开字符串
|
|
32
33
|
* @param str 字符串
|
|
33
|
-
* @param
|
|
34
|
+
* @param names 颜色名称列表,如果提供则也会匹配这些名称,否则只匹配十六进制代码和函数
|
|
34
35
|
*/
|
|
35
|
-
export declare const splitColors: (str: string,
|
|
36
|
+
export declare const splitColors: (str: string, names?: string[] | false) => [string, number, number, boolean][];
|
|
36
37
|
/**
|
|
37
38
|
* 清理内联样式中的`{`和`}`
|
|
38
39
|
* @param style 内联样式
|
package/dist/index.js
CHANGED
|
@@ -4,31 +4,36 @@ export const wmf = 'wiktionary|wiki(?:pedia|books|news|quote|source|versity|voya
|
|
|
4
4
|
* PHP的`rawurldecode`函数的JavaScript实现
|
|
5
5
|
* @param str 要解码的字符串
|
|
6
6
|
*/
|
|
7
|
-
export const rawurldecode = (str) =>
|
|
7
|
+
export const rawurldecode = (str) =>
|
|
8
|
+
// eslint-disable-next-line unicorn/prefer-string-replace-all
|
|
9
|
+
decodeURIComponent(str.replace(/%(?![\da-f]{2})/giu, '%25'));
|
|
8
10
|
/**
|
|
9
11
|
* 将0~255之间的整数转换为十六进制
|
|
10
12
|
* @param d 0~255之间的整数
|
|
13
|
+
* @param len 十六进制数的最小长度,默认为2
|
|
11
14
|
*/
|
|
12
|
-
export const intToHex = (d) => Math.round(d).toString(16).padStart(
|
|
15
|
+
export const intToHex = (d, len = 2) => Math.round(d).toString(16).padStart(len, '0');
|
|
13
16
|
/**
|
|
14
17
|
* 将0~1之间的数字转换为十六进制
|
|
15
18
|
* @param d 0~1之间的数字
|
|
16
19
|
*/
|
|
17
20
|
export const numToHex = (d) => intToHex(d * 255);
|
|
18
21
|
const regex = /* #__PURE__ */ (() => {
|
|
19
|
-
const
|
|
22
|
+
const wordChar = String.raw `\p{L}\p{N}_`, wordBoundary = `(?![${wordChar}])`, hex = String.raw `[\da-f]`, hexColor = `#(?:${hex}{3,4}|(?:${hex}{2}){3,4})${wordBoundary}`, num = String.raw `[+-]?(?:\d*\.)?\d+`, per = `${num}%`, rgbValue = `${per}?`, rgbArr = Array.from({ length: 3 }, () => rgbValue), space = String.raw `\s+`, comma = String.raw `\s*,\s*`, slash = String.raw `\s*\/\s*`, rgbColor = String.raw `rgba?\(\s*(?:${`${rgbArr.join(space)}(?:${slash}${rgbValue})?`}|${`${rgbArr.join(comma)}(?:${comma}${rgbValue})?`})\s*\)`, hslColor = String.raw `hsla?\(\s*${num}(?:deg|g?rad|turn)?(?:${`${(space + rgbValue).repeat(2)}(?:${slash}${rgbValue})?`}|${`${(comma + per).repeat(2)}(?:${comma}${rgbValue})?`})\s*\)`, source = `(^|[^${wordChar}])(${hexColor}|${rgbColor}|${hslColor}`;
|
|
20
23
|
return {
|
|
21
|
-
full: new RegExp(
|
|
22
|
-
|
|
24
|
+
full: new RegExp(`${source})`, 'giu'),
|
|
25
|
+
names: `${source}|(?:transparent|$1)${wordBoundary})`,
|
|
23
26
|
};
|
|
24
27
|
})();
|
|
25
28
|
/**
|
|
26
29
|
* 包含颜色时断开字符串
|
|
27
30
|
* @param str 字符串
|
|
28
|
-
* @param
|
|
31
|
+
* @param names 颜色名称列表,如果提供则也会匹配这些名称,否则只匹配十六进制代码和函数
|
|
29
32
|
*/
|
|
30
|
-
export const splitColors = (str,
|
|
31
|
-
const pieces = [], re =
|
|
33
|
+
export const splitColors = (str, names) => {
|
|
34
|
+
const pieces = [], re = Array.isArray(names) && names.length > 0
|
|
35
|
+
? new RegExp(regex.names.replace('$1', names.join('|')), 'giu')
|
|
36
|
+
: regex.full;
|
|
32
37
|
re.lastIndex = 0;
|
|
33
38
|
let mt = re.exec(str), lastIndex = 0;
|
|
34
39
|
while (mt) {
|
|
@@ -49,7 +54,7 @@ export const splitColors = (str, hsl = true) => {
|
|
|
49
54
|
* 清理内联样式中的`{`和`}`
|
|
50
55
|
* @param style 内联样式
|
|
51
56
|
*/
|
|
52
|
-
export const sanitizeInlineStyle = (style) => style.
|
|
57
|
+
export const sanitizeInlineStyle = (style) => style.replaceAll(/[{}]/gu, p => p === '{' ? '{' : '}')
|
|
53
58
|
.replace(/^[\s;]+/u, p => p.replaceAll(';', ' '));
|
|
54
59
|
export function getRegex(f) {
|
|
55
60
|
const map = new Map(), weakMap = new WeakMap();
|
|
@@ -161,6 +166,7 @@ export const lintJSON = (str) => {
|
|
|
161
166
|
return [];
|
|
162
167
|
}
|
|
163
168
|
const errors = lintJSONBase(str, json_parse);
|
|
169
|
+
// eslint-disable-next-line unicorn/prefer-at
|
|
164
170
|
return errors[errors.length - 1]?.severity === 'error' ? errors : [...errors, ...lintJSONNative(str)];
|
|
165
171
|
};
|
|
166
172
|
/**
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface JsonSyntaxError {
|
|
2
|
+
warnings: ExtendedJsonSyntaxError[];
|
|
3
|
+
severity?: "error" | "warning";
|
|
4
|
+
message?: string;
|
|
5
|
+
from?: number | null;
|
|
6
|
+
to?: number;
|
|
7
|
+
/** @deprecated */
|
|
8
|
+
position?: number;
|
|
9
|
+
}
|
|
10
|
+
export interface ExtendedJsonSyntaxError extends Omit<JsonSyntaxError, "warnings"> {
|
|
11
|
+
severity: "error" | "warning";
|
|
12
|
+
message: string;
|
|
13
|
+
line?: number | null;
|
|
14
|
+
column?: number | null;
|
|
15
|
+
endLine?: number;
|
|
16
|
+
endColumn?: number;
|
|
17
|
+
}
|
|
18
|
+
export declare const json_parse: (source: string) => void, jsonc_parse: (source: string) => void;
|
package/dist/json_parse.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bhsd/common",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"homepage": "https://github.com/bhsd-harry/common#readme",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/bhsd-harry/common/issues"
|
|
@@ -8,11 +8,14 @@
|
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"author": "Bhsd",
|
|
10
10
|
"files": [
|
|
11
|
-
"/dist
|
|
12
|
-
"/dist/index.d.ts"
|
|
11
|
+
"/dist/"
|
|
13
12
|
],
|
|
14
13
|
"main": "./dist/index.js",
|
|
15
14
|
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./dist/index.js",
|
|
17
|
+
"./color": "./dist/color.js"
|
|
18
|
+
},
|
|
16
19
|
"type": "module",
|
|
17
20
|
"sideEffects": false,
|
|
18
21
|
"scripts": {
|
|
@@ -21,25 +24,23 @@
|
|
|
21
24
|
"build": "tsc",
|
|
22
25
|
"lint:ts": "tsc --noEmit && eslint --cache .",
|
|
23
26
|
"lint": "npm run lint:ts",
|
|
24
|
-
"test": "
|
|
27
|
+
"test": "testUtil test/*.test.js"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"culori": "^4.0.0"
|
|
31
|
+
},
|
|
32
|
+
"peerDependenciesMeta": {
|
|
33
|
+
"culori": {
|
|
34
|
+
"optional": true
|
|
35
|
+
}
|
|
25
36
|
},
|
|
26
37
|
"devDependencies": {
|
|
27
|
-
"@bhsd/code-standard": "^
|
|
28
|
-
"@
|
|
29
|
-
"@
|
|
30
|
-
"@types/
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"esbuild": "^0.28.0",
|
|
34
|
-
"eslint": "^10.3.0",
|
|
35
|
-
"eslint-plugin-es-x": "^9.6.0",
|
|
36
|
-
"eslint-plugin-jsdoc": "^62.9.0",
|
|
37
|
-
"eslint-plugin-jsonc": "^3.1.2",
|
|
38
|
-
"eslint-plugin-promise": "^7.3.0",
|
|
39
|
-
"eslint-plugin-regexp": "^3.1.0",
|
|
40
|
-
"eslint-plugin-unicorn": "^64.0.0",
|
|
41
|
-
"eslint-plugin-yml": "^3.3.2",
|
|
42
|
-
"mocha": "^11.7.5",
|
|
38
|
+
"@bhsd/code-standard": "^3.1.0",
|
|
39
|
+
"@bhsd/lezer-json": "^2.1.0",
|
|
40
|
+
"@bhsd/test-util": "^3.1.0",
|
|
41
|
+
"@types/culori": "^4.0.1",
|
|
42
|
+
"culori": "^4.0.2",
|
|
43
|
+
"eslint": "^10.7.0",
|
|
43
44
|
"typescript": "^6.0.3"
|
|
44
45
|
},
|
|
45
46
|
"engines": {
|