From 6bf48c81277293d26d8e7124f67dfbf91d41187d Mon Sep 17 00:00:00 2001 From: Jonathan Burke Date: Wed, 25 Jan 2023 10:58:25 +0100 Subject: [PATCH] fix(merge): allow array fields to be merged Signed-off-by: Jonathan Burke --- lib/utils.js | 5 +++++ test/utils.test.js | 3 +++ 2 files changed, 8 insertions(+) diff --git a/lib/utils.js b/lib/utils.js index 2aa7546203..d4ec4e4b36 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -315,6 +315,11 @@ exports.merge = function merge(to, from, options, path) { } } merge(to[key], from[key], options, path ? path + '.' + key : key); + } else if(Array.isArray(from[key])) { + if (!Array.isArray(to[key])) { + to[key] = []; + } + to[key].push(...from[key]); } else if (options.overwrite) { to[key] = from[key]; } diff --git a/test/utils.test.js b/test/utils.test.js index 74e4a41086..c7be3e668d 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -292,6 +292,7 @@ describe('utils', function() { it('merges two objects together without overriding properties & methods', function() { function To() { this.name = 'to'; + this.array = ['to']; this.toProperty = true; } To.prototype.getName = function() {}; @@ -299,6 +300,7 @@ describe('utils', function() { function From() { this.name = 'from'; + this.array = ['from']; this.fromProperty = true; } From.prototype.getName = function() {}; @@ -310,6 +312,7 @@ describe('utils', function() { utils.merge(to, from); assert.equal(to.name, 'to'); + assert.deepEqual(to.array,['to', 'from']); assert.equal(to.toProperty, true); assert.equal(to.fromProperty, true); assert.ok(to.getName === To.prototype.getName);