Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'vega-expression' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
export default function parseDateTime(dateTime: string | number | DateTime) {
if (typeof dateTime === 'number' || typeof dateTime === 'string') {
return new Date(dateTime);
}
const expression = dateTimeExpr(dateTime, true) as string;
const code = codegen({ globalvar: 'window' })(parse(expression)).code as string;
// Technically the "code" here is safe to eval(),
// but we will use more conservative approach and manually parse at the moment.
const isUtc = code.startsWith('Date.UTC');
const dateParts = code
.replace(/^(Date[.]UTC|new[ ]Date)\(/, '')
.replace(/\)$/, '')
.split(',')
.map((chunk: string) => Number(chunk.trim())) as [
number, // year
number, // month
number, // date
number, // hours
number, // minutes
number, // seconds
number, // milliseconds
export default function(expr, scope, preamble) {
var params = {}, ast, gen;
// parse the expression to an abstract syntax tree (ast)
try {
expr = isString(expr) ? expr : (stringValue(expr) + '');
ast = parse(expr);
} catch (err) {
error('Expression parse error: ' + expr);
}
// analyze ast function calls for dependencies
ast.visit(function visitor(node) {
if (node.type !== CallExpression) return;
var name = node.callee.name,
visit = codegenParams.visitors[name];
if (visit) visit(name, node.arguments, scope, params);
});
// perform code generation
gen = codeGenerator(ast);
// collect signal dependencies
export function toPredicateFunction(filters: ShelfFilter[]) {
const expr = '(' +
filters.map(f => {
return fieldFilterExpression(f, false); // Do not use inrange as it is not included in the main Vega Expression
}).join(')&&(') +
')';
const ast = vegaExpression.parse(expr);
const codegen = vegaExpression.codegen({
whitelist: ['datum'],
globalvar: 'global'
});
const value = codegen(ast);
return new Function('datum', `return ${value.code};`) as (d: object) => boolean;
}
export function toPredicateFunction(filters: ShelfFilter[]) {
const expr = '(' +
filters.map(f => {
return fieldFilterExpression(f, false); // Do not use inrange as it is not included in the main Vega Expression
}).join(')&&(') +
')';
const ast = vegaExpression.parse(expr);
const codegen = vegaExpression.codegen({
whitelist: ['datum'],
globalvar: 'global'
});
const value = codegen(ast);
return new Function('datum', `return ${value.code};`) as (d: object) => boolean;
}
for (var name in functionContext) { fn[name] = thisPrefix + name; }
return fn;
}
// Export code generator and parameters
export var codegenParams = {
blacklist: ['_'],
whitelist: ['datum', 'event', 'item'],
fieldvar: 'datum',
globalvar: function(id) { return '_[' + stringValue('$' + id) + ']'; },
functions: buildFunctions,
constants: constants,
visitors: astVisitors
};
export var codeGenerator = codegen(codegenParams);
// register Vega-Lite selection functions
expressionFunction('vlSelectionTest', selectionTest, selectionVisitor);
expressionFunction('vlSelectionResolve', selectionResolve, selectionVisitor);
// Export code generator and parameters
export const codegenParams = {
blacklist: ['_'],
whitelist: ['datum', 'event', 'item'],
fieldvar: 'datum',
globalvar: function(id) { return '_[' + stringValue(SignalPrefix + id) + ']'; },
functions: buildFunctions,
constants: constants,
visitors: astVisitors
};
export var codeGenerator = codegen(codegenParams);
if (args[0].type === Literal) { // scale dependency
name = args[0].value;
var scaleName = scalePrefix + name;
if (!params.hasOwnProperty(scaleName)) {
try {
params[scaleName] = scope.scaleRef(name);
} catch (err) {
// TODO: error handling? warning?
}
}
}
else if (args[0].type === Identifier) { // forward reference to signal
name = args[0].name;
args[0] = new ASTNode(Literal);
args[0].raw = '{signal:"' + name + '"}';
}
}
if (args[0].type === Literal) { // scale dependency
name = args[0].value;
var scaleName = scalePrefix + name;
if (!params.hasOwnProperty(scaleName)) {
try {
params[scaleName] = scope.scaleRef(name);
} catch (err) {
// TODO: error handling? warning?
}
}
}
else if (args[0].type === Identifier) { // forward reference to signal
name = args[0].name;
args[0] = new ASTNode(Literal);
args[0].raw = '{signal:"' + name + '"}';
}
}
export function getDependentFields(expression: string) {
const ast = parse(expression);
const dependents = new Set();
ast.visit((node: any) => {
if (node.type === 'MemberExpression' && startsWithDatum(node)) {
dependents.add(
getName(node)
.slice(1)
.join('.')
);
}
});
return dependents;
}
export function getDependentFields(expression) {
const ast = parse(expression);
const dependents = new Set();
ast.visit((node) => {
if (node.type === 'MemberExpression' && startsWithDatum(node)) {
dependents.add(getName(node)
.slice(1)
.join('.'));
}
});
return dependents;
}
//# sourceMappingURL=expressions.js.map