copyObject.js 965 Bytes
Newer Older
1 2
import assignValue from './assignValue.js';
import baseAssignValue from './baseAssignValue.js';
徐立's avatar
徐立 committed
3 4 5 6 7 8 9 10 11 12 13 14

/**
 * Copies properties of `source` to `object`.
 * 将“源”的属性复制到“对象”。
 * @private
 * @param {Object} source 要从中复制属性的对象。
 * @param {Array} props 要复制的属性标识符。
 * @param {Object} [object={}] 要将属性复制到的对象。
 * @param {Function} [customizer] 自定义复制值的函数。
 * @returns {Object} 返回的对象。
 */
function copyObject(source, props, object, customizer) {
15 16
	const isNew = !object;
	object || (object = {});
徐立's avatar
徐立 committed
17

18 19 20 21
	for (const key of props) {
		let newValue = customizer
			? customizer(object[key], source[key], key, object, source)
			: undefined;
徐立's avatar
徐立 committed
22

23 24 25 26 27 28 29 30 31 32
		if (newValue === undefined) {
			newValue = source[key];
		}
		if (isNew) {
			baseAssignValue(object, key, newValue);
		} else {
			assignValue(object, key, newValue);
		}
	}
	return object;
徐立's avatar
徐立 committed
33 34
}

35
export default copyObject;