Code coverage report for lib/ActionCreator.js

Statements: 94.12% (16 / 17)      Branches: 83.33% (5 / 6)      Functions: 100% (4 / 4)      Lines: 94.12% (16 / 17)      Ignored: none     

All files » lib/ » ActionCreator.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 65 66 67 68 69 70 71 72 73 74 75 76                1   1 1                         1   1   2   2   2         3   3   3                           1   3         3                 1 1      
/**
 * @file 动作工厂便利小工具 暂不开放
 * @ignore
 * @author Leon(leon@outlook.com)
 * @module ActionCreator
 */
 
 
var u = require('underscore');
 
var ACTION_ATTR = 'EI_ACTION_TAG';
var ACTION_TAG = 'EI_ACTION';
 
 
/**
 * 生成一个Action工厂函数
 *
 * @inner
 *
 * @param {string} type action类型
 * @param {?Function} factory 工厂函数
 *
 * @return {Function}
 */
function createFactory(type, factory) {
 
    function ActionCreator(payload) {
 
        var action = u.isFunction(factory) ? factory.apply(null, arguments) : {};
 
        action.type = type;
 
        return action;
 
    }
 
    // 给工厂函数添加一个标识
    ActionCreator[ACTION_ATTR] = ACTION_TAG;
 
    ActionCreator.type = type;
 
    return ActionCreator;
 
}
 
/**
 * 生成一个ActionCreator
 *
 * @method module:ActionCreator.extend
 *
 * @param {string} type 类型
 * @param {Function} factory 工厂函数
 *
 * @return {Function}
 */
exports.extend = function (type, factory) {
 
    Iif (!u.isString(type)) {
        throw new Error('Action Creator must have a type');
    }
 
    // 我们提供一个很简单的工厂函数
    return createFactory(type, factory);
};
 
/**
 * 是否为一个ActionCreator
 *
 * @param {*} action 待判断的对象
 * @return {boolean}
 */
exports.is = function (action) {
    return action && action[ACTION_ATTR] === ACTION_TAG;
};