@bhsd/common 2.2.0 → 3.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 CHANGED
@@ -1,4 +1,4 @@
1
1
  # @bhsd/common
2
2
 
3
3
  [![npm version](https://badge.fury.io/js/@bhsd%2Fcommon.svg)](https://www.npmjs.com/package/@bhsd/common)
4
- [![License](https://img.shields.io/badge/License-MIT-brightgreen.svg)](LICENSE)
4
+ [![CodeQL](https://github.com/bhsd-harry/common/actions/workflows/codeql.yml/badge.svg)](https://github.com/bhsd-harry/common/actions/workflows/codeql.yml)
@@ -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
@@ -11,6 +11,7 @@ declare interface JsonError {
11
11
  position: number;
12
12
  }
13
13
  export type RegexGetter<T = string> = (s: T) => RegExp;
14
+ export * from './color.js';
14
15
  export declare const wmf = "wiktionary|wiki(?:pedia|books|news|quote|source|versity|voyage)";
15
16
  /**
16
17
  * PHP的`rawurldecode`函数的JavaScript实现
@@ -20,8 +21,9 @@ export declare const rawurldecode: (str: string) => string;
20
21
  /**
21
22
  * 将0~255之间的整数转换为十六进制
22
23
  * @param d 0~255之间的整数
24
+ * @param len 十六进制数的最小长度,默认为2
23
25
  */
24
- export declare const intToHex: (d: number) => string;
26
+ export declare const intToHex: (d: number, len?: number) => string;
25
27
  /**
26
28
  * 将0~1之间的数字转换为十六进制
27
29
  * @param d 0~1之间的数字
@@ -30,9 +32,9 @@ export declare const numToHex: (d: number) => string;
30
32
  /**
31
33
  * 包含颜色时断开字符串
32
34
  * @param str 字符串
33
- * @param hsl 是否包含 HSL
35
+ * @param names 颜色名称列表,如果提供则也会匹配这些名称,否则只匹配十六进制代码和函数
34
36
  */
35
- export declare const splitColors: (str: string, hsl?: boolean) => [string, number, number, boolean][];
37
+ export declare const splitColors: (str: string, names?: string[] | false) => [string, number, number, boolean][];
36
38
  /**
37
39
  * 清理内联样式中的`{`和`}`
38
40
  * @param style 内联样式
@@ -74,6 +76,6 @@ export declare const lintJSONC: (str: string) => JsonError[];
74
76
  /**
75
77
  * 获取字符串开头的空白字符数量
76
78
  * @param str 字符串
79
+ * @param re 其他正则表达式,默认为匹配非空白字符的正则表达式
77
80
  */
78
- export declare const numLeadingSpaces: (str: string) => number;
79
- export {};
81
+ export declare const numLeadingSpaces: (str: string, re?: RegExp) => number;
package/dist/index.js CHANGED
@@ -1,34 +1,40 @@
1
1
  import { json_parse, jsonc_parse } from './json_parse.js';
2
+ export * from './color.js';
2
3
  export const wmf = 'wiktionary|wiki(?:pedia|books|news|quote|source|versity|voyage)';
3
4
  /**
4
5
  * PHP的`rawurldecode`函数的JavaScript实现
5
6
  * @param str 要解码的字符串
6
7
  */
7
- export const rawurldecode = (str) => decodeURIComponent(str.replace(/%(?![\da-f]{2})/giu, '%25'));
8
+ export const rawurldecode = (str) =>
9
+ // eslint-disable-next-line unicorn/prefer-string-replace-all
10
+ decodeURIComponent(str.replace(/%(?![\da-f]{2})/giu, '%25'));
8
11
  /**
9
12
  * 将0~255之间的整数转换为十六进制
10
13
  * @param d 0~255之间的整数
14
+ * @param len 十六进制数的最小长度,默认为2
11
15
  */
12
- export const intToHex = (d) => Math.round(d).toString(16).padStart(2, '0');
16
+ export const intToHex = (d, len = 2) => Math.round(d).toString(16).padStart(len, '0');
13
17
  /**
14
18
  * 将0~1之间的数字转换为十六进制
15
19
  * @param d 0~1之间的数字
16
20
  */
17
21
  export const numToHex = (d) => intToHex(d * 255);
18
22
  const regex = /* #__PURE__ */ (() => {
19
- const hexColor = String.raw `#(?:[\da-f]{3,4}|(?:[\da-f]{2}){3,4})(?![\p{L}\p{N}_])`, 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*\)`;
23
+ 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
24
  return {
21
- full: new RegExp(String.raw `(^|[^\p{L}\p{N}_])(${hexColor}|${rgbColor}|${hslColor})`, 'giu'),
22
- rgb: new RegExp(String.raw `(^|[^\p{L}\p{N}_])(${hexColor}|${rgbColor})`, 'giu'),
25
+ full: new RegExp(`${source})`, 'giu'),
26
+ names: `${source}|(?:transparent|$1)${wordBoundary})`,
23
27
  };
24
28
  })();
25
29
  /**
26
30
  * 包含颜色时断开字符串
27
31
  * @param str 字符串
28
- * @param hsl 是否包含 HSL
32
+ * @param names 颜色名称列表,如果提供则也会匹配这些名称,否则只匹配十六进制代码和函数
29
33
  */
30
- export const splitColors = (str, hsl = true) => {
31
- const pieces = [], re = regex[hsl ? 'full' : 'rgb'];
34
+ export const splitColors = (str, names) => {
35
+ const pieces = [], re = Array.isArray(names) && names.length > 0
36
+ ? new RegExp(regex.names.replace('$1', names.join('|')), 'giu')
37
+ : regex.full;
32
38
  re.lastIndex = 0;
33
39
  let mt = re.exec(str), lastIndex = 0;
34
40
  while (mt) {
@@ -49,7 +55,7 @@ export const splitColors = (str, hsl = true) => {
49
55
  * 清理内联样式中的`{`和`}`
50
56
  * @param style 内联样式
51
57
  */
52
- export const sanitizeInlineStyle = (style) => style.replace(/[{}]/gu, p => p === '{' ? '{' : '}')
58
+ export const sanitizeInlineStyle = (style) => style.replaceAll(/[{}]/gu, p => p === '{' ? '{' : '}')
53
59
  .replace(/^[\s;]+/u, p => p.replaceAll(';', ' '));
54
60
  export function getRegex(f) {
55
61
  const map = new Map(), weakMap = new WeakMap();
@@ -161,6 +167,7 @@ export const lintJSON = (str) => {
161
167
  return [];
162
168
  }
163
169
  const errors = lintJSONBase(str, json_parse);
170
+ // eslint-disable-next-line unicorn/prefer-at
164
171
  return errors[errors.length - 1]?.severity === 'error' ? errors : [...errors, ...lintJSONNative(str)];
165
172
  };
166
173
  /**
@@ -171,5 +178,6 @@ export const lintJSONC = (str) => str.trim() ? lintJSONBase(str, jsonc_parse) :
171
178
  /**
172
179
  * 获取字符串开头的空白字符数量
173
180
  * @param str 字符串
181
+ * @param re 其他正则表达式,默认为匹配非空白字符的正则表达式
174
182
  */
175
- export const numLeadingSpaces = (str) => str.search(/\S|$/u);
183
+ export const numLeadingSpaces = (str, re = /\S|$/u) => str.search(re);
@@ -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;
@@ -392,4 +392,4 @@ const factory = (jsonc) => {
392
392
  }
393
393
  };
394
394
  };
395
- export const json_parse = /* @__PURE__ */ factory(), jsonc_parse = /* @__PURE__ */ factory(true);
395
+ export const json_parse = /* #__PURE__ */ factory(), jsonc_parse = /* #__PURE__ */ factory(true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bhsd/common",
3
- "version": "2.2.0",
3
+ "version": "3.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,8 +8,7 @@
8
8
  "license": "MIT",
9
9
  "author": "Bhsd",
10
10
  "files": [
11
- "/dist/*.js",
12
- "/dist/index.d.ts"
11
+ "/dist/*"
13
12
  ],
14
13
  "main": "./dist/index.js",
15
14
  "types": "./dist/index.d.ts",
@@ -23,23 +22,18 @@
23
22
  "lint": "npm run lint:ts",
24
23
  "test": "mocha"
25
24
  },
25
+ "dependencies": {
26
+ "culori": "^4.0.2"
27
+ },
26
28
  "devDependencies": {
27
- "@bhsd/code-standard": "^2.4.0",
28
- "@eslint-community/eslint-plugin-eslint-comments": "^4.7.1",
29
- "@stylistic/eslint-plugin": "^5.10.0",
29
+ "@bhsd/code-standard": "^2.6.2",
30
+ "@bhsd/lezer-json": "^2.1.0",
31
+ "@types/culori": "^4.0.1",
30
32
  "@types/mocha": "^10.0.10",
31
- "@typescript-eslint/eslint-plugin": "^8.59.1",
32
- "@typescript-eslint/parser": "^8.59.1",
33
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",
34
+ "eslint": "^10.4.1",
35
+ "globals": "^17.6.0",
36
+ "mocha": "^11.7.6",
43
37
  "typescript": "^6.0.3"
44
38
  },
45
39
  "engines": {