diff --git a/Web/src/main.js b/Web/src/main.js index 3e6598c..ab56557 100644 --- a/Web/src/main.js +++ b/Web/src/main.js @@ -39,6 +39,8 @@ Vue.prototype.$api = api * Lodash全局化 */ import _ from 'lodash' +import * as _extend from './util/lodash-extend' +Object.assign(_, _extend) Vue.prototype.$_ = _ /** * moment全局化 diff --git a/Web/src/util/lodash-extend/index.js b/Web/src/util/lodash-extend/index.js new file mode 100644 index 0000000..c707c47 --- /dev/null +++ b/Web/src/util/lodash-extend/index.js @@ -0,0 +1,59 @@ +/** + * 将源对象的属性值解析到目标对象已存在的属性 + * @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 +} \ No newline at end of file