-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe.js
More file actions
35 lines (32 loc) · 685 Bytes
/
pipe.js
File metadata and controls
35 lines (32 loc) · 685 Bytes
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
let test = {
a: {
b: (a, b, c) => a + b + c,
c: (a, b, c) => a + b - c,
},
d: (a, b, c) => a - b - c,
e: 1,
f: true,
};
function calculate(obj, ...args) {
if (typeof obj !== "object") {
return obj;
}
const newObj = {};
let keys = Object.keys(obj);
for (let key of keys) {
if (typeof obj[key] === "object") {
newObj[key] = calculate(obj[key], ...args);
} else if (typeof obj[key] === "function") {
newObj[key] = obj[key](...args);
} else {
newObj[key] = obj[key];
}
}
return newObj;
}
function pipe(obj) {
return function (...args) {
return calculate(obj, ...args);
};
}
console.log(pipe(test)(1, 1, 1));