@d-matrix/utils 1.28.4 → 1.29.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/dom.d.ts CHANGED
@@ -3,9 +3,16 @@
3
3
  */
4
4
  export declare function scrollToTop(element: Element | null | undefined): void;
5
5
  /**
6
- * Strips HTML tags from a given string and returns the plain text content.
6
+ * HTML字符串中提取纯文本内容
7
7
  *
8
- * @param {string} html - The HTML string to strip tags from.
9
- * @return {string} The plain text content of the HTML string.
8
+ * @param html - 需要处理的HTML字符串,可能为null或undefined
9
+ * @returns 提取的纯文本内容,如果无法提取则返回空字符串
10
10
  */
11
11
  export declare function strip(html: string): string;
12
+ /**
13
+ * 将HTML字符串中的RGB和RGBA颜色值转换为十六进制颜色值
14
+ *
15
+ * @param htmlStr - 包含RGB/RGBA颜色值的HTML字符串
16
+ * @returns 将所有RGB/RGBA颜色值转换为十六进制格式的HTML字符串
17
+ */
18
+ export declare function convertRgbToHexInHtml(htmlStr: string): string;
package/dist/dom.js CHANGED
@@ -16,12 +16,68 @@ export function scrollToTop(element) {
16
16
  }
17
17
  }
18
18
  /**
19
- * Strips HTML tags from a given string and returns the plain text content.
19
+ * HTML字符串中提取纯文本内容
20
20
  *
21
- * @param {string} html - The HTML string to strip tags from.
22
- * @return {string} The plain text content of the HTML string.
21
+ * @param html - 需要处理的HTML字符串,可能为null或undefined
22
+ * @returns 提取的纯文本内容,如果无法提取则返回空字符串
23
23
  */
24
24
  export function strip(html) {
25
25
  let doc = new DOMParser().parseFromString(html !== null && html !== void 0 ? html : '', 'text/html');
26
26
  return doc.body.textContent || '';
27
27
  }
28
+ /**
29
+ * 将HTML字符串中的RGB和RGBA颜色值转换为十六进制颜色值
30
+ *
31
+ * @param htmlStr - 包含RGB/RGBA颜色值的HTML字符串
32
+ * @returns 将所有RGB/RGBA颜色值转换为十六进制格式的HTML字符串
33
+ */
34
+ export function convertRgbToHexInHtml(htmlStr) {
35
+ // Fixed regex: added -? to match negative numbers (e.g., -50)
36
+ const rgbPattern = /rgba?\(\s*(-?\d+(?:\.\d+)?%?)\s*,\s*(-?\d+(?:\.\d+)?%?)\s*,\s*(-?\d+(?:\.\d+)?%?)\s*(?:,\s*(-?\d+(?:\.\d+)?))?\s*\)/gi;
37
+ return htmlStr.replace(rgbPattern, (match, rVal, gVal, bVal, aVal) => {
38
+ /**
39
+ * 规范化单个RGB分量值
40
+ *
41
+ * 此函数处理两种格式的输入:
42
+ * 1. 百分数格式(如 "50%")- 将其限制在0-100%范围内,然后转换为0-255范围
43
+ * 2. 数字格式(如 "128")- 将其限制在0-255范围内
44
+ *
45
+ * @param valueStr - RGB分量的字符串表示,可以是百分比或数值(例如 "50%" 或 "128")
46
+ * @returns 转换后的0-255范围内的整数值
47
+ */
48
+ const normalizeRgbComponent = (valueStr) => {
49
+ const isPercentage = valueStr.endsWith('%');
50
+ let rawValue = parseFloat(valueStr.replace('%', ''));
51
+ if (isPercentage) {
52
+ // Clamp percentage to 0-100% then convert to 0-255
53
+ rawValue = Math.max(0, Math.min(100, rawValue));
54
+ return Math.floor((rawValue / 100) * 255);
55
+ }
56
+ else {
57
+ // Clamp numeric RGB to 0-255 (fixes 300→255, -50→0)
58
+ rawValue = Math.max(0, Math.min(255, rawValue));
59
+ return Math.trunc(rawValue);
60
+ }
61
+ };
62
+ // 标准化alpha值(范围限制在0-1之间)
63
+ const normalizeAlpha = (alphaStr) => {
64
+ const alpha = parseFloat(alphaStr);
65
+ const clampedAlpha = Math.max(0, Math.min(1, alpha));
66
+ return Math.round(clampedAlpha * 255);
67
+ };
68
+ // 处理R/G/B组件
69
+ const r = normalizeRgbComponent(rVal);
70
+ const g = normalizeRgbComponent(gVal);
71
+ const b = normalizeRgbComponent(bVal);
72
+ // 转换为2位大写十六进制
73
+ const toHex = (num) => num.toString(16).padStart(2, '0').toUpperCase();
74
+ const hexRGB = `${toHex(r)}${toHex(g)}${toHex(b)}`;
75
+ // 处理alpha值(RGBA情况)
76
+ if (aVal) {
77
+ const alphaHex = toHex(normalizeAlpha(aVal));
78
+ return `#${hexRGB}${alphaHex}`;
79
+ }
80
+ // RGB情况(无alpha值)
81
+ return `#${hexRGB}`;
82
+ });
83
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
3
  "sideEffects": false,
4
- "version": "1.28.4",
4
+ "version": "1.29.0",
5
5
  "description": "A dozen of utils for Front-End Development",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {