add _.omitted to omit properties (#78)
This commit is contained in:
parent
eb4868775a
commit
d8da597465
13
README.md
13
README.md
@ -394,6 +394,18 @@ var user = {
|
|||||||
var result = u({ user: u.omit(['authToken', 'SSN']) }, user);
|
var result = u({ user: u.omit(['authToken', 'SSN']) }, user);
|
||||||
|
|
||||||
expect(result).to.eql({ user: { email: 'john@aol.com', username: 'john123' } });
|
expect(result).to.eql({ user: { email: 'john@aol.com', username: 'john123' } });
|
||||||
|
|
||||||
|
### `u.omitted`
|
||||||
|
|
||||||
|
A property updated to this constant will be removed from the final object.
|
||||||
|
Useful when one wishes to remove and update properties in a single operation.
|
||||||
|
|
||||||
|
```js
|
||||||
|
var user = { email: 'john@aol.com', username: 'john123', authToken: '1211..' };
|
||||||
|
|
||||||
|
var result = u({ authToken: u.omitted, active: true }, user);
|
||||||
|
|
||||||
|
expect(result).to.eql({ user: { email: 'john@aol.com', username: 'john123', active: true } });
|
||||||
```
|
```
|
||||||
|
|
||||||
### `u.omitBy(predicate(, object))`
|
### `u.omitBy(predicate(, object))`
|
||||||
@ -414,6 +426,7 @@ function isSensitive(value, key) { return key == 'SSN' }
|
|||||||
var result = u({ user: u.omitBy(isSensitive) }, user);
|
var result = u({ user: u.omitBy(isSensitive) }, user);
|
||||||
|
|
||||||
expect(result).to.eql({ user: { email: 'john@aol.com', username: 'john123', authToken: '1211..' } });
|
expect(result).to.eql({ user: { email: 'john@aol.com', username: 'john123', authToken: '1211..' } });
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### `u.reject(predicate(, object))`
|
### `u.reject(predicate(, object))`
|
||||||
|
@ -7,7 +7,7 @@ import map from './map'
|
|||||||
import omit from './omit'
|
import omit from './omit'
|
||||||
import omitBy from './omitBy'
|
import omitBy from './omitBy'
|
||||||
import reject from './reject'
|
import reject from './reject'
|
||||||
import update from './update'
|
import update, { omitted } from './update'
|
||||||
import updateIn from './updateIn'
|
import updateIn from './updateIn'
|
||||||
import withDefault from './withDefault'
|
import withDefault from './withDefault'
|
||||||
import { _ } from './util/curry'
|
import { _ } from './util/curry'
|
||||||
@ -26,6 +26,7 @@ u.omitBy = omitBy
|
|||||||
u.reject = reject
|
u.reject = reject
|
||||||
u.update = update
|
u.update = update
|
||||||
u.updateIn = updateIn
|
u.updateIn = updateIn
|
||||||
|
u.omitted = omitted
|
||||||
u.withDefault = withDefault
|
u.withDefault = withDefault
|
||||||
|
|
||||||
module.exports = u
|
module.exports = u
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
import isPlainObject from 'lodash/isPlainObject'
|
import isPlainObject from 'lodash/isPlainObject'
|
||||||
|
import _omitBy from 'lodash/omitBy'
|
||||||
|
|
||||||
import wrap from './wrap'
|
import wrap from './wrap'
|
||||||
|
import constant from './constant'
|
||||||
|
|
||||||
|
const innerOmitted = { __omitted: true }
|
||||||
|
export const omitted = constant(innerOmitted)
|
||||||
|
|
||||||
function isEmpty(object) {
|
function isEmpty(object) {
|
||||||
return !Object.keys(object).length
|
return !Object.keys(object).length
|
||||||
@ -84,10 +90,15 @@ function update(updates, object, ...args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(defaultedObject)) {
|
if (Array.isArray(defaultedObject)) {
|
||||||
return updateArray(resolvedUpdates, defaultedObject)
|
return updateArray(resolvedUpdates, defaultedObject).filter(
|
||||||
|
value => value !== innerOmitted
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return { ...defaultedObject, ...resolvedUpdates }
|
return _omitBy(
|
||||||
|
{ ...defaultedObject, ...resolvedUpdates },
|
||||||
|
value => value === innerOmitted
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default wrap(update, 2)
|
export default wrap(update, 2)
|
||||||
|
@ -142,4 +142,33 @@ describe('updeep', () => {
|
|||||||
const result = u({ created: date }, {})
|
const result = u({ created: date }, {})
|
||||||
expect(result).to.eql({ created: date })
|
expect(result).to.eql({ created: date })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const expectU = (update, orig, expected) =>
|
||||||
|
expect(update(orig)).to.eql(expected)
|
||||||
|
|
||||||
|
describe('u.omitted', () => {
|
||||||
|
it('omit properties via u.omitted', () => {
|
||||||
|
expectU(u({ a: u.omitted, b: i => i + 1 }), { a: 1, b: 2 }, { b: 3 })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('omit array and object properties', () => {
|
||||||
|
expectU(
|
||||||
|
u({ a: u.omitted, b: 'stuff', c: u.omitted }),
|
||||||
|
{ a: [1, 2, 3], b: 'orig', c: { z: 'bar' } },
|
||||||
|
{ b: 'stuff' }
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('deep omit', () => {
|
||||||
|
expectU(
|
||||||
|
u({ a: { b: u.omitted, c: 'stuff' } }),
|
||||||
|
{ a: { b: 'foo', z: 'bar' } },
|
||||||
|
{ a: { z: 'bar', c: 'stuff' } }
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('omitting an array item filters it out', () => {
|
||||||
|
expectU(u({ 1: u.omitted }), ['a', 'b', 'c'], ['a', 'c'])
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user