@rosepetal/node-red-contrib-image-tools 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +6 -0
- package/.github/workflows/build-prebuild.yml +326 -0
- package/LICENSE +13 -0
- package/README.md +161 -0
- package/artifacts/darwin-arm64/package.json +25 -0
- package/artifacts/linux-arm64/package.json +26 -0
- package/artifacts/linux-x64/package.json +26 -0
- package/artifacts/linuxmusl-x64/package.json +26 -0
- package/assets/flow_time_example.gif +0 -0
- package/assets/nodes/io/array-in-demo.gif +0 -0
- package/assets/nodes/io/array-out-demo.gif +0 -0
- package/assets/nodes/io/array-select-demo.gif +0 -0
- package/assets/nodes/io/image-in-demo.gif +0 -0
- package/assets/nodes/mix/concat-demo.gif +0 -0
- package/assets/nodes/mix/mosaic-demo.gif +0 -0
- package/assets/nodes/transform/crop-demo.gif +0 -0
- package/assets/nodes/transform/filter-demo.gif +0 -0
- package/assets/nodes/transform/padding-demo.gif +0 -0
- package/assets/nodes/transform/resize-demo.gif +0 -0
- package/assets/nodes/transform/rotate-demo.gif +0 -0
- package/docs/nodes/blend/add-mask.md +275 -0
- package/docs/nodes/blend/add-masks.md +284 -0
- package/docs/nodes/blend/blend.md +248 -0
- package/docs/nodes/io/array-in.md +119 -0
- package/docs/nodes/io/array-out.md +150 -0
- package/docs/nodes/io/array-select.md +157 -0
- package/docs/nodes/io/image-in.md +131 -0
- package/docs/nodes/io/image-out.md +179 -0
- package/docs/nodes/io/queue.md +57 -0
- package/docs/nodes/io/save-file.md +30 -0
- package/docs/nodes/mix/advanced-mosaic.md +108 -0
- package/docs/nodes/mix/concat.md +127 -0
- package/docs/nodes/mix/mosaic.md +146 -0
- package/docs/nodes/specialized/cropBB.md +271 -0
- package/docs/nodes/specialized/image-align.md +426 -0
- package/docs/nodes/transform/crop.md +132 -0
- package/docs/nodes/transform/draw.md +77 -0
- package/docs/nodes/transform/filter.md +84 -0
- package/docs/nodes/transform/padding.md +133 -0
- package/docs/nodes/transform/resize.md +158 -0
- package/docs/nodes/transform/rotate.md +121 -0
- package/docs/nodes/util/block-detect.md +46 -0
- package/install.sh +144 -0
- package/node-red-contrib-image-tools/lib/cpp-bridge.js +224 -0
- package/node-red-contrib-image-tools/lib/node-utils.js +529 -0
- package/node-red-contrib-image-tools/nodes/blend/add-bbs.html +590 -0
- package/node-red-contrib-image-tools/nodes/blend/add-bbs.js +241 -0
- package/node-red-contrib-image-tools/nodes/blend/add-mask.html +449 -0
- package/node-red-contrib-image-tools/nodes/blend/add-mask.js +170 -0
- package/node-red-contrib-image-tools/nodes/blend/add-masks.html +703 -0
- package/node-red-contrib-image-tools/nodes/blend/add-masks.js +321 -0
- package/node-red-contrib-image-tools/nodes/blend/blend.html +552 -0
- package/node-red-contrib-image-tools/nodes/blend/blend.js +124 -0
- package/node-red-contrib-image-tools/nodes/io/array-in.html +110 -0
- package/node-red-contrib-image-tools/nodes/io/array-in.js +113 -0
- package/node-red-contrib-image-tools/nodes/io/array-out.html +125 -0
- package/node-red-contrib-image-tools/nodes/io/array-out.js +213 -0
- package/node-red-contrib-image-tools/nodes/io/array-select.html +156 -0
- package/node-red-contrib-image-tools/nodes/io/array-select.js +187 -0
- package/node-red-contrib-image-tools/nodes/io/folder-in.html +248 -0
- package/node-red-contrib-image-tools/nodes/io/folder-in.js +335 -0
- package/node-red-contrib-image-tools/nodes/io/image-in.html +146 -0
- package/node-red-contrib-image-tools/nodes/io/image-in.js +143 -0
- package/node-red-contrib-image-tools/nodes/io/image-out.html +506 -0
- package/node-red-contrib-image-tools/nodes/io/image-out.js +545 -0
- package/node-red-contrib-image-tools/nodes/io/queue.html +119 -0
- package/node-red-contrib-image-tools/nodes/io/queue.js +191 -0
- package/node-red-contrib-image-tools/nodes/io/save-file.html +243 -0
- package/node-red-contrib-image-tools/nodes/io/save-file.js +440 -0
- package/node-red-contrib-image-tools/nodes/mix/advanced-mosaic.html +2149 -0
- package/node-red-contrib-image-tools/nodes/mix/advanced-mosaic.js +195 -0
- package/node-red-contrib-image-tools/nodes/mix/concat.html +390 -0
- package/node-red-contrib-image-tools/nodes/mix/concat.js +112 -0
- package/node-red-contrib-image-tools/nodes/mix/mosaic.html +550 -0
- package/node-red-contrib-image-tools/nodes/mix/mosaic.js +151 -0
- package/node-red-contrib-image-tools/nodes/specialized/cropBB.html +268 -0
- package/node-red-contrib-image-tools/nodes/specialized/cropBB.js +277 -0
- package/node-red-contrib-image-tools/nodes/specialized/image-align.html +528 -0
- package/node-red-contrib-image-tools/nodes/specialized/image-align.js +306 -0
- package/node-red-contrib-image-tools/nodes/transform/crop.html +410 -0
- package/node-red-contrib-image-tools/nodes/transform/crop.js +149 -0
- package/node-red-contrib-image-tools/nodes/transform/draw.html +719 -0
- package/node-red-contrib-image-tools/nodes/transform/draw.js +509 -0
- package/node-red-contrib-image-tools/nodes/transform/filter.html +578 -0
- package/node-red-contrib-image-tools/nodes/transform/filter.js +177 -0
- package/node-red-contrib-image-tools/nodes/transform/padding.html +475 -0
- package/node-red-contrib-image-tools/nodes/transform/padding.js +142 -0
- package/node-red-contrib-image-tools/nodes/transform/resize.html +429 -0
- package/node-red-contrib-image-tools/nodes/transform/resize.js +162 -0
- package/node-red-contrib-image-tools/nodes/transform/rotate.html +299 -0
- package/node-red-contrib-image-tools/nodes/transform/rotate.js +141 -0
- package/node-red-contrib-image-tools/nodes/util/block-detect.html +85 -0
- package/node-red-contrib-image-tools/nodes/util/block-detect.js +136 -0
- package/node-red-contrib-image-tools/nodes/util/clean-debug.html +201 -0
- package/node-red-contrib-image-tools/nodes/util/clean-debug.js +393 -0
- package/node-red-contrib-image-tools/package.json +64 -0
- package/npm/darwin-arm64/lib/.gitkeep +1 -0
- package/npm/darwin-arm64/package.json +25 -0
- package/npm/darwin-x64/lib/.gitkeep +1 -0
- package/npm/darwin-x64/package.json +25 -0
- package/npm/linux-arm64/lib/.gitkeep +1 -0
- package/npm/linux-arm64/package.json +26 -0
- package/npm/linux-x64/lib/.gitkeep +1 -0
- package/npm/linux-x64/package.json +26 -0
- package/npm/linuxmusl-x64/lib/.gitkeep +1 -0
- package/npm/linuxmusl-x64/package.json +26 -0
- package/npm/win32-x64/lib/.gitkeep +1 -0
- package/npm/win32-x64/package.json +25 -0
- package/package.json +74 -0
- package/rosepetal-image-engine/binding.gyp +153 -0
- package/rosepetal-image-engine/package.json +19 -0
- package/rosepetal-image-engine/src/add-bbs.cpp +433 -0
- package/rosepetal-image-engine/src/add-mask.cpp +240 -0
- package/rosepetal-image-engine/src/add-masks.cpp +638 -0
- package/rosepetal-image-engine/src/advanced-mosaic.cpp +740 -0
- package/rosepetal-image-engine/src/blend.cpp +200 -0
- package/rosepetal-image-engine/src/concat.cpp +329 -0
- package/rosepetal-image-engine/src/crop.cpp +139 -0
- package/rosepetal-image-engine/src/draw.cpp +414 -0
- package/rosepetal-image-engine/src/filter.cpp +230 -0
- package/rosepetal-image-engine/src/filters/kernels.h +171 -0
- package/rosepetal-image-engine/src/image-align.cpp +551 -0
- package/rosepetal-image-engine/src/main.cpp +36 -0
- package/rosepetal-image-engine/src/mosaic.cpp +373 -0
- package/rosepetal-image-engine/src/padding.cpp +124 -0
- package/rosepetal-image-engine/src/resize.cpp +210 -0
- package/rosepetal-image-engine/src/rotate.cpp +177 -0
- package/rosepetal-image-engine/src/utils.h +563 -0
- package/rosepetal-image-engine/test-formats.js +119 -0
- package/scripts/build-opencv-static.ps1 +166 -0
- package/scripts/build-opencv-static.sh +147 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Node.js logic for the Array‑Out node with timeout, ordering and validation.
|
|
3
|
+
* @author Rosepetal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
module.exports = function (RED) {
|
|
7
|
+
function ArrayOutNode(config) {
|
|
8
|
+
RED.nodes.createNode(this, config);
|
|
9
|
+
const node = this;
|
|
10
|
+
|
|
11
|
+
/* ────────────────────────────
|
|
12
|
+
░░ 1. Read & validate cfg ░░
|
|
13
|
+
──────────────────────────── */
|
|
14
|
+
node.timeout = parseInt(config.timeout) || 5000;
|
|
15
|
+
node.expectedCount = parseInt(config.expectedCount);
|
|
16
|
+
node.outputPath = config.outputPath || 'payload';
|
|
17
|
+
node.outputPathType = config.outputPathType || 'msg';
|
|
18
|
+
|
|
19
|
+
if (!Number.isInteger(node.expectedCount) || node.expectedCount < 1) {
|
|
20
|
+
node.error('Invalid expectedCount. Must be a positive integer.');
|
|
21
|
+
node.expectedCount = 2; // sensible default
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/* ────────────────────────────
|
|
25
|
+
░░ 2. Internal state ░░
|
|
26
|
+
──────────────────────────── */
|
|
27
|
+
node.collection = {};
|
|
28
|
+
node.timeoutHandle = null;
|
|
29
|
+
node.isCollecting = false;
|
|
30
|
+
node.collectionStartTime = null;
|
|
31
|
+
|
|
32
|
+
setStatusReady();
|
|
33
|
+
|
|
34
|
+
/* ────────────────────────────
|
|
35
|
+
░░ 3. Helper functions ░░
|
|
36
|
+
──────────────────────────── */
|
|
37
|
+
function setStatusReady() {
|
|
38
|
+
node.status({
|
|
39
|
+
fill: 'blue',
|
|
40
|
+
shape: 'dot',
|
|
41
|
+
text: `Ready (expect ${node.expectedCount})`
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function resetCollection() {
|
|
46
|
+
if (node.timeoutHandle) {
|
|
47
|
+
clearTimeout(node.timeoutHandle);
|
|
48
|
+
node.timeoutHandle = null;
|
|
49
|
+
}
|
|
50
|
+
node.collection = {};
|
|
51
|
+
node.isCollecting = false;
|
|
52
|
+
node.collectionStartTime = null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function handleTimeout() {
|
|
56
|
+
const elapsed = Date.now() - node.collectionStartTime;
|
|
57
|
+
|
|
58
|
+
/* Build timeout message with collected data + missing info */
|
|
59
|
+
const collectedPositions = Object.keys(node.collection).map(k => parseInt(k)).sort((a, b) => a - b);
|
|
60
|
+
const missingPositions = [];
|
|
61
|
+
for (let i = 0; i < node.expectedCount; i++) {
|
|
62
|
+
if (!node.collection.hasOwnProperty(i)) {
|
|
63
|
+
missingPositions.push(i);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/* Build the partial array (same logic as normal completion) */
|
|
68
|
+
const result = Array.from({ length: node.expectedCount }, (_, i) =>
|
|
69
|
+
node.collection.hasOwnProperty(i) ? node.collection[i] : null
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
/* Create timeout message */
|
|
73
|
+
const timeoutMsg = {
|
|
74
|
+
meta: {
|
|
75
|
+
timeout: true,
|
|
76
|
+
missingPositions: missingPositions,
|
|
77
|
+
collectedPositions: collectedPositions,
|
|
78
|
+
expectedCount: node.expectedCount,
|
|
79
|
+
collectedCount: collectedPositions.length,
|
|
80
|
+
elapsed: elapsed
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/* Place the partial array where the user wants it */
|
|
85
|
+
if (node.outputPathType === 'msg') {
|
|
86
|
+
RED.util.setMessageProperty(timeoutMsg, node.outputPath, result, true);
|
|
87
|
+
} else if (node.outputPathType === 'flow') {
|
|
88
|
+
node.context().flow.set(node.outputPath, result);
|
|
89
|
+
} else if (node.outputPathType === 'global') {
|
|
90
|
+
node.context().global.set(node.outputPath, result);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/* Send to output 2 only */
|
|
94
|
+
node.send([null, timeoutMsg]);
|
|
95
|
+
|
|
96
|
+
node.status({
|
|
97
|
+
fill: 'yellow',
|
|
98
|
+
shape: 'ring',
|
|
99
|
+
text: `Timeout: sent ${collectedPositions.length}/${node.expectedCount} to output 2`
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
resetCollection();
|
|
103
|
+
setTimeout(setStatusReady, 3000);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function assembleAndOutput(sourceMsg) {
|
|
107
|
+
const elapsed = Date.now() - node.collectionStartTime;
|
|
108
|
+
|
|
109
|
+
/* Build the ordered array (fill gaps with null) */
|
|
110
|
+
const result = Array.from({ length: node.expectedCount }, (_, i) =>
|
|
111
|
+
node.collection.hasOwnProperty(i) ? node.collection[i] : null
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
/* Place the array where the user wants it */
|
|
115
|
+
const outMsg = { ...sourceMsg }; // shallow‑clone to keep headers etc.
|
|
116
|
+
if (node.outputPathType === 'msg') {
|
|
117
|
+
RED.util.setMessageProperty(outMsg, node.outputPath, result, true);
|
|
118
|
+
} else if (node.outputPathType === 'flow') {
|
|
119
|
+
node.context().flow.set(node.outputPath, result);
|
|
120
|
+
} else if (node.outputPathType === 'global') {
|
|
121
|
+
node.context().global.set(node.outputPath, result);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
node.status({
|
|
125
|
+
fill: 'green',
|
|
126
|
+
shape: 'dot',
|
|
127
|
+
text: `Complete: ${result.length} (${elapsed} ms)`
|
|
128
|
+
});
|
|
129
|
+
node.send([outMsg, null]);
|
|
130
|
+
|
|
131
|
+
/* Return to ready after a short pause */
|
|
132
|
+
setTimeout(setStatusReady, 2000);
|
|
133
|
+
resetCollection();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/* ────────────────────────────
|
|
137
|
+
░░ 4. Input handler ░░
|
|
138
|
+
──────────────────────────── */
|
|
139
|
+
node.on('input', function (msg, send, done) {
|
|
140
|
+
try {
|
|
141
|
+
/* 4.1 Basic sanity checks */
|
|
142
|
+
if (!msg.meta || typeof msg.meta.arrayPosition !== 'number') {
|
|
143
|
+
node.warn(
|
|
144
|
+
'Message missing array metadata. Expected msg.meta.arrayPosition (number) from array‑in node'
|
|
145
|
+
);
|
|
146
|
+
return done?.();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const position = msg.meta.arrayPosition;
|
|
150
|
+
|
|
151
|
+
/* 4.2 Validate range */
|
|
152
|
+
if (position < 0 || position >= node.expectedCount) {
|
|
153
|
+
node.warn(
|
|
154
|
+
`Position ${position} out of range 0‑${node.expectedCount - 1}`
|
|
155
|
+
);
|
|
156
|
+
return done?.();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/* 4.3 Extract data – prefer meta.arrayData, fall back to payload */
|
|
160
|
+
const data =
|
|
161
|
+
msg.meta && msg.meta.arrayData !== undefined
|
|
162
|
+
? msg.meta.arrayData
|
|
163
|
+
: msg.payload;
|
|
164
|
+
|
|
165
|
+
/* 4.4 Start collection if first element */
|
|
166
|
+
if (!node.isCollecting) {
|
|
167
|
+
node.isCollecting = true;
|
|
168
|
+
node.collection = {};
|
|
169
|
+
node.collectionStartTime = Date.now();
|
|
170
|
+
node.timeoutHandle = setTimeout(handleTimeout, node.timeout);
|
|
171
|
+
node.status({
|
|
172
|
+
fill: 'yellow',
|
|
173
|
+
shape: 'dot',
|
|
174
|
+
text: 'Collecting… (1/' + node.expectedCount + ')'
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/* 4.5 Store element */
|
|
179
|
+
node.collection[position] = data;
|
|
180
|
+
|
|
181
|
+
/* 4.6 Update collecting status */
|
|
182
|
+
const collected = Object.keys(node.collection).length;
|
|
183
|
+
node.status({
|
|
184
|
+
fill: 'yellow',
|
|
185
|
+
shape: 'dot',
|
|
186
|
+
text: `Collecting… (${collected}/${node.expectedCount})`
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
/* 4.7 Check completion */
|
|
190
|
+
if (collected === node.expectedCount) {
|
|
191
|
+
assembleAndOutput(msg);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
done?.();
|
|
195
|
+
} catch (err) {
|
|
196
|
+
node.status({ fill: 'red', shape: 'ring', text: 'Error' });
|
|
197
|
+
resetCollection();
|
|
198
|
+
if (done) done(err);
|
|
199
|
+
else node.error(err, msg);
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
/* ────────────────────────────
|
|
204
|
+
░░ 5. Cleanup ░░
|
|
205
|
+
──────────────────────────── */
|
|
206
|
+
node.on('close', function () {
|
|
207
|
+
resetCollection();
|
|
208
|
+
node.status({});
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
RED.nodes.registerType('array-out', ArrayOutNode);
|
|
213
|
+
};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType('array-select', {
|
|
3
|
+
category: 'RP Image',
|
|
4
|
+
color: '#7b8dff',
|
|
5
|
+
defaults: {
|
|
6
|
+
name: { value: "" },
|
|
7
|
+
inputPath: { value: "payload" },
|
|
8
|
+
inputPathType: { value: "msg" },
|
|
9
|
+
outputPath: { value: "payload" },
|
|
10
|
+
outputPathType: { value: "msg" },
|
|
11
|
+
selection: { value: "0" },
|
|
12
|
+
asArray: { value: false }
|
|
13
|
+
},
|
|
14
|
+
inputs: 1,
|
|
15
|
+
outputs: 1,
|
|
16
|
+
icon: "font-awesome/fa-columns",
|
|
17
|
+
label: function() {
|
|
18
|
+
if (this.name) return this.name;
|
|
19
|
+
return `Array Select [${this.selection}]`;
|
|
20
|
+
},
|
|
21
|
+
oneditprepare: function() {
|
|
22
|
+
// Set up TypedInput for input path
|
|
23
|
+
$("#node-input-inputPath").typedInput({
|
|
24
|
+
default: 'msg',
|
|
25
|
+
types: ['msg', 'flow', 'global'],
|
|
26
|
+
typeField: "#node-input-inputPathType"
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Set up TypedInput for output path
|
|
30
|
+
$("#node-input-outputPath").typedInput({
|
|
31
|
+
default: 'msg',
|
|
32
|
+
types: ['msg', 'flow', 'global'],
|
|
33
|
+
typeField: "#node-input-outputPathType"
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
</script>
|
|
38
|
+
|
|
39
|
+
<script type="text/x-red" data-template-name="array-select">
|
|
40
|
+
<div class="form-row">
|
|
41
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
42
|
+
<input type="text" id="node-input-name" placeholder="Name">
|
|
43
|
+
</div>
|
|
44
|
+
|
|
45
|
+
<hr>
|
|
46
|
+
|
|
47
|
+
<div class="form-row">
|
|
48
|
+
<label for="node-input-inputPath"><i class="fa fa-sign-in"></i> Input from</label>
|
|
49
|
+
<input type="text" id="node-input-inputPath" style="width: 70%;">
|
|
50
|
+
<input type="hidden" id="node-input-inputPathType">
|
|
51
|
+
</div>
|
|
52
|
+
|
|
53
|
+
<div class="form-row">
|
|
54
|
+
<label for="node-input-outputPath"><i class="fa fa-sign-out"></i> Output to</label>
|
|
55
|
+
<input type="text" id="node-input-outputPath" style="width: 70%;">
|
|
56
|
+
<input type="hidden" id="node-input-outputPathType">
|
|
57
|
+
</div>
|
|
58
|
+
|
|
59
|
+
<div class="form-row">
|
|
60
|
+
<label for="node-input-selection"><i class="fa fa-filter"></i> Selection</label>
|
|
61
|
+
<input type="text" id="node-input-selection" style="width: 70%;" placeholder="e.g., 0, 1,3,5, 1:4, 0::2">
|
|
62
|
+
</div>
|
|
63
|
+
|
|
64
|
+
<div class="form-row">
|
|
65
|
+
<label for="node-input-asArray"><i class="fa fa-list"></i> Force Array</label>
|
|
66
|
+
<input type="checkbox" id="node-input-asArray">
|
|
67
|
+
<span style="margin-left: 5px;">Always output as array (even for single items)</span>
|
|
68
|
+
</div>
|
|
69
|
+
|
|
70
|
+
<div class="form-tips">
|
|
71
|
+
<b>Selection Examples:</b><br>
|
|
72
|
+
• <code>0</code> - First element<br>
|
|
73
|
+
• <code>1,3,5</code> - Elements at indices 1, 3, 5<br>
|
|
74
|
+
• <code>1:4</code> - Elements 1 to 3 (range)<br>
|
|
75
|
+
• <code>0::2</code> - Every 2nd element starting from 0<br>
|
|
76
|
+
• <code>-1</code> - Last element<br>
|
|
77
|
+
• <code>-2:-1</code> - Last two elements
|
|
78
|
+
</div>
|
|
79
|
+
</script>
|
|
80
|
+
|
|
81
|
+
<script type="text/x-red" data-help-name="array-select">
|
|
82
|
+
<p>Selects specific elements from an input array using flexible selection syntax including indices, ranges, and patterns.</p>
|
|
83
|
+
|
|
84
|
+
<h3>Details</h3>
|
|
85
|
+
<p>This node provides powerful array element selection capabilities with Python-like slicing syntax. It can extract single elements, multiple specific indices, ranges, or patterns from arrays. The output format is automatically determined based on the selection size, with an option to force array output.</p>
|
|
86
|
+
|
|
87
|
+
<h3>Properties</h3>
|
|
88
|
+
<dl class="message-properties">
|
|
89
|
+
<dt>Input from <span class="property-type">string</span></dt>
|
|
90
|
+
<dd>The location to read the input array from. You can select between <code>msg</code>, <code>flow</code>, or <code>global</code> context. Defaults to <code>msg.payload</code>.</dd>
|
|
91
|
+
<dt>Output to <span class="property-type">string</span></dt>
|
|
92
|
+
<dd>The location where to write the selected element(s). You can select between <code>msg</code>, <code>flow</code>, or <code>global</code> context. Defaults to <code>msg.payload</code>.</dd>
|
|
93
|
+
<dt>Selection <span class="property-type">string</span></dt>
|
|
94
|
+
<dd>Specify which array elements to select using various formats: single indices, comma-separated lists, ranges, or step patterns.</dd>
|
|
95
|
+
<dt>Force Array <span class="property-type">boolean</span></dt>
|
|
96
|
+
<dd>When checked, always outputs an array even for single elements. Default: false (single elements output directly).</dd>
|
|
97
|
+
</dl>
|
|
98
|
+
|
|
99
|
+
<h3>Inputs</h3>
|
|
100
|
+
<dl class="message-properties">
|
|
101
|
+
<dt>payload <span class="property-type">array</span></dt>
|
|
102
|
+
<dd>Input array from which to select elements. <strong>Must be an array</strong> - the node will warn and skip processing if input is not an array.</dd>
|
|
103
|
+
</dl>
|
|
104
|
+
|
|
105
|
+
<h3>Outputs</h3>
|
|
106
|
+
<dl class="message-properties">
|
|
107
|
+
<dt>configured output <span class="property-type">any | array</span></dt>
|
|
108
|
+
<dd>Selected element(s) written to the configured output path. Single elements output directly unless "Force Array" is enabled. Multiple elements always output as array.</dd>
|
|
109
|
+
</dl>
|
|
110
|
+
|
|
111
|
+
<h3>Selection Syntax</h3>
|
|
112
|
+
<ul>
|
|
113
|
+
<li><strong>Single Index:</strong> <code>0</code>, <code>2</code>, <code>-1</code> (negative indices count from end)</li>
|
|
114
|
+
<li><strong>Multiple Indices:</strong> <code>0,2,4</code>, <code>1,3,5</code> (comma-separated)</li>
|
|
115
|
+
<li><strong>Range:</strong> <code>1:4</code> (indices 1 to 3), <code>0:5</code> (indices 0 to 4)</li>
|
|
116
|
+
<li><strong>Step Range:</strong> <code>0::2</code> (every 2nd element), <code>1:7:2</code> (indices 1,3,5)</li>
|
|
117
|
+
<li><strong>Negative Ranges:</strong> <code>-2:</code> (last two elements), <code>:-2</code> (all but last two)</li>
|
|
118
|
+
</ul>
|
|
119
|
+
|
|
120
|
+
<h3>Examples</h3>
|
|
121
|
+
<p><strong>Input Array:</strong> <code>["A", "B", "C", "D", "E"]</code></p>
|
|
122
|
+
<ul>
|
|
123
|
+
<li><strong>Single:</strong> <code>0</code> → <code>"A"</code> (first element)</li>
|
|
124
|
+
<li><strong>Multiple:</strong> <code>1,3</code> → <code>["B", "D"]</code> (specific indices)</li>
|
|
125
|
+
<li><strong>Range:</strong> <code>1:4</code> → <code>["B", "C", "D"]</code> (slice range)</li>
|
|
126
|
+
<li><strong>Step:</strong> <code>0::2</code> → <code>["A", "C", "E"]</code> (every 2nd element)</li>
|
|
127
|
+
<li><strong>Negative:</strong> <code>-1</code> → <code>"E"</code> (last element)</li>
|
|
128
|
+
<li><strong>Last Two:</strong> <code>-2:</code> → <code>["D", "E"]</code> (from 2nd last to end)</li>
|
|
129
|
+
</ul>
|
|
130
|
+
|
|
131
|
+
<h3>Node Interactions</h3>
|
|
132
|
+
<p><strong>Works with:</strong> Array-out (to extract from assembled arrays), Transform nodes (to select processed results), any node that produces arrays</p>
|
|
133
|
+
<p><strong>Common Patterns:</strong> Array-out → Array-select (extract specific results), Transform → Array-select (pick best results), Image processing → Array-select (choose specific images)</p>
|
|
134
|
+
|
|
135
|
+
<h3>Output Behavior</h3>
|
|
136
|
+
<ul>
|
|
137
|
+
<li><strong>Single Element:</strong> Outputs the element directly (unless "Force Array" is checked)</li>
|
|
138
|
+
<li><strong>Multiple Elements:</strong> Always outputs as an array</li>
|
|
139
|
+
<li><strong>Empty Selection:</strong> Outputs empty array <code>[]</code></li>
|
|
140
|
+
<li><strong>Invalid Selection:</strong> No message sent, warning logged</li>
|
|
141
|
+
<li><strong>Out of Bounds:</strong> Invalid indices are ignored, warning logged</li>
|
|
142
|
+
</ul>
|
|
143
|
+
|
|
144
|
+
<h3>Error Handling</h3>
|
|
145
|
+
<p>Comprehensive validation and error reporting:</p>
|
|
146
|
+
<ul>
|
|
147
|
+
<li><strong>Non-array input:</strong> Warning logged, message not propagated</li>
|
|
148
|
+
<li><strong>Empty array:</strong> Warning logged, selection processed normally</li>
|
|
149
|
+
<li><strong>Invalid syntax:</strong> Warning logged, message not propagated</li>
|
|
150
|
+
<li><strong>Out-of-bounds indices:</strong> Invalid indices skipped, warning logged</li>
|
|
151
|
+
<li><strong>Status display:</strong> Shows current selection and processing state</li>
|
|
152
|
+
</ul>
|
|
153
|
+
|
|
154
|
+
<h3>Performance</h3>
|
|
155
|
+
<p>Selection operations are performed efficiently with minimal memory copying. Node status displays selection information and processing state.</p>
|
|
156
|
+
</script>
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Node.js logic for the Array-Select node with single output and flexible selection.
|
|
3
|
+
* @author Rosepetal
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
module.exports = function(RED) {
|
|
7
|
+
function ArraySelectNode(config) {
|
|
8
|
+
RED.nodes.createNode(this, config);
|
|
9
|
+
const node = this;
|
|
10
|
+
|
|
11
|
+
// Store configuration
|
|
12
|
+
node.inputPath = config.inputPath || 'payload';
|
|
13
|
+
node.inputPathType = config.inputPathType || 'msg';
|
|
14
|
+
node.outputPath = config.outputPath || 'payload';
|
|
15
|
+
node.outputPathType = config.outputPathType || 'msg';
|
|
16
|
+
node.selection = config.selection || '0';
|
|
17
|
+
node.asArray = config.asArray || false;
|
|
18
|
+
|
|
19
|
+
// Set initial status
|
|
20
|
+
node.status({
|
|
21
|
+
fill: "blue",
|
|
22
|
+
shape: "dot",
|
|
23
|
+
text: `Ready: ${node.selection}`
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Parse selection string and return indices
|
|
28
|
+
* @param {string} selection - Selection string (e.g., "0", "1,3,5", "1:4", "0::2")
|
|
29
|
+
* @param {number} arrayLength - Length of input array
|
|
30
|
+
* @returns {Array} Array of indices or null if invalid
|
|
31
|
+
*/
|
|
32
|
+
function parseSelection(selection, arrayLength) {
|
|
33
|
+
if (!selection || selection.trim() === '') return null;
|
|
34
|
+
|
|
35
|
+
const sel = selection.trim();
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
// Single index (e.g., "2" or "-1")
|
|
39
|
+
if (/^-?\d+$/.test(sel)) {
|
|
40
|
+
const index = parseInt(sel);
|
|
41
|
+
const normalizedIndex = index < 0 ? arrayLength + index : index;
|
|
42
|
+
if (normalizedIndex >= 0 && normalizedIndex < arrayLength) {
|
|
43
|
+
return [normalizedIndex];
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Multiple indices (e.g., "0,2,4")
|
|
49
|
+
if (sel.includes(',')) {
|
|
50
|
+
const indices = sel.split(',').map(s => {
|
|
51
|
+
const index = parseInt(s.trim());
|
|
52
|
+
return index < 0 ? arrayLength + index : index;
|
|
53
|
+
}).filter(i => i >= 0 && i < arrayLength);
|
|
54
|
+
return indices.length > 0 ? indices : null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Range (e.g., "1:4" or "1:4:2")
|
|
58
|
+
if (sel.includes(':')) {
|
|
59
|
+
const parts = sel.split(':');
|
|
60
|
+
if (parts.length === 2) {
|
|
61
|
+
// Simple range: start:end
|
|
62
|
+
let start = parseInt(parts[0]) || 0;
|
|
63
|
+
let end = parseInt(parts[1]) || arrayLength;
|
|
64
|
+
start = start < 0 ? arrayLength + start : start;
|
|
65
|
+
end = end < 0 ? arrayLength + end : end;
|
|
66
|
+
|
|
67
|
+
const indices = [];
|
|
68
|
+
for (let i = Math.max(0, start); i < Math.min(arrayLength, end); i++) {
|
|
69
|
+
indices.push(i);
|
|
70
|
+
}
|
|
71
|
+
return indices.length > 0 ? indices : null;
|
|
72
|
+
} else if (parts.length === 3) {
|
|
73
|
+
// Step range: start:end:step
|
|
74
|
+
let start = parseInt(parts[0]) || 0;
|
|
75
|
+
let end = parseInt(parts[1]) || arrayLength;
|
|
76
|
+
let step = parseInt(parts[2]) || 1;
|
|
77
|
+
start = start < 0 ? arrayLength + start : start;
|
|
78
|
+
end = end < 0 ? arrayLength + end : end;
|
|
79
|
+
|
|
80
|
+
if (step <= 0) step = 1;
|
|
81
|
+
|
|
82
|
+
const indices = [];
|
|
83
|
+
for (let i = Math.max(0, start); i < Math.min(arrayLength, end); i += step) {
|
|
84
|
+
indices.push(i);
|
|
85
|
+
}
|
|
86
|
+
return indices.length > 0 ? indices : null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return null;
|
|
91
|
+
} catch (err) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Handle incoming messages
|
|
97
|
+
node.on('input', function(msg, send, done) {
|
|
98
|
+
try {
|
|
99
|
+
// Get data from configured input path
|
|
100
|
+
let inputArray;
|
|
101
|
+
if (node.inputPathType === 'msg') {
|
|
102
|
+
inputArray = RED.util.getMessageProperty(msg, node.inputPath);
|
|
103
|
+
} else if (node.inputPathType === 'flow') {
|
|
104
|
+
inputArray = node.context().flow.get(node.inputPath);
|
|
105
|
+
} else if (node.inputPathType === 'global') {
|
|
106
|
+
inputArray = node.context().global.get(node.inputPath);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Validate input is an array
|
|
110
|
+
if (!Array.isArray(inputArray)) {
|
|
111
|
+
node.warn(`Input must be an array. Received: ${typeof inputArray}`);
|
|
112
|
+
node.status({ fill: "red", shape: "ring", text: "Invalid input: not an array" });
|
|
113
|
+
return done?.();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (inputArray.length === 0) {
|
|
117
|
+
node.warn('Input array is empty');
|
|
118
|
+
node.status({ fill: "yellow", shape: "ring", text: "Empty array" });
|
|
119
|
+
return done?.();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Process selection
|
|
123
|
+
const indices = parseSelection(node.selection, inputArray.length);
|
|
124
|
+
|
|
125
|
+
if (indices && indices.length > 0) {
|
|
126
|
+
const selectedItems = indices.map(idx => inputArray[idx]);
|
|
127
|
+
|
|
128
|
+
// Determine output format
|
|
129
|
+
const result = (node.asArray || selectedItems.length > 1) ? selectedItems : selectedItems[0];
|
|
130
|
+
|
|
131
|
+
// Create clean output message with only the selected result
|
|
132
|
+
let outputMsg = {};
|
|
133
|
+
|
|
134
|
+
// Set output based on configured path
|
|
135
|
+
if (node.outputPathType === 'msg') {
|
|
136
|
+
RED.util.setMessageProperty(outputMsg, node.outputPath, result, true);
|
|
137
|
+
} else if (node.outputPathType === 'flow') {
|
|
138
|
+
node.context().flow.set(node.outputPath, result);
|
|
139
|
+
// For flow context, send minimal message (no payload)
|
|
140
|
+
} else if (node.outputPathType === 'global') {
|
|
141
|
+
node.context().global.set(node.outputPath, result);
|
|
142
|
+
// For global context, send minimal message (no payload)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
node.status({
|
|
146
|
+
fill: "green",
|
|
147
|
+
shape: "dot",
|
|
148
|
+
text: `Selected ${selectedItems.length} from [${inputArray.length}]`
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
send(outputMsg);
|
|
152
|
+
} else {
|
|
153
|
+
// Invalid selection
|
|
154
|
+
node.warn(`Invalid selection "${node.selection}" for array of length ${inputArray.length}`);
|
|
155
|
+
node.status({ fill: "red", shape: "ring", text: "Invalid selection" });
|
|
156
|
+
return done?.();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Reset status after a delay
|
|
160
|
+
setTimeout(() => {
|
|
161
|
+
node.status({
|
|
162
|
+
fill: "blue",
|
|
163
|
+
shape: "dot",
|
|
164
|
+
text: `Ready: ${node.selection}`
|
|
165
|
+
});
|
|
166
|
+
}, 2000);
|
|
167
|
+
|
|
168
|
+
done?.();
|
|
169
|
+
|
|
170
|
+
} catch (err) {
|
|
171
|
+
node.status({ fill: "red", shape: "ring", text: "Error" });
|
|
172
|
+
if (done) {
|
|
173
|
+
done(err);
|
|
174
|
+
} else {
|
|
175
|
+
node.error(err, msg);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// Clean up on node removal
|
|
181
|
+
node.on('close', function() {
|
|
182
|
+
node.status({});
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
RED.nodes.registerType("array-select", ArraySelectNode);
|
|
187
|
+
};
|