From: Haoqun Jiang Date: Mon, 26 Jul 2021 15:35:08 +0000 (+0800) Subject: refactor: extract deepMerge function X-Git-Tag: v3.0.0-alpha.2~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ecdd912b1001a33e6c340c6056b34d7321dbc1c1;p=thirdparty%2Fvuejs%2Fcreate-vue.git refactor: extract deepMerge function --- diff --git a/deepMerge.js b/deepMerge.js new file mode 100644 index 00000000..1fb16e83 --- /dev/null +++ b/deepMerge.js @@ -0,0 +1,26 @@ +const isObject = val => val && typeof val === 'object' +const mergeArrayWithDedupe = (a, b) => Array.from(new Set([...a, ...b])) + +/** + * Recursively merge the content of the new object to the existing one + * @param {Object} target the existing object + * @param {Object} obj the new object + */ +function deepMerge(target, obj) { + for (const key of Object.keys(obj)) { + const oldVal = target[key] + const newVal = obj[key] + + if (Array.isArray(oldVal) && Array.isArray(newVal)) { + target[key] = mergeArrayWithDedupe(oldVal, newVal) + } else if (isObject(oldVal) && isObject(newVal)) { + target[key] = deepMerge(oldVal, newVal) + } else { + target[key] = newVal + } + } + + return target +} + +export default deepMerge diff --git a/renderTemplate.js b/renderTemplate.js index 17a0c737..c35df967 100644 --- a/renderTemplate.js +++ b/renderTemplate.js @@ -1,30 +1,7 @@ import fs from 'fs' import path from 'path' -const isObject = val => val && typeof val === 'object' -const mergeArrayWithDedupe = (a, b) => Array.from(new Set([...a, ...b])) - -/** - * Recursively merge the content of the new object to the existing one - * @param {Object} target the existing object - * @param {Object} obj the new object - */ -function deepMerge(target, obj) { - for (const key of Object.keys(obj)) { - const oldVal = target[key] - const newVal = obj[key] - - if (Array.isArray(oldVal) && Array.isArray(newVal)) { - target[key] = mergeArrayWithDedupe(oldVal, newVal) - } else if (isObject(oldVal) && isObject(newVal)) { - target[key] = deepMerge(oldVal, newVal) - } else { - target[key] = newVal - } - } - - return target -} +import deepMerge from './deepMerge.js' /** * Renders a template folder/file to the file system,