mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Add $context/$flow/$global functions to jsonata
This commit is contained in:
parent
8f92a3e875
commit
30920b1b78
@ -168,8 +168,8 @@ module.exports = function(grunt) {
|
|||||||
// TODO: resolve relative resource paths in
|
// TODO: resolve relative resource paths in
|
||||||
// bootstrap/FA/jquery
|
// bootstrap/FA/jquery
|
||||||
],
|
],
|
||||||
"public/vendor/jsonata/jsonata.js": [
|
"public/vendor/jsonata/jsonata.min.js": [
|
||||||
"node_modules/jsonata/jsonata.js",
|
"node_modules/jsonata/jsonata.min.js",
|
||||||
"editor/vendor/jsonata/formatter.js"
|
"editor/vendor/jsonata/formatter.js"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -180,7 +180,6 @@ module.exports = function(grunt) {
|
|||||||
files: {
|
files: {
|
||||||
'public/red/red.min.js': 'public/red/red.js',
|
'public/red/red.min.js': 'public/red/red.js',
|
||||||
'public/red/main.min.js': 'public/red/main.js',
|
'public/red/main.min.js': 'public/red/main.js',
|
||||||
'public/vendor/jsonata/jsonata.min.js': 'public/vendor/jsonata/jsonata.js',
|
|
||||||
'public/vendor/ace/mode-jsonata.js': 'editor/vendor/jsonata/mode-jsonata.js',
|
'public/vendor/ace/mode-jsonata.js': 'editor/vendor/jsonata/mode-jsonata.js',
|
||||||
'public/vendor/ace/worker-jsonata.js': 'editor/vendor/jsonata/worker-jsonata.js',
|
'public/vendor/ace/worker-jsonata.js': 'editor/vendor/jsonata/worker-jsonata.js',
|
||||||
'public/vendor/ace/snippets/jsonata.js': 'editor/vendor/jsonata/snippets-jsonata.js'
|
'public/vendor/ace/snippets/jsonata.js': 'editor/vendor/jsonata/snippets-jsonata.js'
|
||||||
|
3
editor/vendor/jsonata/formatter.js
vendored
3
editor/vendor/jsonata/formatter.js
vendored
@ -111,8 +111,11 @@
|
|||||||
'$average':{ args:['value'] },
|
'$average':{ args:['value'] },
|
||||||
'$boolean':{ args:['value'] },
|
'$boolean':{ args:['value'] },
|
||||||
'$contains':{ args:['str','pattern']},
|
'$contains':{ args:['str','pattern']},
|
||||||
|
'$context': {args:['string']},
|
||||||
'$count':{ args:['array'] },
|
'$count':{ args:['array'] },
|
||||||
'$exists':{ args:['value'] },
|
'$exists':{ args:['value'] },
|
||||||
|
'$flow': {args:['string']},
|
||||||
|
'$global': {args:['string']},
|
||||||
'$join':{ args:['array','separator'] },
|
'$join':{ args:['array','separator'] },
|
||||||
'$keys':{ args:['object'] },
|
'$keys':{ args:['object'] },
|
||||||
'$length':{ args:['string'] },
|
'$length':{ args:['string'] },
|
||||||
|
@ -17,8 +17,6 @@
|
|||||||
module.exports = function(RED) {
|
module.exports = function(RED) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var jsonata = require('jsonata');
|
|
||||||
|
|
||||||
var operators = {
|
var operators = {
|
||||||
'eq': function(a, b) { return a == b; },
|
'eq': function(a, b) { return a == b; },
|
||||||
'neq': function(a, b) { return a != b; },
|
'neq': function(a, b) { return a != b; },
|
||||||
@ -44,7 +42,7 @@ module.exports = function(RED) {
|
|||||||
|
|
||||||
if (this.propertyType === 'jsonata') {
|
if (this.propertyType === 'jsonata') {
|
||||||
try {
|
try {
|
||||||
this.property = jsonata(this.property);
|
this.property = RED.util.prepareJSONataExpression(this.property,this);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
this.error(RED._("switch.errors.invalid-expr",{error:err.message}));
|
this.error(RED._("switch.errors.invalid-expr",{error:err.message}));
|
||||||
return;
|
return;
|
||||||
@ -70,7 +68,7 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
} else if (rule.vt === "jsonata") {
|
} else if (rule.vt === "jsonata") {
|
||||||
try {
|
try {
|
||||||
rule.v = jsonata(rule.v);
|
rule.v = RED.util.prepareJSONataExpression(rule.v,node);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
this.error(RED._("switch.errors.invalid-expr",{error:err.message}));
|
this.error(RED._("switch.errors.invalid-expr",{error:err.message}));
|
||||||
valid = false;
|
valid = false;
|
||||||
@ -88,7 +86,7 @@ module.exports = function(RED) {
|
|||||||
rule.v2 = Number(rule.v2);
|
rule.v2 = Number(rule.v2);
|
||||||
} else if (rule.v2t === 'jsonata') {
|
} else if (rule.v2t === 'jsonata') {
|
||||||
try {
|
try {
|
||||||
rule.v2 = jsonata(rule.v2);
|
rule.v2 = RED.util.prepareJSONataExpression(rule.v2,node);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
this.error(RED._("switch.errors.invalid-expr",{error:err.message}));
|
this.error(RED._("switch.errors.invalid-expr",{error:err.message}));
|
||||||
valid = false;
|
valid = false;
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
module.exports = function(RED) {
|
module.exports = function(RED) {
|
||||||
"use strict";
|
"use strict";
|
||||||
var jsonata = require("jsonata");
|
|
||||||
|
|
||||||
function ChangeNode(n) {
|
function ChangeNode(n) {
|
||||||
RED.nodes.createNode(this, n);
|
RED.nodes.createNode(this, n);
|
||||||
@ -88,7 +87,7 @@ module.exports = function(RED) {
|
|||||||
rule.to = /^true$/i.test(rule.to);
|
rule.to = /^true$/i.test(rule.to);
|
||||||
} else if (rule.tot === 'jsonata') {
|
} else if (rule.tot === 'jsonata') {
|
||||||
try {
|
try {
|
||||||
rule.to = jsonata(rule.to);
|
rule.to = RED.util.prepareJSONataExpression(rule.to,this);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
valid = false;
|
valid = false;
|
||||||
this.error(RED._("change.errors.invalid-from",{error:e.message}));
|
this.error(RED._("change.errors.invalid-from",{error:e.message}));
|
||||||
|
@ -109,7 +109,18 @@
|
|||||||
"$spread": {
|
"$spread": {
|
||||||
"args": "object",
|
"args": "object",
|
||||||
"desc": "Splits an object containing key/value pairs into an array of objects, each of which has a single key/value pair from the input object. If the parameter is an array of objects, then the resultant array contains an object for every key/value pair in every object in the supplied array."
|
"desc": "Splits an object containing key/value pairs into an array of objects, each of which has a single key/value pair from the input object. If the parameter is an array of objects, then the resultant array contains an object for every key/value pair in every object in the supplied array."
|
||||||
|
},
|
||||||
|
|
||||||
|
"$context": {
|
||||||
|
"args": "string",
|
||||||
|
"desc": "Retrieves a node context property."
|
||||||
|
},
|
||||||
|
"$flow": {
|
||||||
|
"args": "string",
|
||||||
|
"desc": "Retrieves a flow context property."
|
||||||
|
},
|
||||||
|
"$global": {
|
||||||
|
"args": "string",
|
||||||
|
"desc": "Retrieves a global context property."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -323,11 +323,25 @@ function evaluateNodeProperty(value, type, node, msg) {
|
|||||||
} else if (type === 'bool') {
|
} else if (type === 'bool') {
|
||||||
return /^true$/i.test(value);
|
return /^true$/i.test(value);
|
||||||
} else if (type === 'jsonata') {
|
} else if (type === 'jsonata') {
|
||||||
return jsonata(value).evaluate({msg:msg});
|
return prepareJSONataExpression(value,node).evaluate({msg:msg});
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function prepareJSONataExpression(value,node) {
|
||||||
|
var expr = jsonata(value);
|
||||||
|
expr.assign('context',function(val) {
|
||||||
|
return node.context().get(val);
|
||||||
|
});
|
||||||
|
expr.assign('flow',function(val) {
|
||||||
|
return node.context().flow.get(val);
|
||||||
|
});
|
||||||
|
expr.assign('global',function(val) {
|
||||||
|
return node.context().global(val);
|
||||||
|
});
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
|
|
||||||
function normaliseNodeTypeName(name) {
|
function normaliseNodeTypeName(name) {
|
||||||
var result = name.replace(/[^a-zA-Z0-9]/g, " ");
|
var result = name.replace(/[^a-zA-Z0-9]/g, " ");
|
||||||
result = result.trim();
|
result = result.trim();
|
||||||
@ -351,5 +365,6 @@ module.exports = {
|
|||||||
setMessageProperty: setMessageProperty,
|
setMessageProperty: setMessageProperty,
|
||||||
evaluateNodeProperty: evaluateNodeProperty,
|
evaluateNodeProperty: evaluateNodeProperty,
|
||||||
normalisePropertyExpression: normalisePropertyExpression,
|
normalisePropertyExpression: normalisePropertyExpression,
|
||||||
normaliseNodeTypeName: normaliseNodeTypeName
|
normaliseNodeTypeName: normaliseNodeTypeName,
|
||||||
|
prepareJSONataExpression: prepareJSONataExpression
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user