Skip to content

Commit

Permalink
Fix to only merge settings for plain objects
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Feb 24, 2017
1 parent 6e4e5bf commit 03650ff
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var trough = require('trough');
var string = require('x-is-string');
var func = require('x-is-function');
var array = require('isarray');
var plain = require('is-plain-obj');

/* Expose a frozen processor. */
module.exports = unified().freeze();
Expand Down Expand Up @@ -234,7 +235,7 @@ function unified() {
var entry = find(plugin);

if (entry) {
if (value !== false && entry[1] !== false && !array(value)) {
if (plain(entry[1]) && plain(value)) {
value = extend(entry[1], value);
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"bail": "^1.0.0",
"extend": "^3.0.0",
"has": "^1.0.1",
"is-plain-obj": "^1.1.0",
"isarray": "^2.0.1",
"trough": "^1.0.0",
"vfile": "^2.0.0",
Expand Down
49 changes: 39 additions & 10 deletions test/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,49 @@ test('use(plugin[, options])', function (t) {
st.end();
});

t.test('should reconfigure', function (st) {
var p = unified();
t.test('should reconfigure objects', function (st) {
var a = {foo: true, bar: true};
var b = {foo: false, qux: true};

st.plan(1);
st.plan(4);

p
.use([
[plugin, {foo: true, bar: true}],
[plugin, {foo: false, qux: true}]
])
.freeze();
unified().use(change, 'this').use(change, b).freeze();
unified().use(change).use(change, b).freeze();
unified().use(change, [1, 2, 3]).use(change, b).freeze();
unified().use(merge, a).use(merge, b).freeze();

function change(options) {
st.deepEqual(options, {foo: false, qux: true}, 'should reconfigure (set)');
}

function merge(options) {
st.deepEqual(options, {foo: false, bar: true, qux: true}, 'should reconfigure (merge)');
}
});

t.test('should reconfigure strings', function (st) {
st.plan(4);

unified().use(plugin, 'this').use(plugin, 'that').freeze();
unified().use(plugin).use(plugin, 'that').freeze();
unified().use(plugin, [1, 2, 3]).use(plugin, 'that').freeze();
unified().use(plugin, {foo: 'bar'}).use(plugin, 'that').freeze();

function plugin(options) {
st.equal(options, 'that', 'should reconfigure');
}
});

t.test('should reconfigure arrays', function (st) {
st.plan(4);

unified().use(plugin, [1, 2, 3]).use(plugin, [4, 5, 6]).freeze();
unified().use(plugin).use(plugin, [4, 5, 6]).freeze();
unified().use(plugin, {foo: 'true'}).use(plugin, [4, 5, 6]).freeze();
unified().use(plugin, 'foo').use(plugin, [4, 5, 6]).freeze();

function plugin(options) {
st.deepEqual(options, {foo: false, bar: true, qux: true}, 'should reconfigure');
st.deepEqual(options, [4, 5, 6], 'should reconfigure');
}
});

Expand Down

0 comments on commit 03650ff

Please sign in to comment.