]> git.ipfire.org Git - thirdparty/vuejs/create-vue.git/commitdiff
refactor: extract deepMerge function
authorHaoqun Jiang <haoqunjiang@gmail.com>
Mon, 26 Jul 2021 15:35:08 +0000 (23:35 +0800)
committerHaoqun Jiang <haoqunjiang@gmail.com>
Mon, 26 Jul 2021 15:35:08 +0000 (23:35 +0800)
deepMerge.js [new file with mode: 0644]
renderTemplate.js

diff --git a/deepMerge.js b/deepMerge.js
new file mode 100644 (file)
index 0000000..1fb16e8
--- /dev/null
@@ -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
index 17a0c7374e5ac1a8348436b0886e39d23d6f8bd4..c35df96778f07dba12fe964113096cb324cd43d5 100644 (file)
@@ -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,