Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow functional override of "." for property read #259

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ export default function evaluate(tokens, expr, values) {
nstack.push(item);
} else if (type === IMEMBER) {
n1 = nstack.pop();
nstack.push(n1[item.value]);
f = expr.binaryOps['.']
if(f.apply && f.call) {
nstack.push(f.apply(undefined, [n1, item.value]));
} else {
throw new Error(f + ' is not a function');
}
} else if (type === IENDSTATEMENT) {
nstack.pop();
} else if (type === IARRAY) {
Expand Down
4 changes: 4 additions & 0 deletions src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ export function arrayIndex(array, index) {
return array[index | 0];
}

export function member(object, name) {
return object[name];
}

export function max(array) {
if (arguments.length === 1 && Array.isArray(array)) {
return Math.max.apply(Math, array);
Expand Down
8 changes: 7 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
roundTo,
setVar,
arrayIndex,
member,
max,
min,
arrayMap,
Expand Down Expand Up @@ -108,9 +109,14 @@ export function Parser(options) {
or: orOperator,
'in': inOperator,
'=': setVar,
'[': arrayIndex
'[': arrayIndex,
'.': member
};

if(this.options.overrideMember) {
this.binaryOps['.'] = this.options.overrideMember
}

this.ternaryOps = {
'?': condition
};
Expand Down
3 changes: 2 additions & 1 deletion src/simplify.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Instruction, INUMBER, IOP1, IOP2, IOP3, IVAR, IVARNAME, IEXPR, IMEMBER, IARRAY } from './instruction';
import { member } from './functions';

export default function simplify(tokens, unaryOps, binaryOps, ternaryOps, values) {
var nstack = [];
Expand Down Expand Up @@ -46,7 +47,7 @@ export default function simplify(tokens, unaryOps, binaryOps, ternaryOps, values
newexpression.push(nstack.shift());
}
newexpression.push(new Instruction(IEXPR, simplify(item.value, unaryOps, binaryOps, ternaryOps, values)));
} else if (type === IMEMBER && nstack.length > 0) {
} else if (type === IMEMBER && nstack.length > 0 && member === binaryOps['.']) { //check if member overridden
n1 = nstack.pop();
nstack.push(new Instruction(INUMBER, n1.value[item.value]));
} /* else if (type === IARRAY && nstack.length >= item.value) {
Expand Down
13 changes: 13 additions & 0 deletions test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,17 @@ describe('Functions', function () {
assert.strictEqual(parser.evaluate('sum([1, 2])'), 3);
});
});

describe('member(value,name)', function () {
it('basic', function () {
var parser = new Parser();
assert.strictEqual(parser.evaluate('a.something', {a : {something: 123}}), 123);
});

it('able to override', function () {
var parser = new Parser({overrideMember:function(v,n) { return 456; }});
assert.strictEqual(parser.evaluate('a.anything', {a : {}}), 456);
});
});

});