Code coverage report for lib/util/bindSelectors.js

Statements: 100% (14 / 14)      Branches: 100% (8 / 8)      Functions: 100% (3 / 3)      Lines: 100% (14 / 14)      Ignored: none     

All files » lib/util/ » bindSelectors.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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64          1   1                   1   12   11   10       1       2         1 1               2       4     1                   1  
/**
 * @file 合成`store`选择器
 * @author Leon(leon@outlook.com)
 */
 
var u = require('underscore');
 
var invariant = require('./invariant');
 
/**
 * 将若干个数据选择器绑定到一个`store`上
 *
 * @private
 * @param {*}       selectors 选择器map
 * @param {!Object} store     数据仓库
 * @return {Function}
 */
function bindSelectors(selectors) {
 
    return function (store) {
 
        invariant(store, 'need store');
 
        switch (typeof selectors) {
 
            case 'function':
 
                return selectors(store);
 
            case 'object':
 
                return u
                    .chain(selectors)
                    .pick(u.isFunction)
                    .reduce(
                        function (result, select, name) {
                            result[name] = select(store[name]);
                            return result;
                        },
                        {}
                    )
                    .value();
 
            case 'number': case 'string':
 
                return store[selectors];
 
            case 'boolean':
 
                return selectors ? store : {};
 
            default:
                return {};
 
        }
 
    };
 
 
 
}
 
module.exports = bindSelectors;