-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (36 loc) · 1.19 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
const last = xs => xs[xs.length - 1];
const head = xs => xs[0];
const tail = xs => xs.slice(1);
const init = xs => xs.slice(0, -1);
const createContainer = (...mixins) => {
return (...constructorArgs) => {
const instances = mixins.map(Mixin => new Mixin(...constructorArgs));
const strategies = mixins.reduce(
(strategies, { strategies: mixinStrategies = {} }) => {
Object.entries(mixinStrategies).forEach(([method, strategy]) => {
strategies[method] = strategy(
instances.map(instance => instance[method].bind(instance))
);
});
return strategies;
},
{}
);
return strategies;
};
};
module.exports.createContainer = createContainer;
const strategies = {};
strategies.override = methods => (...args) => last(methods)(...args);
strategies.parallel = methods => (...args) => methods.map(m => m(...args));
strategies.pipe = methods => (...args) =>
tail(methods).reduce(
(result, method) => method(result),
head(methods)(...args)
);
strategies.compose = methods => (...args) =>
init(methods).reduceRight(
(result, method) => method(result),
last(methods)(...args)
);
module.exports.strategies = strategies;