Skip to content

Commit

Permalink
Added merge multiple that outputs an array
Browse files Browse the repository at this point in the history
  • Loading branch information
herecydev committed Mar 16, 2017
1 parent c3cd11d commit 237117c
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/index.js
@@ -1,5 +1,5 @@
import {
differenceWith, mergeWith, unionWith
differenceWith, mergeWith, unionWith, values
} from 'lodash';
import joinArrays from './join-arrays';
import { uniteRules } from './join-arrays-smart';
Expand Down Expand Up @@ -41,6 +41,8 @@ const mergeSmart = merge({
}
});

const mergeMultiple = (...sources) => values(merge(sources));

// rules: { <field>: <'append'|'prepend'|'replace'> }
// All default to append but you can override here
const mergeStrategy = (rules = {}) => merge({
Expand Down Expand Up @@ -103,6 +105,7 @@ function isRule(key) {
}

module.exports = merge;
module.exports.multiple = mergeMultiple;
module.exports.smart = mergeSmart;
module.exports.strategy = mergeStrategy;
module.exports.smartStrategy = mergeSmartStrategy;
Expand Down
114 changes: 114 additions & 0 deletions tests/merge-multiple-tests.js
@@ -0,0 +1,114 @@
/* eslint-env mocha */
const assert = require('assert');

function multipleTests(merge) {
it('should override objects', function () {
const a = {
client: {
entry: './client.js'
}
};
const b = {
client: {
entry: './replaced.js'
}
};
const result = [
{
entry: './replaced.js'
}
];

assert.deepEqual(merge(a, b), result);
});

it('should add new objects if not existing', function () {
const a = {
client: {
entry: './client.js'
},
server: {
entry: './server.js',
}
};
const b = {
client: {
entry: './replaced.js'
}
};
const result = [
{
entry: './replaced.js'
},
{
entry: './server.js'
}
];

assert.deepEqual(merge(a, b), result);
});

it('should work with an array of objects', function () {
const a = {
client: {
entry: ['./client.js', './client2.js']
},
server: {
entry: ['./server.js', './server2.js']
}
};
const b = {
client: {
entry: ['./replaced.js', './replaced2.js']
}
};
const result = [
{
entry: ['./client.js', './client2.js', './replaced.js', './replaced2.js']
},
{
entry: ['./server.js', './server2.js']
}
];

assert.deepEqual(merge(a, b), result);
});

it('should deeply merge objects', function () {
const a = {
client: {
entry: {
main: './client.js'
}
},
server: {
entry: {
main: './server.js'
}
}
};
const b = {
client: {
entry: {
main: './replaced.js'
}
}
};
const result = [
{
entry: {
main: './replaced.js'
}
},
{
entry: {
main: './server.js'
}
}
];

assert.deepEqual(merge(a, b), result);
});
}

module.exports = multipleTests;
10 changes: 10 additions & 0 deletions tests/test-merge-multiple.js
@@ -0,0 +1,10 @@
/* eslint-env mocha */
const webpackMerge = require('..');
const mergeMultipleTests = require('./merge-multiple-tests');

describe('Multiple merge', function () {
const merge = webpackMerge.multiple;
console.log(merge);

mergeMultipleTests(merge);
});

0 comments on commit 237117c

Please sign in to comment.