-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathd3-array.js
54 lines (49 loc) · 1.44 KB
/
d3-array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const d3 = require('d3-array');
const mapToObject = map =>
Array.from(map).reduce(
(obj, [key, value]) => Object.assign(obj, { [key]: value instanceof Map || value instanceof Set ? mapToObject(value) : value }),
{}
);
module.exports = function(RED) {
function D3ArrayNode(n) {
RED.nodes.createNode(this, n);
var node = this;
this.on('input', function(msg, send, done) {
const nodeSend =
send ||
function() {
node.send.apply(node, arguments);
};
const nodeDone = () => {
if (done) {
done();
}
};
const data = RED.util.evaluateNodeProperty(n.property, n.propertyType, this, msg);
const accessors = JSON.parse(n.accessors);
const parameters = accessors.map(({ t: type, fn: value }) => {
if (type == null || type === 'jsonata') {
return args => {
var expr = RED.util.prepareJSONataExpression(value, node);
result = RED.util.evaluateJSONataExpression(expr, args);
return result;
};
}
return RED.util.evaluateNodeProperty(value, type, node, msg);
});
if (d3[n.function] == null) {
node.error(`Function [${n.function}] does not exists.`);
nodeSend(msg);
nodeDone();
return;
}
const payload = d3[n.function].apply(null, [data, ...parameters]);
nodeSend({
...msg,
payload: payload instanceof Map || payload instanceof Set ? mapToObject(payload) : payload
});
nodeDone();
});
}
RED.nodes.registerType('d3-array', D3ArrayNode);
};