Project

General

Profile

1
import { extend, isArray, isFunction, isUndefined, hasOwn } from './index';
2

    
3
var strats = {};
4

    
5
// concat strategy
6
strats.args =
7
strats.created =
8
strats.events =
9
strats.init =
10
strats.ready =
11
strats.connected =
12
strats.disconnected =
13
strats.destroy = function (parentVal, childVal) {
14

    
15
    parentVal = parentVal && !isArray(parentVal) ? [parentVal] : parentVal;
16

    
17
    return childVal
18
        ? parentVal
19
            ? parentVal.concat(childVal)
20
            : isArray(childVal)
21
                ? childVal
22
                : [childVal]
23
        : parentVal;
24
};
25

    
26
// update strategy
27
strats.update = function (parentVal, childVal) {
28
    return strats.args(parentVal, isFunction(childVal) ? {write: childVal} : childVal);
29
};
30

    
31
// property strategy
32
strats.props = function (parentVal, childVal) {
33

    
34
    if (isArray(childVal)) {
35
        childVal = childVal.reduce((value, key) => {
36
            value[key] = String;
37
            return value;
38
        }, {});
39
    }
40

    
41
    return strats.methods(parentVal, childVal);
42
};
43

    
44
// extend strategy
45
strats.computed =
46
strats.defaults =
47
strats.methods = function (parentVal, childVal) {
48
    return childVal
49
        ? parentVal
50
            ? extend(true, {}, parentVal, childVal)
51
            : childVal
52
        : parentVal;
53
};
54

    
55
// default strategy
56
var defaultStrat = function (parentVal, childVal) {
57
    return isUndefined(childVal) ? parentVal : childVal;
58
};
59

    
60
export function mergeOptions(parent, child) {
61

    
62
    var options = {}, key;
63

    
64
    if (child.mixins) {
65
        for (let i = 0, l = child.mixins.length; i < l; i++) {
66
            parent = mergeOptions(parent, child.mixins[i]);
67
        }
68
    }
69

    
70
    for (key in parent) {
71
        mergeKey(key);
72
    }
73

    
74
    for (key in child) {
75
        if (!hasOwn(parent, key)) {
76
            mergeKey(key);
77
        }
78
    }
79

    
80
    function mergeKey(key) {
81
        options[key] = (strats[key] || defaultStrat)(parent[key], child[key]);
82
    }
83

    
84
    return options;
85
}
(7-7/9)