-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsplit.js
46 lines (36 loc) · 1.25 KB
/
split.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
const _ = require('lodash');
const { messages } = require('elasticio-node');
/** @this processAction */
async function processAction(msg, conf) {
const splitting = conf.splitter || {};
const body = msg.body || {};
this.logger.debug('Received new message');
const split = splitting === '$' ? body : _.get(body, splitting);
if (!split) {
await this.emit('error', new Error(`Could not find properties by following path: "${splitting}"!`));
return;
}
if (!_.isObject(split)) {
await this.emit('error', new Error('Only objects are accepted!'));
return;
}
if (_.isArray(split) && _.find(split, (elem) => !_.isObject(elem))) {
await this.emit('error', new Error('Splitting arrays of objects only!'));
return;
}
const results = [];
if (_.isArray(split)) {
split.forEach((elem) => results.push(elem));
} else if (_.isObject(split)) {
this.logger.debug('Splitting is not an array. Returning the original object');
results.push(split);
}
this.logger.info('Splitting the incoming message into %s messages', results.length);
results.forEach(async (result) => {
if (result) {
await this.emit('data', messages.newMessageWithBody(result));
}
});
await this.emit('end');
}
exports.process = processAction;