-
Notifications
You must be signed in to change notification settings - Fork 1
/
random-item.js
168 lines (154 loc) · 5.63 KB
/
random-item.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const sampleSize = require('lodash.samplesize');
module.exports = function (RED) {
function RandomItemNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.on('input', async (msg, send, done) => {
// For maximum backwards compatibility, check that send exists.
// If this node is installed in Node-RED 0.x, it will need to
// fallback to using `node.send`
send =
send ||
function () {
node.send.apply(node, arguments);
};
done =
done ||
function (err) {
if (err) {
node.error(err, msg);
}
};
try {
const array = await getArray(
config.inputType,
config.input,
msg
).catch((err) => {
throw err;
});
if (!Array.isArray(array)) {
throw new Error('Input is not an array');
}
if (array.length === 0) {
throw new Error('Input is an empty array');
}
const count = Math.min(config.number || 1, array.length);
const randomItems = sampleSize(array, count);
const payload =
randomItems.length === 1 ? randomItems[0] : randomItems;
await setContext(payload, msg).catch((err) => {
throw err;
});
node.status({
fill: 'green',
shape: 'dot',
text: JSON.stringify(payload),
});
} catch (err) {
setStatusError(err.message);
done(err);
return;
}
send(msg);
done();
});
function getArray(location, property, message) {
return new Promise((resolve, reject) => {
let val = property;
switch (location) {
case 'env':
val = RED.util.evaluateNodeProperty(
property,
'env',
node
);
break;
case 'flow':
case 'global':
RED.util.evaluateNodeProperty(
property,
location,
node,
message,
(err, value) => {
if (err) {
reject(err);
} else {
resolve(value);
}
}
);
return;
case 'json':
try {
val = JSON.parse(property);
} catch (e) {
reject('invalid json');
}
break;
case 'jsonata':
RED.util.evaluateJSONataExpression(
RED.util.prepareJSONataExpression(property, node),
message,
(err, value) => {
if (err) {
reject(
RED._('change.errors.invalid-expr', {
error: err.message,
})
);
} else {
resolve(value);
}
}
);
return;
case 'msg':
val = RED.util.getMessageProperty(message, property);
break;
}
resolve(val);
});
}
function setContext(payload, msg) {
return new Promise((resolve, reject) => {
const contextKey = RED.util.parseContextStore(config.output);
switch (config.outputType) {
case 'flow':
case 'global':
node.context()[config.outputType].set(
contextKey.key,
payload,
contextKey.store,
(err) => {
if (err) {
reject(err);
} else {
resolve();
}
}
);
break;
case 'msg':
default:
RED.util.setObjectProperty(
msg,
contextKey.key,
payload
);
break;
}
resolve();
});
}
function setStatusError(text) {
node.status({
fill: 'red',
shape: 'ring',
text,
});
}
}
RED.nodes.registerType('random-item', RandomItemNode);
};