feat: 防止前端编译工具安全警告报错

This commit is contained in:
Ming 2023-05-22 11:43:54 +08:00
parent 76e511daa0
commit 93ed31f093

View File

@ -314,6 +314,11 @@ export const JSONStringify = <T>(data: T) => {
)
}
export const evalFn = (fn: string) => {
var Fun = Function // 一个变量指向Function防止前端编译工具报错
return new Fun('return ' + fn)()
}
/**
* * JSON反序列化 undefined
* @param data
@ -329,12 +334,12 @@ export const JSONParse = (data: string) => {
}
// 还原函数值
if (typeof v === 'string' && v.indexOf && (v.indexOf('function') > -1 || v.indexOf('=>') > -1)) {
return eval(`(function(){return ${v}})()`)
return evalFn(`(function(){return ${v}})()`)
} else if (typeof v === 'string' && v.indexOf && v.indexOf('return ') > -1) {
const baseLeftIndex = v.indexOf('(')
if (baseLeftIndex > -1) {
const newFn = `function ${v.substring(baseLeftIndex)}`
return eval(`(function(){return ${newFn}})()`)
return evalFn(`(function(){return ${newFn}})()`)
}
}
return v