Skip to content

Commit

Permalink
Merge pull request #13 from moqmar/patch-1
Browse files Browse the repository at this point in the history
Fix #12 by adding a _.mergeWith customizer function
  • Loading branch information
alexlafroscia committed Oct 16, 2020
2 parents 481eea0 + feb6737 commit 19dae8b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/arrays/a.yml
@@ -0,0 +1,2 @@
key:
- a
2 changes: 2 additions & 0 deletions test/fixtures/arrays/b.yml
@@ -0,0 +1,2 @@
key:
- b
12 changes: 12 additions & 0 deletions test/lib-test.js
Expand Up @@ -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'
);
});
});

0 comments on commit 19dae8b

Please sign in to comment.