diff --git a/index.js b/index.js index 5d82f2e..c1d7759 100644 --- a/index.js +++ b/index.js @@ -20,7 +20,10 @@ function readAsJSON(fileName) { function yamlMerge(...from) { const files = from.map((path) => readAsJSON(path)); - const outputJSON = _.merge({}, ...files); + const outputJSON = _.mergeWith({}, ...files, (objValue, srcValue, key, object, source, stack) => { + if (Array.isArray(objValue) && Array.isArray(srcValue)) return [...objValue, ...srcValue]; + return undefined; // handle it just like with _.merge + }) return jsYaml.dump(outputJSON); } diff --git a/test/fixtures/arrays/a.yml b/test/fixtures/arrays/a.yml new file mode 100644 index 0000000..21c9a44 --- /dev/null +++ b/test/fixtures/arrays/a.yml @@ -0,0 +1,2 @@ +key: +- a diff --git a/test/fixtures/arrays/b.yml b/test/fixtures/arrays/b.yml new file mode 100644 index 0000000..e5eb9af --- /dev/null +++ b/test/fixtures/arrays/b.yml @@ -0,0 +1,2 @@ +key: +- b diff --git a/test/lib-test.js b/test/lib-test.js index 48214ba..8283f5b 100644 --- a/test/lib-test.js +++ b/test/lib-test.js @@ -47,4 +47,16 @@ describe('merge logic', function () { ` + '\n' ); }); + + it('concatenates arrays instead of overwriting them', function () { + const output = merge(...fixtureFiles('arrays/a.yml', 'arrays/b.yml')); + + expect(output).to.equal( + stripIndent` + key: + - a + - b + ` + '\n' + ); + }); });