Last active
March 29, 2019 02:39
-
-
Save john-yuan/f9f62dc2a2535e464ab289db741247b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 复制数据结构定义(definition)中指定的内容 | |
* | |
* @param {Object.<string, *>} definition 数据结构定义 | |
* @param {Object.<string, *>} dataSource 数据源 | |
* @param {Object.<string, *>} [options] (可选)附加选项 | |
* @param {any} [options.defaultObject=null] (可选)当数据源中没有定义中的对象时使用的默认值 | |
* @param {any} [options.defaultArray=[]] (可选)当数据源中没有定义中的数组时使用的默认值 | |
* @returns {Object.<string, *>} 返回过滤后的数据 | |
*/ | |
var filterCopy = function (definition, dataSource, options) { | |
var toString = Object.prototype.toString; | |
var hasOwn = Object.prototype.hasOwnProperty; | |
var isObject = function (it) { | |
return it && toString.call(it) === '[object Object]'; | |
}; | |
var isArray = function (it) { | |
return it && toString.call(it) === '[object Array]'; | |
}; | |
if (!isObject(options)) { | |
options = {}; | |
} | |
if (!hasOwn.call(options, 'defaultObject')) { | |
options.defaultObject = null; | |
} | |
if (!hasOwn.call(options, 'defaultArray')) { | |
options.defaultArray = []; | |
} | |
var copyRecursively = function (definition, dataSource) { | |
var result = {}; | |
var key; | |
var def; | |
var src; | |
var i; | |
var l; | |
var arr; | |
var val; | |
if (isObject(definition) && isObject(dataSource)) { | |
for (key in definition) { | |
if (hasOwn.call(definition, key)) { | |
def = definition[key]; | |
if (hasOwn.call(dataSource, key)) { | |
src = dataSource[key]; | |
if (isObject(def)) { | |
if (isObject(src)) { | |
result[key] = copyRecursively(def, src); | |
} else { | |
result[key] = options.defaultObject; | |
} | |
} else if (isArray(def)) { | |
if (isObject(def[0])) { | |
def = def[0]; | |
if (isArray(src)) { | |
arr = []; | |
for (i = 0, l = src.length; i < l; i += 1) { | |
val = src[i]; | |
if (isObject(val)) { | |
arr.push(copyRecursively(def, val)); | |
} | |
} | |
result[key] = arr; | |
} else { | |
result[key] = options.defaultArray; | |
} | |
} else { | |
if (isArray(src)) { | |
result[key] = src; | |
} else { | |
result[key] = options.defaultArray; | |
} | |
} | |
} else { | |
result[key] = src; | |
} | |
} else { | |
if (isObject(def)) { | |
result[key] = options.defaultObject; | |
} else if (isArray(def)) { | |
result[key] = options.defaultArray; | |
} else { | |
result[key] = def; | |
} | |
} | |
} | |
} | |
} | |
return result; | |
}; | |
return copyRecursively(definition, dataSource); | |
}; | |
// TEST --------------------------------------------------------------------------- | |
var definition = { | |
projectId: null, | |
projectName: null, | |
// 支持嵌套 | |
customerInfo: { | |
customerId: null, | |
}, | |
// 过滤数组里面的内容 | |
routes: [ | |
{ | |
routeName: null, | |
// 支持嵌套 | |
startAddress: { | |
addressName: null, | |
location: null | |
}, | |
endAddress: { | |
addressName: null, | |
location: null | |
} | |
} | |
], | |
roadMapIds: [], // 普通数组,等同于 roadMapIds: null | |
}; | |
var dataSource = { | |
projectName: '项目名称', | |
createTime: '2019-03-29 15:00:00', | |
lastUpdateTime: '2019-03-29 15:00:00', | |
customerInfo: { | |
customerId: 5729, | |
customerName: '刘萌萌' | |
}, | |
routes: [ | |
{ | |
routeName: '测试线路1', | |
createTime: '2019-03-29 15:00:00', | |
lastUpdateTime: '2019-03-29 15:00:00', | |
startAddress: { | |
addressName: '天府宝座', | |
location: '120.3434,24.2342', | |
createTime: '2019-03-29 15:00:00', | |
lastUpdateTime: '2019-03-29 15:00:00' | |
}, | |
endAddress: { | |
addressName: '天府广场', | |
location: '122.3434,22.2342', | |
createTime: '2019-03-29 15:00:00', | |
lastUpdateTime: '2019-03-29 15:00:00' | |
} | |
}, | |
{ | |
routeName: '测试线路2', | |
createTime: '2019-03-29 15:00:00', | |
lastUpdateTime: '2019-03-29 15:00:00', | |
startAddress: { | |
addressName: '天府宝座2', | |
location: '2120.3434,24.2342', | |
createTime: '2019-03-29 15:00:00', | |
lastUpdateTime: '2019-03-29 15:00:00' | |
}, | |
endAddress: { | |
addressName: '天府广场2', | |
location: '2122.3434,22.2342', | |
createTime: '2019-03-29 15:00:00', | |
lastUpdateTime: '2019-03-29 15:00:00' | |
} | |
} | |
], | |
roadMapIds: [1, 2, 3, 4] | |
}; | |
var result = filterCopy(definition, dataSource); | |
console.log(JSON.stringify(result, null, 4)); | |
// 期望结果如下: | |
// { | |
// "projectId": null, | |
// "projectName": "项目名称", | |
// "customerInfo": { | |
// "customerId": 5729 | |
// }, | |
// "routes": [ | |
// { | |
// "routeName": "测试线路1", | |
// "startAddress": { | |
// "addressName": "天府宝座", | |
// "location": "120.3434,24.2342" | |
// }, | |
// "endAddress": { | |
// "addressName": "天府广场", | |
// "location": "122.3434,22.2342" | |
// } | |
// }, | |
// { | |
// "routeName": "测试线路2", | |
// "startAddress": { | |
// "addressName": "天府宝座2", | |
// "location": "2120.3434,24.2342" | |
// }, | |
// "endAddress": { | |
// "addressName": "天府广场2", | |
// "location": "2122.3434,22.2342" | |
// } | |
// } | |
// ], | |
// "roadMapIds": [ | |
// 1, | |
// 2, | |
// 3, | |
// 4 | |
// ] | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment