src/applyMiddleware.js   A
last analyzed

Complexity

Total Complexity 6
Complexity/F 2

Size

Lines of Code 27
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 27
rs 10
bpm 1
cpm 2
noi 0
1
export default function applyMiddleware(
2
    applyTo,
3
    middlewares = [],
4
    options = {},
5
    params = {},
6
    resourceId = '',
7
    type = ''
8
) {
9
10
    middlewares = [].concat(middlewares).filter(func => typeof func === 'function');
11
12
    if (!middlewares.length) {
13
        return applyTo(params);
14
    }
15
16
    return middlewares.reduceRight((prev, middleware, index) => {
17
18
        const next = index === middlewares.length - 1
19
            ? prev.bind(null, params)
20
            : prev.bind(null, options, params, resourceId, type);
21
22
        return index === 0
23
            ? middleware(next)(options, params, resourceId, type)
24
            : middleware(next);
25
    }, applyTo);
26
27
}
28