From 10769073e2039d91c4385e37f6397fff9225216f Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 19 Jun 2018 22:06:31 +0800 Subject: [PATCH] fix https://github.com/thx/rap2-delos/issues/214 --- src/components/utils/Tree.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/components/utils/Tree.js b/src/components/utils/Tree.js index 4e009ce..639f4f3 100644 --- a/src/components/utils/Tree.js +++ b/src/components/utils/Tree.js @@ -46,6 +46,36 @@ const treeToJson = (tree) => { result[item.name + rule] = value break case 'Function': + try { + let fn = eval('(' + item.value + ')') // eslint-disable-line no-eval + try { + let value = fn() + } catch(e) { + console.warn(`{ ${item.name}: ${item.value} } => ${e.message}`) + result['string'] = item.value + break + } + let type = typeof value + if (['string', 'number', 'boolean'].indexOf(type) > -1) { + result[type] = value + } else if (type === 'object') { + let objectType = Object.prototype.toString.call(type) + if (objectType === '[object RegExp]') { + type = 'regexp' + } else if (objectType === '[object Array]') { + type = 'array' + } else { + type = 'object' + } + result[type] = value + } else { + result['string'] = item.value + } + } catch (e) { + console.warn(`{ ${item.name}: ${item.value} } => ${e.message}`) // TODO 2.2 初始值异常,应该直接提示到页面上。 + result[item.name] = item.value + } + break case 'RegExp': try { result[item.name + rule] = eval('(' + item.value + ')') // eslint-disable-line no-eval