This repository has been archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨ Add object.defaults * ✨ Add object.mapKeys * ✨ Add object.merge * ✨ Add object.omit * ✨ Add object.omitBy * ✨ Add object.pick
- Loading branch information
Showing
17 changed files
with
307 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import _defaults from 'lodash/defaults' | ||
import { convert } from 'util/convert' | ||
|
||
/** | ||
* Replaces by an object assigning own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to <code>undefined</code>.<br > | ||
* Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored. | ||
* @function | ||
* @memberof object | ||
* @param {Object} object The object to modify. | ||
* @param {Array|string} path The path of the property to set. | ||
* @param {...Object} [sources] The source objects. | ||
* @return {Object} Returns the updated object. | ||
* @example defaults({ nested: { a: 1, b: 2 } }, 'nested', { b: 3, c: 4 }) // => { nested: { a:1, b: 2, c: 4 } } | ||
* @see {@link https://lodash.com/docs#defaults|lodash.defaults} for more information. | ||
* @since 0.3.0 | ||
*/ | ||
const defaults = convert(_defaults) | ||
export { defaults, defaults as default } |
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,34 @@ | ||
/* eslint-env jest */ | ||
import { defaults } from './defaults' | ||
import { immutaTest } from 'test.utils' | ||
|
||
describe('Defaults', () => { | ||
|
||
it('should assign default properties objects', () => { | ||
immutaTest((input, path) => { | ||
const output = defaults(input, path, { | ||
b: 3, | ||
c: 4, | ||
}) | ||
expect(output).toEqual({ | ||
nested: { | ||
prop: { | ||
a: 1, | ||
b: 2, | ||
c: 4, | ||
}, | ||
}, | ||
other: {}, | ||
}) | ||
return output | ||
}, { | ||
nested: { | ||
prop: { | ||
a: 1, | ||
b: 2, | ||
}, | ||
}, | ||
other: {}, | ||
}, 'nested.prop') | ||
}) | ||
}) |
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,18 @@ | ||
import _mapKeys from 'lodash/mapKeys' | ||
import { convert } from 'util/convert' | ||
|
||
/** | ||
* Replaces by an object with the same values as the former object and values generated by running each own enumerable string keyed property of the former object thru <code>iteratee</code>. | ||
* The iteratee is invoked with three arguments: (value, key, object). | ||
* @function | ||
* @memberof object | ||
* @param {Object} object The object to modify. | ||
* @param {Array|string} path The path of the property to set. | ||
* @param {function} [iteratee={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration. | ||
* @return {Object} Returns the updated object. | ||
* @example mapKeys({ nested: { a: 1, b: 2, c: 3 } }, 'nested', (v, k) => '_' + k) // => { nested: { _a: 1, _b: 2, _c: 3 } } | ||
* @see {@link https://lodash.com/docs#mapKeys|lodash.mapKeys} for more information. | ||
* @since 0.3.0 | ||
*/ | ||
const mapKeys = convert(_mapKeys) | ||
export { mapKeys, mapKeys as default } |
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,31 @@ | ||
/* eslint-env jest */ | ||
import { immutaTest } from 'test.utils' | ||
import { mapKeys } from './mapKeys' | ||
|
||
describe('MapKeys', () => { | ||
it('should replace the keys of object', () => { | ||
immutaTest((input, path) => { | ||
const output = mapKeys(input, path, (_, k) => `_${k}`) | ||
expect(output).toEqual({ | ||
nested: { | ||
prop: { | ||
_a: 1, | ||
_b: 2, | ||
_c: 3, | ||
}, | ||
}, | ||
other: {}, | ||
}) | ||
return output | ||
}, { | ||
nested: { | ||
prop: { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
}, | ||
}, | ||
other: {}, | ||
}, 'nested.prop') | ||
}) | ||
}) |
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,18 @@ | ||
import _merge from 'lodash/fp/merge' | ||
import { convertLodashFp } from 'util/convert' | ||
|
||
/** | ||
* Replaces by an object deeply merging own enumerable string keyed properties of source objects to the former object.<br /> | ||
* Source objects are applied from left to right. Subsequent sources overwrite properties of previous sources. | ||
* @function | ||
* @memberof object | ||
* @param {Object} object The object to modify. | ||
* @param {Array|string} path The path of the property to set. | ||
* @param {...Object} [sources] The source objects. | ||
* @return {Object} Returns the updated object. | ||
* @example merge({ nested: { prop: { a: 1 } } }, 'nested', { prop: { a: 2, b: 3 } }) // => { nested: { prop: { a: 2, b: 3 } } } | ||
* @see {@link https://lodash.com/docs#merge|lodash.merge} for more information. | ||
* @since 0.3.0 | ||
*/ | ||
const merge = convertLodashFp(_merge) | ||
export { merge, merge as default } |
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,30 @@ | ||
/* eslint-env jest */ | ||
import { immutaTest } from 'test.utils' | ||
import { merge } from './merge' | ||
|
||
describe('Merge', () => { | ||
|
||
it('should merge objects', () => { | ||
immutaTest((input, path) => { | ||
const output = merge(input, path, { | ||
prop: { | ||
a: 2, | ||
b: 3, | ||
}, | ||
}) | ||
expect(output).toEqual({ | ||
nested: { | ||
prop: { | ||
a: 2, | ||
b: 3, | ||
}, | ||
}, | ||
other: {}, | ||
}) | ||
return output | ||
}, { | ||
nested: { prop: { a: 1 } }, | ||
other: {}, | ||
}, 'nested') | ||
}) | ||
}) |
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,17 @@ | ||
import _omit from 'lodash/omit' | ||
import { convert } from 'util/convert' | ||
|
||
/** | ||
* Replaces by an object omitting specified properties. | ||
* @function | ||
* @memberof object | ||
* @param {Object} object The object to modify. | ||
* @param {Array|string} path The path of the property to set. | ||
* @param {...(string|string[])} [paths] The property paths to omit. | ||
* @return {Object} Returns the updated object. | ||
* @example omit({ nested: { a: 1, b: 2, c: 3 } }, 'nested', 'b') // => { nested: { a:1, c: 3 } } | ||
* @see {@link https://lodash.com/docs#omit|lodash.omit} for more information. | ||
* @since 0.3.0 | ||
*/ | ||
const omit = convert(_omit) | ||
export { omit, omit as default } |
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,31 @@ | ||
/* eslint-env jest */ | ||
import { immutaTest } from 'test.utils' | ||
import { omit } from './omit' | ||
|
||
describe('Omit', () => { | ||
|
||
it('should omit properties of object', () => { | ||
immutaTest((input, path) => { | ||
const output = omit(input, path, 'b') | ||
expect(output).toEqual({ | ||
nested: { | ||
prop: { | ||
a: 1, | ||
c: 3, | ||
}, | ||
}, | ||
other: {}, | ||
}) | ||
return output | ||
}, { | ||
nested: { | ||
prop: { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
}, | ||
}, | ||
other: {}, | ||
}, 'nested.prop') | ||
}) | ||
}) |
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,17 @@ | ||
import _omitBy from 'lodash/omitBy' | ||
import { convert } from 'util/convert' | ||
|
||
/** | ||
* Replaces by an object omitting properties that <code>predicate</code> doesn't return truthy for. | ||
* @function | ||
* @memberof object | ||
* @param {Object} object The object to modify. | ||
* @param {Array|string} path The path of the property to set. | ||
* @param {function} [predicate={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per property. | ||
* @return {Object} Returns the updated object. | ||
* @example omitBy({ nested: { a: 1, b: 2, c: 3 } }, 'nested', v => v === 2) // => { nested: { a:1, c: 3 } } | ||
* @see {@link https://lodash.com/docs#omitBy|lodash.omitBy} for more information. | ||
* @since 0.3.0 | ||
*/ | ||
const omitBy = convert(_omitBy) | ||
export { omitBy, omitBy as default } |
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,31 @@ | ||
/* eslint-env jest */ | ||
import { immutaTest } from 'test.utils' | ||
import { omitBy } from './omitBy' | ||
|
||
describe('OmitBy', () => { | ||
|
||
it('should omit properties matching predicate', () => { | ||
immutaTest((input, path) => { | ||
const output = omitBy(input, path, v => v === 2) | ||
expect(output).toEqual({ | ||
nested: { | ||
prop: { | ||
a: 1, | ||
c: 3, | ||
}, | ||
}, | ||
other: {}, | ||
}) | ||
return output | ||
}, { | ||
nested: { | ||
prop: { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
}, | ||
}, | ||
other: {}, | ||
}, 'nested.prop') | ||
}) | ||
}) |
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,17 @@ | ||
import _pick from 'lodash/pick' | ||
import { convert } from 'util/convert' | ||
|
||
/** | ||
* Replaces by an object picking specified properties. | ||
* @function | ||
* @memberof object | ||
* @param {Object} object The object to modify. | ||
* @param {Array|string} path The path of the property to set. | ||
* @param {...(string|string[])} [paths] The property paths to pick. | ||
* @return {Object} Returns the updated object. | ||
* @example pick({ nested: { a: 1, b: 2, c: 3 } }, 'nested', 'b') // => { nested: { b: 2 } } | ||
* @see {@link https://lodash.com/docs#pick|lodash.pick} for more information. | ||
* @since 0.3.0 | ||
*/ | ||
const pick = convert(_pick) | ||
export { pick, pick as default } |
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,26 @@ | ||
/* eslint-env jest */ | ||
import { immutaTest } from 'test.utils' | ||
import { pick } from './pick' | ||
|
||
describe('Pick', () => { | ||
|
||
it('should pick properties of object', () => { | ||
immutaTest((input, path) => { | ||
const output = pick(input, path, 'b') | ||
expect(output).toEqual({ | ||
nested: { prop: { b: 2 } }, | ||
other: {}, | ||
}) | ||
return output | ||
}, { | ||
nested: { | ||
prop: { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
}, | ||
}, | ||
other: {}, | ||
}, 'nested.prop') | ||
}) | ||
}) |
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