-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use scope-eval and contextual-proxy to replace bcx-expression-e…
…valuator BREAKING CHANGE: the expression behaviour is now different from bcx-expression-evaluator, noticably not silent exception on accessing property of undefined.
- Loading branch information
Showing
13 changed files
with
145 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
export default function canBeProxied(value) { | ||
const type = typeof value; | ||
return type === 'function' || (type === 'object' && value !== null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import _ from 'lodash'; | ||
|
||
export function modifiedOverrideContext(overrideContext, variation) { | ||
return {...overrideContext, ...variation}; | ||
} | ||
import proxy from 'contextual-proxy'; | ||
|
||
export default function (scope, variation) { | ||
if (_.isEmpty(variation)) return scope; | ||
let {bindingContext, overrideContext} = scope; | ||
return {bindingContext, overrideContext: modifiedOverrideContext(overrideContext, variation)}; | ||
} | ||
let {$this, $parent, $contextual} = scope; | ||
const contextual = Object.create($contextual); | ||
Object.assign(contextual, variation); | ||
return proxy($this, $parent, contextual); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import test from 'tape'; | ||
import canBeProxied from '../src/can-be-proxied'; | ||
|
||
test('object can be proxied, but not null object', t => { | ||
console.log('canBeProxied({})', canBeProxied({})); | ||
t.ok(canBeProxied({})); | ||
t.ok(canBeProxied(Object.create(null))); | ||
t.ok(canBeProxied([])); | ||
t.ok(canBeProxied({a: 1})); | ||
t.ok(canBeProxied(/a/)); | ||
t.notOk(canBeProxied(null)); | ||
t.end(); | ||
}); | ||
|
||
test('function can be proxied', t => { | ||
t.ok(canBeProxied(function() {})); | ||
t.ok(canBeProxied(() => false)); | ||
t.end(); | ||
}); | ||
|
||
test('primitive values cannot be proxied', t => { | ||
t.notOk(canBeProxied(1)); | ||
t.notOk(canBeProxied(0)); | ||
t.notOk(canBeProxied("")); | ||
t.notOk(canBeProxied("lorem")); | ||
t.notOk(canBeProxied(NaN)); | ||
t.notOk(canBeProxied(Infinity)); | ||
t.notOk(canBeProxied(undefined)); | ||
t.notOk(canBeProxied(false)); | ||
t.notOk(canBeProxied(true)); | ||
t.notOk(canBeProxied(Symbol('f'))); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,39 @@ | ||
import test from 'tape'; | ||
import _ from 'lodash'; | ||
import scopeVariation from '../src/scope-variation'; | ||
import {createSimpleScope, Parser} from 'bcx-expression-evaluator'; | ||
import proxy from 'contextual-proxy'; | ||
import ScopedEval from 'scoped-eval'; | ||
|
||
const parser = new Parser(); | ||
const scopedEval = new ScopedEval(); | ||
|
||
test('scopeVariation: creates scope variation', t => { | ||
const model = {a: 1, b: 2}; | ||
const parent = {c: 3, d: 4}; | ||
|
||
const scope = createSimpleScope(model, parent); | ||
const scope = proxy(model, parent); | ||
|
||
const variation = scopeVariation(scope, {a: 'one', c: {new: 1}}); | ||
|
||
t.deepEqual(model, {a: 1, b: 2}, 'does not touch original bindingContext'); | ||
t.deepEqual(parent, {c: 3, d: 4}, 'does not touch original parentBindingContext'); | ||
t.deepEqual(model, {a: 1, b: 2}, 'does not touch original object'); | ||
t.deepEqual(parent, {c: 3, d: 4}, 'does not touch original parent object'); | ||
|
||
t.equal(parser.parse('a').evaluate(scope), 1); | ||
t.equal(parser.parse('a').evaluate(variation), 'one'); | ||
t.equal(scopedEval.eval('a', scope), 1); | ||
t.equal(scopedEval.eval('a', variation), 'one'); | ||
|
||
t.equal(parser.parse('b').evaluate(scope), 2); | ||
t.equal(parser.parse('b').evaluate(variation), 2); | ||
t.equal(scopedEval.eval('b', scope), 2); | ||
t.equal(scopedEval.eval('b', variation), 2); | ||
|
||
t.equal(parser.parse('c').evaluate(scope), 3); | ||
t.deepEqual(parser.parse('c').evaluate(variation), {new: 1}); | ||
t.equal(scopedEval.eval('c', scope), 3); | ||
t.deepEqual(scopedEval.eval('c', variation), {new: 1}); | ||
|
||
t.equal(parser.parse('$parent.a').evaluate(scope), undefined); | ||
t.equal(parser.parse('$parent.a').evaluate(variation), undefined); | ||
t.equal(scopedEval.eval('$parent.a', scope), undefined); | ||
t.equal(scopedEval.eval('$parent.a', variation), undefined); | ||
|
||
t.equal(parser.parse('$parent.b').evaluate(scope), undefined); | ||
t.equal(parser.parse('$parent.b').evaluate(variation), undefined); | ||
t.equal(scopedEval.eval('$parent.b', scope), undefined); | ||
t.equal(scopedEval.eval('$parent.b', variation), undefined); | ||
|
||
t.equal(parser.parse('$parent.c').evaluate(scope), 3); | ||
t.deepEqual(parser.parse('$parent.c').evaluate(variation), 3); | ||
t.equal(scopedEval.eval('$parent.c', scope), 3); | ||
t.deepEqual(scopedEval.eval('$parent.c', variation), 3); | ||
|
||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.