Skip to content

Commit

Permalink
feat(props): Add function to drop all keys whose values are undefined…
Browse files Browse the repository at this point in the history
… in a… (#12)

* feat(): Add function to drop all keys whose values are undefined in an object

Add function to drop all keys whose values are undefined in an object

* refactor(): Rename function

- Rename function `removeEmptyKeys` to `removeEmptyProperties` as
  requested by author.

- Make other changes to accommodate this change.

* refactor(): Add `removeEmptyProperties` to package's exports

* test: Update top-level export test

Update top-level exports test to recognize new function `removeEmptyProperties`.

* docs: Update documentation to include new function `removeEmptyProperties`
  • Loading branch information
huy-nguyen authored and Kent C. Dodds committed Dec 8, 2016
1 parent 7b2a8b9 commit e3f6841
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4 deletions.
12 changes: 12 additions & 0 deletions .all-contributorsrc
Expand Up @@ -46,6 +46,18 @@
"contributions": [
"doc"
]
},
{
"login": "huy-nguyen",
"name": "Huy Nguyen",
"avatar_url": "https://avatars.githubusercontent.com/u/7352279?v=3",
"profile": "http://www.huy-nguyen.com/",
"contributions": [
"code",
"doc",
"example",
"test"
]
}
]
}
23 changes: 19 additions & 4 deletions README.md
Expand Up @@ -9,7 +9,7 @@ Utilities to help your webpack config be easier to read
[![downloads][downloads-badge]][npm-stat]
[![MIT License][license-badge]][LICENSE]

[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-5-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -48,12 +48,27 @@ It is expected that you use this in your `webpack.config.js` file.
```javascript
const webpack = require('webpack')
const {getIfUtils, removeEmpty} = require('webpack-config-utils')
const {getIfUtils, removeEmpty, removeEmptyProperties} = require('webpack-config-utils')

const {ifProduction} = getIfUtils(process.env.NODE_ENV)

module.exports = {
// ... your config
entry: removeEmptyProperties({
app: ifProd('./indexWithoutCSS', './indexWithCSS'),
css: ifProd('./style.scss')
}),
module: {
loaders: [
removeEmptyProperties({
test: /\.scss$/,
loader: ifProd(ExtractTextPlugin.extract({
loader: ['css-loader', 'sass-loader']
})),
loaders: ifNotProd(['style-loader', 'css-loader', 'sass-loader'])
})
]
},
plugins: removeEmpty([
ifProduction(new webpack.optimize.DedupePlugin()),
ifProduction(new webpack.LoaderOptionsPlugin({
Expand Down Expand Up @@ -126,8 +141,8 @@ a few another helpful utility: [`webpack-combine-loaders`](https://www.npmjs.com
Thanks goes to these people ([emoji key][emojis]):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/webpack-config-utils/commits?author=kentcdodds) [📖](https://github.com/kentcdodds/webpack-config-utils/commits?author=kentcdodds) 💡 🚇 [⚠️](https://github.com/kentcdodds/webpack-config-utils/commits?author=kentcdodds) | [<img src="https://avatars.githubusercontent.com/u/284515?v=3" width="100px;"/><br /><sub>Breno Calazans</sub>](https://twitter.com/breno_calazans)<br />💡 | [<img src="https://avatars.githubusercontent.com/u/363583?v=3" width="100px;"/><br /><sub>Tamara Temple</sub>](http://tamouse.org)<br />[📖](https://github.com/kentcdodds/webpack-config-utils/commits?author=tamouse) | [<img src="https://avatars.githubusercontent.com/u/7907232?v=3" width="100px;"/><br /><sub>Ben Halverson</sub>](benhalverson.me)<br /> |
| :---: | :---: | :---: | :---: |
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/webpack-config-utils/commits?author=kentcdodds) [📖](https://github.com/kentcdodds/webpack-config-utils/commits?author=kentcdodds) 💡 🚇 [⚠️](https://github.com/kentcdodds/webpack-config-utils/commits?author=kentcdodds) | [<img src="https://avatars.githubusercontent.com/u/284515?v=3" width="100px;"/><br /><sub>Breno Calazans</sub>](https://twitter.com/breno_calazans)<br />💡 | [<img src="https://avatars.githubusercontent.com/u/363583?v=3" width="100px;"/><br /><sub>Tamara Temple</sub>](http://tamouse.org)<br />[📖](https://github.com/kentcdodds/webpack-config-utils/commits?author=tamouse) | [<img src="https://avatars.githubusercontent.com/u/7907232?v=3" width="100px;"/><br /><sub>Ben Halverson</sub>](benhalverson.me)<br />[📖](https://github.com/kentcdodds/webpack-config-utils/commits?author=benhalverson) | [<img src="https://avatars.githubusercontent.com/u/7352279?v=3" width="100px;"/><br /><sub>Huy Nguyen</sub>](http://www.huy-nguyen.com/)<br />[💻](https://github.com/kentcdodds/webpack-config-utils/commits?author=huy-nguyen) [📖](https://github.com/kentcdodds/webpack-config-utils/commits?author=huy-nguyen) 💡 [⚠️](https://github.com/kentcdodds/webpack-config-utils/commits?author=huy-nguyen) |
| :---: | :---: | :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification. Contributions of any kind welcome!
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -6,3 +6,4 @@ export {default as combineLoaders} from 'webpack-combine-loaders'
export * from './prop-if'
export * from './remove-empty'
export * from './get-if-utils'
export * from './remove-empty-properties'
1 change: 1 addition & 0 deletions src/index.test.js
Expand Up @@ -8,6 +8,7 @@ test('exports all the things we care about', t => {
'combineLoaders',
'propIf', 'propIfNot',
'removeEmpty', 'getIfUtils',
'removeEmptyProperties',
]
const diff = difference(allExports, expectedExports)
t.deepEqual(diff, [])
Expand Down
28 changes: 28 additions & 0 deletions src/remove-empty-properties.js
@@ -0,0 +1,28 @@
export {
removeEmptyProperties,
}
/**
* Accepts an object and removes all keys whose values are undefined.
* @example
* // Use case: move CSS into separate file for production (with `extract-text-webpack-plugin`)
* // but not in development:
* {
* // other webpack configs
* entry: removeEmptyProperties({
* app: ifProd('./indexWithoutCSS', './indexWithCSS'),
* css: ifNotProd('./style.css')
* })
* {
* @param {object} object The object to remove keys from
* @returns {object} The resulting object
*/
function removeEmptyProperties(object) {
const output = {}
Object.keys(object).forEach(key => {
const value = object[key]
if (typeof value !== 'undefined') {
output[key] = value
}
})
return output
}
9 changes: 9 additions & 0 deletions src/remove-empty-properties.test.js
@@ -0,0 +1,9 @@
import test from 'ava'
import {removeEmptyProperties} from './remove-empty-properties'

test('removeEmptyProperties should remove keys whose values are `undefined` out of an object', t => {
t.deepEqual(
removeEmptyProperties({a: 1, b: 'b', c: undefined, d: null}),
{a: 1, b: 'b', d: null}
)
})

0 comments on commit e3f6841

Please sign in to comment.