Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calculate dependent fields for expressions. (#4099)
* Calculate dependent fields for expressions. * Find all prefixes when parsing expressions * Extract expression parsing into its own file. Use for filters as well. * More tests
- Loading branch information
Showing
7 changed files
with
106 additions
and
1 deletion.
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,44 @@ | ||
import {parse} from 'vega-expression'; | ||
import {StringSet} from './../../util'; | ||
|
||
function getName(node: any) { | ||
let name: string[] = []; | ||
|
||
if (node.type === 'Identifier') { | ||
return [node.name]; | ||
} | ||
|
||
if (node.type === 'Literal') { | ||
return [node.value]; | ||
} | ||
|
||
if (node.type === 'MemberExpression') { | ||
name = name.concat(getName(node.object)); | ||
name = name.concat(getName(node.property)); | ||
} | ||
|
||
return name; | ||
} | ||
|
||
function startsWithDatum(node: any): boolean { | ||
if (node.object.type === 'MemberExpression') { | ||
return startsWithDatum(node.object); | ||
} | ||
return node.object.name === 'datum'; | ||
} | ||
|
||
export function getDependentFields(expression: string) { | ||
const ast = parse(expression); | ||
const dependents: StringSet = {}; | ||
ast.visit((node: any) => { | ||
if (node.type === 'MemberExpression' && startsWithDatum(node)) { | ||
dependents[ | ||
getName(node) | ||
.slice(1) | ||
.join('.') | ||
] = true; | ||
} | ||
}); | ||
|
||
return dependents; | ||
} |
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,18 @@ | ||
import {getDependentFields} from '../../../src/compile/data/expressions'; | ||
|
||
describe('compile/data/expressions', () => { | ||
describe('getDependentFields', () => { | ||
it('calcuates right dependent fields for simple expression', () => { | ||
expect(getDependentFields('datum.x + datum.y')).toEqual({x: true, y: true}); | ||
}); | ||
|
||
it('calcuates right dependent fields for compres expression', () => { | ||
expect(getDependentFields('toString(datum.x) + 12')).toEqual({x: true}); | ||
}); | ||
|
||
it('calculates right dependent fields for nested field', () => { | ||
expect(getDependentFields('datum.x.y')).toEqual({x: true, 'x.y': true}); | ||
expect(getDependentFields('datum["x.y"]')).toEqual({'x.y': true}); | ||
}); | ||
}); | ||
}); |
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,3 @@ | ||
declare module 'vega-expression' { | ||
export function parse(expression: string): any; | ||
} |