Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #22 from eliperelman/when
Browse files Browse the repository at this point in the history
Allow conditional configuration via when
  • Loading branch information
eliperelman authored Mar 29, 2017
2 parents bcc2362 + 2f5fb94 commit 56995dd
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ entries()
merge(obj)
```

```js
// Conditionally execute a function to continue configuration
// condition: Boolean
// truthyHandler: Function -> ChainedMap
// invoked when condition is truthy, given a single argument of the ChainedMap instance
// falsyHandler: Function -> ChainedMap
// invoked when condition is falsy, given a single argument of the ChainedMap instance
when(condition, truthyHandler, falsyHandler)
```

## ChainedSet

Another of the core API interfaces in webpack-chain is a `ChainedSet`. A `ChainedSet` operates
Expand Down Expand Up @@ -255,6 +265,16 @@ values()
merge(arr)
```

```js
// Conditionally execute a function to continue configuration
// condition: Boolean
// truthyHandler: Function -> ChainedSet
// invoked when condition is truthy, given a single argument of the ChainedSet instance
// falsyHandler: Function -> ChainedSet
// invoked when condition is falsy, given a single argument of the ChainedSet instance
when(condition, truthyHandler, falsyHandler)
```

## Shorthand methods

A number of shorthand methods exist for setting a value on a `ChainedMap`
Expand Down Expand Up @@ -845,3 +865,30 @@ config.merge({
}
})
```

### Conditional configuration

When working with instances of `ChainedMap` and `ChainedSet`, you can perform conditional configuration using `when`.
You must specify an expression to `when()` which will be evaluated for truthiness or falsiness. If the expression is
truthy, the first function argument will be invoked with an instance of the current chained instance. You can optionally
provide a second function to be invoked when the condition is falsy, which is also given the current chained instance.

```js
// Example: Only add minify plugin during production
config
.when(process.env.NODE_ENV === 'production', config => {
config
.plugin('minify')
.use(BabiliWebpackPlugin);
});
```

```js
// Example: Only add minify plugin during production,
// otherwise set devtool to source-map
config
.when(process.env.NODE_ENV === 'production',
config => config.plugin('minify').use(BabiliWebpackPlugin),
config => config.devtool('source-map')
);
```
10 changes: 10 additions & 0 deletions src/ChainedMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,14 @@ module.exports = class extends Chainable {
return acc;
}, {});
}

when(condition, trueBrancher = Function.prototype, falseBrancher = Function.prototype) {
if (condition) {
trueBrancher(this);
} else {
falseBrancher(this);
}

return this;
}
};
10 changes: 10 additions & 0 deletions src/ChainedSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ module.exports = class extends Chainable {
this.store = new Set([...this.store, ...arr]);
return this;
}

when(condition, trueBrancher = Function.prototype, falseBrancher = Function.prototype) {
if (condition) {
trueBrancher(this);
} else {
falseBrancher(this);
}

return this;
}
};
30 changes: 30 additions & 0 deletions test/ChainedMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,33 @@ test('merge with overriding values', t => {
t.is(map.merge(obj), map);
t.deepEqual(map.entries(), { a: 'alpha', b: 'beta', c: 'gamma' });
});

test('when true', t => {
const map = new ChainedMap();
const right = instance => {
t.is(instance, map);
instance.set('alpha', 'a');
};
const left = instance => {
instance.set('beta', 'b');
};

t.is(map.when(true, right, left), map);
t.true(map.has('alpha'));
t.false(map.has('beta'));
});

test('when false', t => {
const map = new ChainedMap();
const right = instance => {
instance.set('alpha', 'a');
};
const left = instance => {
t.is(instance, map);
instance.set('beta', 'b');
};

t.is(map.when(false, right, left), map);
t.false(map.has('alpha'));
t.true(map.has('beta'));
});
30 changes: 30 additions & 0 deletions test/ChainedSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,33 @@ test('merge with existing values', t => {
t.is(set.merge(arr), set);
t.deepEqual(set.values(), ['delta', 'alpha', 'beta', 'gamma']);
});

test('when true', t => {
const set = new ChainedSet();
const right = instance => {
t.is(instance, set);
instance.add('alpha');
};
const left = instance => {
instance.add('beta');
};

t.is(set.when(true, right, left), set);
t.true(set.has('alpha'));
t.false(set.has('beta'));
});

test('when false', t => {
const set = new ChainedSet();
const right = instance => {
instance.add('alpha');
};
const left = instance => {
t.is(instance, set);
instance.add('beta');
};

t.is(set.when(false, right, left), set);
t.false(set.has('alpha'));
t.true(set.has('beta'));
});

0 comments on commit 56995dd

Please sign in to comment.