@lambo-design/shared 1.0.0-beta.295 → 1.0.0-beta.296
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/package.json +1 -1
- package/utils/json.js +22 -7
- package/utils/style.js +24 -24
- package/utils/transfer-queue.js +7 -7
package/package.json
CHANGED
package/utils/json.js
CHANGED
|
@@ -13,17 +13,32 @@ json.stringify = function (value) {
|
|
|
13
13
|
});
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
|
-
* 字符串(
|
|
17
|
-
*
|
|
18
|
-
* @
|
|
16
|
+
* 字符串(包含函数)转对象(函数可调用)
|
|
17
|
+
* 支持将形如 "function() { ... }" 的字符串还原为可执行的函数
|
|
18
|
+
* @param {string} value - 要解析的 JSON 字符串
|
|
19
|
+
* @returns {any} 解析后的 JavaScript 对象(包含可调用函数)
|
|
19
20
|
*/
|
|
20
21
|
json.parse = function (value) {
|
|
21
|
-
return JSON.parse(value,function(k,v){
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
return JSON.parse(value, function (k, v) {
|
|
23
|
+
// 检查值是否为字符串,并且包含 "function" 关键字
|
|
24
|
+
if (typeof v === 'string' && v.includes('function')) {
|
|
25
|
+
// 使用正则进一步判断是否为函数定义(避免将普通文本如 "type: 'user-function'" 误判)
|
|
26
|
+
const functionPattern = /^\s*function\s*\b|\bfunction\s*\(/;
|
|
27
|
+
if (functionPattern.test(v)) {
|
|
28
|
+
try {
|
|
29
|
+
// 通过立即执行函数的方式将字符串转为真正的函数
|
|
30
|
+
return eval("(function(){return " + v + "})()");
|
|
31
|
+
} catch (e) {
|
|
32
|
+
// 解析失败时给出中文警告,并返回原字符串
|
|
33
|
+
console.warn(`【JSON解析警告】键 "${k}" 的函数字符串解析失败:`, v);
|
|
34
|
+
console.warn(`错误信息:`, e.message);
|
|
35
|
+
return v; // 保留原始字符串,避免程序崩溃
|
|
36
|
+
}
|
|
37
|
+
}
|
|
24
38
|
}
|
|
39
|
+
// 非函数字符串或非字符串类型,直接返回原值
|
|
25
40
|
return v;
|
|
26
41
|
});
|
|
27
|
-
}
|
|
42
|
+
};
|
|
28
43
|
|
|
29
44
|
export default json;
|
package/utils/style.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
const SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g
|
|
2
|
-
const MOZ_HACK_REGEXP = /^moz([A-Z])/
|
|
3
|
-
|
|
4
|
-
function camelCase(name) {
|
|
5
|
-
return name
|
|
6
|
-
.replace(SPECIAL_CHARS_REGEXP, function (_, separator, letter, offset) {
|
|
7
|
-
return offset ? letter.toUpperCase() : letter
|
|
8
|
-
})
|
|
9
|
-
.replace(MOZ_HACK_REGEXP, 'Moz$1')
|
|
10
|
-
}
|
|
11
|
-
// getStyle
|
|
12
|
-
export function getStyle(element, styleName) {
|
|
13
|
-
if (!element || !styleName) return null
|
|
14
|
-
styleName = camelCase(styleName)
|
|
15
|
-
if (styleName === 'float') {
|
|
16
|
-
styleName = 'cssFloat'
|
|
17
|
-
}
|
|
18
|
-
try {
|
|
19
|
-
const computed = document.defaultView.getComputedStyle(element, '')
|
|
20
|
-
return element.style[styleName] || computed ? computed[styleName] : null
|
|
21
|
-
} catch (e) {
|
|
22
|
-
return element.style[styleName]
|
|
23
|
-
}
|
|
24
|
-
}
|
|
1
|
+
const SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g
|
|
2
|
+
const MOZ_HACK_REGEXP = /^moz([A-Z])/
|
|
3
|
+
|
|
4
|
+
function camelCase(name) {
|
|
5
|
+
return name
|
|
6
|
+
.replace(SPECIAL_CHARS_REGEXP, function (_, separator, letter, offset) {
|
|
7
|
+
return offset ? letter.toUpperCase() : letter
|
|
8
|
+
})
|
|
9
|
+
.replace(MOZ_HACK_REGEXP, 'Moz$1')
|
|
10
|
+
}
|
|
11
|
+
// getStyle
|
|
12
|
+
export function getStyle(element, styleName) {
|
|
13
|
+
if (!element || !styleName) return null
|
|
14
|
+
styleName = camelCase(styleName)
|
|
15
|
+
if (styleName === 'float') {
|
|
16
|
+
styleName = 'cssFloat'
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
const computed = document.defaultView.getComputedStyle(element, '')
|
|
20
|
+
return element.style[styleName] || computed ? computed[styleName] : null
|
|
21
|
+
} catch (e) {
|
|
22
|
+
return element.style[styleName]
|
|
23
|
+
}
|
|
24
|
+
}
|
package/utils/transfer-queue.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
let transferIndex = 1000
|
|
2
|
-
|
|
3
|
-
function transferIncrease() {
|
|
4
|
-
transferIndex++
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export { transferIndex, transferIncrease }
|
|
1
|
+
let transferIndex = 1000
|
|
2
|
+
|
|
3
|
+
function transferIncrease() {
|
|
4
|
+
transferIndex++
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export { transferIndex, transferIncrease }
|