Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(props): Add function to drop all keys whose values are undefined…
… 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
1 parent
7b2a8b9
commit e3f6841
Showing
6 changed files
with
70 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
) | ||
}) |