From 412192a05559e8534d917f265f3c06edc8167ed2 Mon Sep 17 00:00:00 2001 From: zsvg <8680782@qq.com> Date: Mon, 8 May 2023 20:54:53 +0800 Subject: [PATCH] =?UTF-8?q?=20=E5=90=8C=E6=AD=A5vue-next-admin=E6=9C=80?= =?UTF-8?q?=E6=96=B0=E7=A8=B3=E5=AE=9A=E7=89=88=E6=9C=AC=EF=BC=882.4.33?= =?UTF-8?q?=EF=BC=89=EF=BC=8C=E4=BC=98=E5=8C=96=E5=B7=A5=E4=BD=9C=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/script-helper.ts | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/utils/script-helper.ts diff --git a/src/utils/script-helper.ts b/src/utils/script-helper.ts new file mode 100644 index 0000000..8ed5155 --- /dev/null +++ b/src/utils/script-helper.ts @@ -0,0 +1,44 @@ +const ScriptHelper = { + execute(scriptStr: string, options: unknown): unknown { + return Function('"use strict";return (' + scriptStr + ')')()(options); + }, + executeEl(callObject: unknown, logicStr: string): unknown { + return Function('"use strict";return (function(){ return ' + logicStr + '})')().call( + callObject, + ); + }, +}; + +/** + * 多层级嵌套赋值 + * @param obj 赋值的对象 + * @param path 需要赋值的属性 如:aaa.bbb + * @param value 值 + */ +// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types +export const set = (obj: any, path: string, value: unknown): void => { + let schema = obj; + const pList = path.split('.'); + const len = pList.length; + for (let i = 0; i < len - 1; i++) { + const elem = String(pList[i]); + schema = schema[elem] || {}; + } + + schema[pList[len - 1]] = value; +}; + +/** + * 根据路径获取目标对象的值 + * 如:{a:{b:{c:'xxxx'}}} 路径为:a.b.c ,则获取的则为xxxx + * @param path 对象深度属性路径 + * @param obj 取值的对象 + */ +// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types +export const resolve = (path: string, obj: any): unknown => { + return path.split('.').reduce(function (prev, curr) { + return prev ? prev[curr] : null; + }, obj || self); +}; + +export default ScriptHelper;