Util.js 1.3 KB
define(function(require) {
    'use strict';

    function mergeStatic(startPtr, endPtr, obj, fieldName, filterFunc) {
        var result = filterFunc(obj);
        var ptr = startPtr;
        while (true) {
            result = _.extend(result, filterFunc(_.result(ptr, fieldName)));
            if (ptr === endPtr) {
                break;
            }
            ptr = ptr.__super__.constructor;
        }
        return result;
    }

    return {
        getField: function getField(obj, key) {
            return obj[key];
        },
        setField: function getField(obj, key, value) {
            return obj[key] = value;
        },
        getItemKeyAccessor: function getItemKeyAccessor(item) {
            return item.get('key');
        },
        mergeStaticProps: function mergeStaticProps(startPtr, endPtr, obj, fieldName) {
            return mergeStatic(startPtr, endPtr, obj, fieldName, _.identity);
        },
        mergeStaticSets: function mergeStaticSets(startPtr, endPtr, obj, fieldName) {
            return _.keys(mergeStatic(startPtr, endPtr, obj, fieldName, function arrayToMap(array) {
                var result = {};
                _.each(array, function(value) {
                    result[value] = true;
                });
                return result;
            }));
        },
    };
});