forked from metatech-university/Paradigms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-execute.js
More file actions
33 lines (28 loc) · 838 Bytes
/
3-execute.js
File metadata and controls
33 lines (28 loc) · 838 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
'use strict';
const reader = ({ id }) => ({ id, name: 'marcus', age: 42 });
const execute = (plan) => (reader, log, env = {}) => {
if (plan.read) {
const user = reader(plan.read);
return execute(plan.then)(reader, log, { user });
}
if (plan.match) {
const ok = env.user.name === plan.match.name;
return execute(ok ? plan.success : plan.fail)(reader, log, env);
}
if (plan.effect) {
if (plan.effect.log) return () => log(env.user[plan.effect.log]);
if (plan.effect === 'noop') return () => {};
}
};
execute({
read: { id: 15 },
then: {
match: { name: 'marcus' },
success: { effect: { log: 'age' } },
fail: { effect: 'noop' },
},
})(reader, console.log)();
// 1. Rewrite in OOP style
// 2. Improve data structure inconsistence
// const main = new Exec(options);
// main.run(steps);