# all files / src/ util.js

73.47% Statements 36/49
51.43% Branches 18/35
71.43% Functions 10/14
73.47% Lines 36/49

    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
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161

    7×
    6×
    10×
    10×

    7×

    3×

    3×

    3×
    3×

    1×
    1×

    1×
    3×

    1×

    1×

    3×

    2×

    1×

    6×
    5×

    6×

    6×

    6×
    52×

    6×

    1×
    1×

    1×
    1×

    1×

    9×
    3×

    6×
    12×

    1×

    1×

     /**
     * 复制一个对象的属性到另一个对象
     *
     * @param {any} obj
     * @param {any} props
     * @returns
     */
    export function extend(obj, props) {
        if (props) {
            for (let i in props) {
                Eif (props.hasOwnProperty(i))
                    obj[i] = props[i]
            }
        }
        return obj
    }
    /**
     * 一个空函数
     *
     * @export
     */
    export function noop() {}
    /**
     * 类继承
     *
     * @export
     * @param {any} SubClass
     * @param {any} SupClass
     */
    export function inherit(SubClass, SupClass) {
        function Bridge() {}
        Bridge.prototype = SupClass.prototype

        let fn = SubClass.prototype = new Bridge()

        // 避免原型链拉长导致方法查找的性能开销
        extend(fn, SupClass.prototype)
        fn.constructor = SubClass
    }
    /**
     * 收集一个元素的所有孩子
     *
     * @export
     * @param {any} dom
     * @returns
     */
    export function getNodes(dom) {
        var ret = [],
            c = dom.childNodes || []
        // eslint-disable-next-line
        for (var i = 0, el; el = c[i++];) {
            ret.push(el)
        }
        return ret
    }
    var lowerCache = {}

    export function toLowerCase(s) {
        return lowerCache[s] || (lowerCache[s] = s.toLowerCase())
    }

    /**
     *
     *
     * @param {any} type
     * @returns
     */
    export function isFn(type) {
        return typeof type === 'function'
    }

    var rword = /[^, ]+/g

    export function oneObject(array, val) {
        if (typeof array === 'string') {
            array = array.match(rword) || []
        }
        var result = {},
         //eslint-disable-next-line
            value = val !== void 666
                ? val
                : 1
        for (var i = 0, n = array.length; i < n; i++) {
            result[array[i]] = value
        }
        return result
    }

    export function getChildContext(instance, context) {
        Eif (instance.getChildContext) {
            return extend(extend({}, context), instance.getChildContext())
        }
        return context
    }
    var rcamelize = /[-_][^-_]/g
    export var __push = Array.prototype.push

    export var HTML_KEY = 'dangerouslySetInnerHTML'
    export function camelize(target) {
        //提前判断，提高getStyle等的效率
        if (!target || target.indexOf('-') < 0 && target.indexOf('_') < 0) {
            return target
        }
        //转换为驼峰风格
        return target.replace(rcamelize, function (match) {
            return match
                .charAt(1)
                .toUpperCase()
        })
    }

    export var options = {
        updateBatchNumber: 1,
        immune: {} // Object.freeze(midway) ;midway.aaa = 'throw err';midway.immune.aaa = 'safe'
    }

    export function checkNull(vnode, type) {
        if (vnode === null || vnode === false) {
            return {type: '#comment', text: 'empty'}
        } else if (!vnode || !vnode.vtype) {
            throw new Error(`@${type.name}#render:You may have returned undefined, an array or some other invalid object`)
        }
        return vnode
    }
    export function getComponentProps(type, props) {
        var defaultProps = type.defaultProps
        props = extend({}, props) //注意，上面传下来的props已经被冻结，无法修改，需要先复制一份
        for (var i in defaultProps) {
            //eslint-disable-next-line
            if (props[i] === void 666) {
                props[i] = defaultProps[i]
            }
        }
        return props
    }
    /**
     * 获取虚拟DOM对应的顶层组件实例的类型
     *
     * @param {any} vnode
     * @param {any} instance
     * @param {any} pool
     */

    export function getComponentName(type) {
        return typeof type === 'function'
            ? (type.displayName || type.name)
            : type
    }
    export var recyclables = {
        '#text': [],
        '#comment': [],
        'span': [],
        'div': [],
        'td': [],
        'p': []
    }