Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
update expression parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffan committed Aug 27, 2018
1 parent df4d2f3 commit c0f986d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
19 changes: 13 additions & 6 deletions src/fields.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import FieldSelect from './components/Select.vue';
import FieldRange from './components/Range.vue';
import FieldNumber from './components/Number.vue';
import {assign, each, evaluate, get, isArray, isString, isUndefined, set, warn} from './util';
import {assign, each, get, parse, isArray, isString, isUndefined, set, warn} from './util';
export default {
Expand Down Expand Up @@ -74,16 +74,23 @@
}
},
evaluate(expr, values = this.values) {
evaluate(expression, values = this.values) {
if (isString(expr)) {
try {
const context = {$match, $values: values, $get: key => get(values, key)};
if (isString(expression)) {
expression = parse(expression);
}
return expression.call(this, values, {
$match, $get: key => get(values, key)
});
return evaluate(this, expr, context);
} catch (e) {
warn(e);
}
return expr.call(this, values, this);
return true;
},
prepare(config = this.config, prefix = this.prefix) {
Expand Down
25 changes: 9 additions & 16 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,17 @@ export function set(obj, key, val) {
_set(obj, parts.shift(), val);
}

const quotedStringRe = /([^"']+)((.)(?:[^\3\\]|\\.)*?\3|.)?/g;
const parsedFunc = {};
const expressionRe = /((?:\d|true|false|null|undefined|(?:this\.|\$)[\S]+|\W)*)([\w][\w+.]*)?/g;
const expressions = {};
export function evaluate(self, expr, context) {

expressions[expr] = expressions[expr] || expr.replace(quotedStringRe, (match, unquoted, quoted = '') =>
unquoted.replace(expressionRe, (match, prefix = '', expr) =>
match ? `${prefix}${expr ? `$get('${expr}')` : ''}` : ''
) + quoted
);

try {
return (Function('c', `with(c){return ${expressions[expr]}}`)).call(self, context);
} catch (e) {
warn(e);
}
const quotedStringRe = /([^"']+)((.)(?:[^\3\\]|\\.)*?\3|.)?/g;

return false;
export function parse(expr) {
return parsedFunc[expr] = parsedFunc[expr] ||
Function('$values', '$context', `with($context){return ${expr.replace(quotedStringRe,
(match, unquoted, quoted = '') => unquoted.replace(expressionRe,
(match, prefix = '', expression) => match ? `${prefix}${expression ? `$get('${expression}')` : ''}` : ''
) + quoted
)}}`);
}

export function each(obj, iterator) {
Expand Down
2 changes: 1 addition & 1 deletion stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import FieldsStory from './components/FieldsStory.vue';
import {storiesOf} from '@storybook/vue';

storiesOf('Fields', module)
.add('Default', () => ({extends: FieldsStory}));
.add('Default', () => FieldsStory);

0 comments on commit c0f986d

Please sign in to comment.