/**
 * 将源对象的属性值解析到目标对象已存在的属性
 * @param {Object} target 
 * @param  {...Object} sources
 * @returns target
 * give({a:1,b:2},{b:3,c:4})
 * => {a:1,b:3}
 */
export const give = (target, ...sources) => {
    target = Object(target)
    sources.forEach(source => {
        if (source) {
            source = Object(source)
            for (const key in target) {
                if (source.hasOwnProperty(key)) {
                    const _s = source[key]
                    if (_s !== undefined && _s !== null) {
                        target[key] = _s
                    }
                }
            }
        }
    })
    return target
}

/**
 * 同give,深度遍历
 * @param {Object} target
 * @param  {...Object} sources
 * @returns target
 * giveDeep({a:1,b:{x:1,y:2}},{b:{x:3,z:4},c:3})
 * => {a:1,b:{x:3,y:2}}
 */
export const giveDeep = (target, ...sources) => {
    const _deep = (_target, _source) => {
        for (const key in _target) {
            if (_source.hasOwnProperty(key)) {
                const _t = _target[key],
                    _s = _source[key]
                if (_s !== undefined && _s !== null) {
                    if (_t && _t.constructor === Object) {
                        _deep(_t, _s)
                    } else {
                        _target[key] = _s
                    }
                }
            }
        }
    }
    target = Object(target)
    sources.forEach(source => {
        if (source) {
            source = Object(source)
            _deep(target, source)
        }
    })
    return target
}