diff --git a/src/utils/helper.ts b/src/utils/helper.ts index 337d8f84..c4d719fa 100644 --- a/src/utils/helper.ts +++ b/src/utils/helper.ts @@ -30,7 +30,11 @@ export function createObject( } }) : Object.create(Object.prototype, properties); - props && Object.keys(props).forEach(key => (obj[key] = props[key])); + + props && + isObject(props) && + Object.keys(props).forEach(key => (obj[key] = props[key])); + return obj; } diff --git a/src/utils/tpl-builtin.ts b/src/utils/tpl-builtin.ts index 3a830a4e..95f108e5 100644 --- a/src/utils/tpl-builtin.ts +++ b/src/utils/tpl-builtin.ts @@ -295,6 +295,10 @@ export function registerFilter( filters[name] = fn; } +export function getFilters() { + return filters; +} + export function pickValues(names: string, data: object) { let arr: Array; if (!names || ((arr = names.split(',')) && arr.length < 2)) { diff --git a/src/utils/tpl-lodash.ts b/src/utils/tpl-lodash.ts index 2bf98989..689b1b5e 100644 --- a/src/utils/tpl-lodash.ts +++ b/src/utils/tpl-lodash.ts @@ -1,14 +1,10 @@ import {reigsterTplEnginer, filter} from './tpl'; import template = require('lodash/template'); -import {filters} from './tpl-builtin'; +import {getFilters} from './tpl-builtin'; import React from 'react'; import moment from 'moment'; const imports = { - ...filters, - formatTimeStamp: filters.date, - formatNumber: filters.number, - defaultValue: filters.defaut, default: undefined, moment: moment, countDown: (end: any) => { @@ -28,11 +24,20 @@ const imports = { formatDate: (value: any, format: string = 'LLL', inputFormat: string = '') => moment(value, inputFormat).format(format) }; -delete imports.default; // default 是个关键字,不能 imports 到 lodash 里面去。 + function lodashCompile(str: string, data: object) { try { + const filters = getFilters(); + const finnalImports = { + ...filters, + formatTimeStamp: filters.date, + formatNumber: filters.number, + defaultValue: filters.defaut, + ...imports + }; + delete imports.default; // default 是个关键字,不能 imports 到 lodash 里面去。 const fn = template(str, { - imports: imports, + imports: finnalImports, variable: 'data' }); diff --git a/src/utils/tpl.ts b/src/utils/tpl.ts index 29a25785..0ea09042 100644 --- a/src/utils/tpl.ts +++ b/src/utils/tpl.ts @@ -1,3 +1,6 @@ +import {createObject} from './helper'; +import {getFilters} from './tpl-builtin'; + export interface Enginer { test: (tpl: string) => boolean; compile: (tpl: string, data: object, ...rest: Array) => string; @@ -50,7 +53,7 @@ export function evalExpression(expression: string, data?: object): boolean { `with(data) {${debug ? 'debugger;' : ''}return !!(${expression});}` ); data = data || {}; - return fn.call(data, data); + return fn.call(createObject(getFilters(), data), data); } catch (e) { console.warn(e); return false; @@ -65,7 +68,7 @@ export function evalJS(js: string, data: object): any { `with(data) {${~js.indexOf('return') ? '' : 'return '}${js};}` ); data = data || {}; - return fn.call(data, data); + return fn.call(createObject(getFilters(), data), data); } catch (e) { console.warn(e); return null;