-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (32 loc) · 1.1 KB
/
index.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
// External
import findIndex from 'lodash.findindex';
import clone from 'lodash.clone';
// Helper
const av = (prop) => prop !== undefined && prop !== null ? true : false;
const isObj = (prop) => Object.prototype.toString.call(prop) === '[object Object]' ? true : false;
export default (payload) => {
// Expect a object
if(!isObj(payload)) throw new Error('Array-Portal expects an object as payload');
const { input: i, output: o, caller } = payload;
const input = clone(i);
const output = clone(o);
if(!av(input) || !av(output) || !av(caller)) {
throw new Error('Array-Portal expects an object with an input and output array and an caller');
}
let inputRes = input || [];
let outputRes = output || [];
let callerFoundIndex = -1;
if(isObj(caller)) {
callerFoundIndex = findIndex(input, caller);
} else {
callerFoundIndex = Array.isArray(input) ? input.indexOf(caller) : -1;
}
if(callerFoundIndex > -1) {
outputRes = outputRes.concat([inputRes[callerFoundIndex]]);
inputRes.splice(callerFoundIndex, 1);
}
return {
input: inputRes,
output: outputRes
};
}