aboutsummaryrefslogtreecommitdiffstats
path: root/sdv/pdf/site/scripts/mergeDeep.js
diff options
context:
space:
mode:
authoropensource-tnbt <sridhar.rao@spirent.com>2020-09-10 12:23:34 +0530
committerSridhar Rao <sridhar.rao@spirent.com>2020-09-12 03:37:23 +0000
commit86d4114ab61f9cb00c1d01f91e81e9a38c6ba2f6 (patch)
tree5d5048528f9c5a15b372e9c889a276b5a3721de2 /sdv/pdf/site/scripts/mergeDeep.js
parent27fff834bee372c53e1b5d222c398543ac3f34aa (diff)
SDV: Platform Description Implementation.
This patch includes PDF Template and a tool to create PDF. Signed-off-by: Sridhar K. N. Rao <sridhar.rao@spirent.com> Change-Id: Ib702ff42352d584cb9146ff1959258c558e06615
Diffstat (limited to 'sdv/pdf/site/scripts/mergeDeep.js')
-rw-r--r--sdv/pdf/site/scripts/mergeDeep.js31
1 files changed, 31 insertions, 0 deletions
diff --git a/sdv/pdf/site/scripts/mergeDeep.js b/sdv/pdf/site/scripts/mergeDeep.js
new file mode 100644
index 0000000..ebede4e
--- /dev/null
+++ b/sdv/pdf/site/scripts/mergeDeep.js
@@ -0,0 +1,31 @@
+
+/**
+* Performs a deep merge of objects and returns new object. Does not modify
+* objects (immutable) and merges arrays via concatenation.
+*
+* @param {...object} objects - Objects to merge
+* @returns {object} New object with merged key/values
+*/
+function mergeDeep(...objects) {
+ const isObject = obj => obj && typeof obj === 'object';
+
+ return objects.reduce((prev, obj) => {
+ Object.keys(obj).forEach(key => {
+ const pVal = prev[key];
+ const oVal = obj[key];
+
+ if (Array.isArray(pVal) && Array.isArray(oVal)) {
+ prev[key] = pVal.concat(...oVal);
+ }
+ else if (isObject(pVal) && isObject(oVal)) {
+ prev[key] = mergeDeep(pVal, oVal);
+ }
+ else {
+ prev[key] = oVal;
+ }
+ });
+
+ return prev;
+ }, {});
+}
+