Skip to content

Commit

Permalink
Implement basic spec for merge.strategy
Browse files Browse the repository at this point in the history
Closes #17.
  • Loading branch information
bebraw committed Nov 16, 2016
1 parent 058666a commit e0c2386
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
15 changes: 13 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,19 @@ function mergeStrategy() {
return function () {
var _ref3;

return lodashMerge.apply(null, (_ref3 = [{}]).concat.apply(_ref3, arguments).concat([joinArrays() // TODO: connect rules with customizers
]));
return lodashMerge.apply(null, (_ref3 = [{}]).concat.apply(_ref3, arguments).concat([joinArrays({
customizeArray: function customizeArray(a, b, key) {
if (rules[key]) {
return rules[key] === 'prepend' && b.concat(a);
}
},
customizeObject: function customizeObject(a, b, key) {
if (rules[key]) {
// No support for recursive checks yet - are those even needed?
return rules[key] === 'prepend' && lodashMerge({}, b, a, joinArrays());
}
}
})]));
};
}

Expand Down
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,19 @@ function mergeStrategy(rules = {}) {
// All default to append but you can override here
return function () {
return lodashMerge.apply(null, [{}].concat(...arguments).concat([
joinArrays() // TODO: connect rules with customizers
joinArrays({
customizeArray: (a, b, key) => {
if (rules[key]) {
return rules[key] === 'prepend' && b.concat(a);
}
},
customizeObject: (a, b, key) => {
if (rules[key]) {
// No support for recursive checks yet - are those even needed?
return rules[key] === 'prepend' && lodashMerge({}, b, a, joinArrays());
}
}
})
]));
};
}
Expand Down
88 changes: 88 additions & 0 deletions tests/test-merge-strategy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env mocha */
const assert = require('assert');
const webpackMerge = require('..');
const normalMergeTests = require('./test-merge');
const mergeTests = require('./merge-tests');
Expand All @@ -11,4 +12,91 @@ describe('Merge strategy', function () {
normalMergeTests(merge(), 'postLoaders');
normalMergeTests(merge(), 'rules');
mergeTests(merge());
mergeStrategyTests(merge);
});

function mergeStrategyTests(merge) {
it('should allow setting to array append', function () {
const a = {
entry: ['foo', 'bar', 'baz']
};
const b = {
entry: ['zoo']
};
const result = {
entry: ['foo', 'bar', 'baz', 'zoo']
};

assert.deepEqual(merge({
entry: 'append'
})(a, b), result);
});

it('should allow setting to array prepend', function () {
const a = {
entry: ['foo', 'bar', 'baz']
};
const b = {
entry: ['zoo']
};
const result = {
entry: ['zoo', 'foo', 'bar', 'baz']
};

assert.deepEqual(merge({
entry: 'prepend'
})(a, b), result);
});

it('should allow setting to object append', function () {
const a = {
entry: {
foo: 'bar'
}
};
const b = {
entry: {
bar: 'baz'
}
};
const result = {
entry: {
foo: 'bar',
bar: 'baz'
}
};

assert.deepEqual(
Object.keys(merge({
entry: 'append'
})(a, b).entry),
Object.keys(result.entry)
);
});

it('should allow setting to object prepend', function () {
const a = {
entry: {
foo: 'bar'
}
};
const b = {
entry: {
bar: 'baz'
}
};
const result = {
entry: {
bar: 'baz',
foo: 'bar'
}
};

assert.deepEqual(
Object.keys(merge({
entry: 'prepend'
})(a, b).entry),
Object.keys(result.entry)
);
});
}

0 comments on commit e0c2386

Please sign in to comment.