Completed
Push — master ( 2040ba...cef9c4 )
by Vitaly
02:22
created

src/applyMiddleware.js   A

Complexity

Total Complexity 6
Complexity/F 2

Size

Lines of Code 20
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 6
c 1
b 0
f 0
nc 1
mnd 1
bc 3
fnc 3
dl 0
loc 20
rs 10
bpm 1
cpm 2
noi 0
1
export default function applyMiddleware(applyTo, middlewares = [], options = {}, params = {}, resourceId = '', method = '') {
2
3
    middlewares = [].concat(middlewares).filter(func => typeof func === 'function');
4
5
    if (!middlewares.length) {
6
        return applyTo(params);
7
    }
8
9
    return middlewares.reduceRight((prev, middleware, index) => {
10
11
        const next = index === middlewares.length - 1
12
            ? prev.bind(null, params)
13
            : prev.bind(null, options, params, resourceId, method);
14
15
        return index === 0
16
            ? middleware(next)(options, params, resourceId, method)
17
            : middleware(next);
18
    }, applyTo);
19
20
}
21