From fce13afbf61791abd8afed331664745713db299a Mon Sep 17 00:00:00 2001 From: Shaun Dern Date: Mon, 30 May 2016 14:11:21 -0700 Subject: [PATCH] Add omitBy (#60) --- README.md | 20 ++++++++++++++++++++ lib/index.js | 2 ++ lib/omitBy.js | 8 ++++++++ test/omitBy-spec.js | 15 +++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 lib/omitBy.js create mode 100644 test/omitBy-spec.js diff --git a/README.md b/README.md index 38acec2..06ca60d 100644 --- a/README.md +++ b/README.md @@ -395,6 +395,26 @@ var result = u({ user: u.omit(['authToken', 'SSN']) }, user); expect(result).to.eql({ user: { email: 'john@aol.com', username: 'john123' } }); ``` +### `u.omitBy(predicate(, object))` + +Remove properties. See [`_.omitBy`](https://lodash.com/docs#omitBy). + +```js +var user = { + user: { + email: 'john@aol.com', + username: 'john123', + authToken: '1211..', + SSN: 5551234 + } +}; + +function isSensitive(value, key) { return key == 'SSN' } +var result = u({ user: u.omitBy(isSensitive, user); + +expect(result).to.eql({ user: { email: 'john@aol.com', username: 'john123', authToken: '1211..' } }); +``` + ### `u.reject(predicate(, object))` Reject items from an array. See [`_.reject`](https://lodash.com/docs#reject). diff --git a/lib/index.js b/lib/index.js index 884ec28..5889166 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,6 +5,7 @@ import _if from './if'; import ifElse from './ifElse'; import map from './map'; import omit from './omit'; +import omitBy from './omitBy'; import reject from './reject'; import update from './update'; import updateIn from './updateIn'; @@ -21,6 +22,7 @@ u.is = is; u.freeze = freeze; u.map = map; u.omit = omit; +u.omitBy = omitBy; u.reject = reject; u.update = update; u.updateIn = updateIn; diff --git a/lib/omitBy.js b/lib/omitBy.js new file mode 100644 index 0000000..eccdec2 --- /dev/null +++ b/lib/omitBy.js @@ -0,0 +1,8 @@ +import _omitBy from 'lodash/omitBy'; +import wrap from './wrap'; + +function omitBy(predicate, collection) { + return _omitBy(collection, predicate); +} + +export default wrap(omitBy); diff --git a/test/omitBy-spec.js b/test/omitBy-spec.js new file mode 100644 index 0000000..abfb706 --- /dev/null +++ b/test/omitBy-spec.js @@ -0,0 +1,15 @@ +import { expect } from 'chai'; +import u from '../lib'; + +describe('u.omitBy', () => { + it('can omitBy with a function', () => { + const predicate = (value, key) => (value === 7 && key === 'bar'); + const result = u({ foo: u.omitBy(predicate) }, { foo: { bar: 7, baz: 'a' } }); + + expect(result).to.eql({ foo: { baz: 'a' } }); + }); + + it('freezes the result', () => { + expect(Object.isFrozen(u.omit('a', {}))).to.be.true; + }); +});