forked from p96170835/amis
evalJs evalExpression 支持 filter 调用
This commit is contained in:
parent
3054537522
commit
3d6a6160f3
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<string>;
|
||||
if (!names || ((arr = names.split(',')) && arr.length < 2)) {
|
||||
|
|
|
@ -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'
|
||||
});
|
||||
|
||||
|
|
|
@ -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<any>) => 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;
|
||||
|
|
Loading…
Reference in New Issue