-
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 array.concat * ✨ Add array.difference * ✨ Add array.drop * ✨ Add array.pull * ✨ Add array.remove
- Loading branch information
Showing
15 changed files
with
202 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import _concat from 'lodash/concat' | ||
import convert from '../util/convert' | ||
|
||
/** | ||
* Replaces an array concatenating the former array with additional arrays and/or values. | ||
* @function | ||
* @memberof array | ||
* @param {Object} object The object to modify. | ||
* @param {Array|string} path The path of the property to set. | ||
* @param {...Array} arrays The arrays to concatenate. | ||
* @return {Object} Returns the updated object. | ||
* @example concat({ nested: { prop: [1, 2] } }, 'nested.prop', [3, 4]) // => { nested: { prop: [1, 2, 3, 4] } } | ||
* @see {@link https://lodash.com/docs#concat|lodash.concat} for more information. | ||
* @since 0.2.0 | ||
*/ | ||
const concat = convert(_concat) | ||
export default concat |
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,22 @@ | ||
/* eslint-env jest */ | ||
import concat from './concat' | ||
|
||
describe('Concat', () => { | ||
|
||
it('should concat an array', () => { | ||
const original = { nested: { prop: [1, 2] } } | ||
|
||
const final = concat(original, 'nested.prop', [3, 4]) | ||
|
||
expect(final).toEqual({ nested: { prop: [1, 2, 3, 4] } }) | ||
expect(original).toEqual({ nested: { prop: [1, 2] } }) | ||
}) | ||
|
||
it('should add several arrays', () => { | ||
expect(concat({ nested: { prop: [1, 2] } }, 'nested.prop', [3, 4], [5, 6])).toEqual({ nested: { prop: [1, 2, 3, 4, 5, 6] } }) | ||
}) | ||
|
||
it('should replace value with array', () => { | ||
expect(concat({ nested: { prop: 1 } }, 'nested.prop', [2, 3])).toEqual({ nested: { prop: [1, 2, 3] } }) | ||
}) | ||
}) |
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 _difference from 'lodash/difference' | ||
import convert from '../util/convert' | ||
|
||
/** | ||
* Replaces an array removing values in the other given arrays from the former array. | ||
* @function | ||
* @memberof array | ||
* @param {Object} object The object to modify. | ||
* @param {Array|string} path The path of the property to set. | ||
* @param {...Array} arrays The arrays of values to exclude. | ||
* @return {Object} Returns the updated object. | ||
* @example difference({ nested: { prop: [1, 2] } }, 'nested.prop', [2, 3]) // => { nested: { prop: [1] } } | ||
* @see {@link https://lodash.com/docs#difference|lodash.difference} for more information. | ||
* @since 0.2.0 | ||
*/ | ||
const difference = convert(_difference) | ||
export default difference |
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,22 @@ | ||
/* eslint-env jest */ | ||
import difference from './difference' | ||
|
||
describe('Difference', () => { | ||
|
||
it('should remove intersecting elements', () => { | ||
const original = { nested: { prop: [1, 2, 3] } } | ||
|
||
const final = difference(original, 'nested.prop', [3, 4]) | ||
|
||
expect(final).toEqual({ nested: { prop: [1, 2] } }) | ||
expect(original).toEqual({ nested: { prop: [1, 2, 3] } }) | ||
}) | ||
|
||
it('should remove intersecting elements from several arrays', () => { | ||
expect(difference({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', [1], [2])).toEqual({ nested: { prop: [3, 4] } }) | ||
}) | ||
|
||
it('should replace deep undefined with array', () => { | ||
expect(difference(undefined, 'nested.prop', [1, 2])).toEqual({ 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 _drop from 'lodash/drop' | ||
import convert from '../util/convert' | ||
|
||
/** | ||
* Replaces an array dropping one or several elements at the start of the former array. | ||
* @function | ||
* @memberof array | ||
* @param {Object} object The object to modify. | ||
* @param {Array|string} path The path of the property to set. | ||
* @param {number} [n=1] The number of elements to drop. | ||
* @return {Object} Returns the updated object. | ||
* @example drop({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 2) // => { nested: { prop: [3, 4] } } | ||
* @see {@link https://lodash.com/docs#drop|lodash.drop} for more information. | ||
* @since 0.2.0 | ||
*/ | ||
const drop = convert(_drop) | ||
export default drop |
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,22 @@ | ||
/* eslint-env jest */ | ||
import drop from './drop' | ||
|
||
describe('Drop', () => { | ||
|
||
it('should drop an element at the start of the array', () => { | ||
const original = { nested: { prop: [1, 2, 3] } } | ||
|
||
const final = drop(original, 'nested.prop') | ||
|
||
expect(final).toEqual({ nested: { prop: [2, 3] } }) | ||
expect(original).toEqual({ nested: { prop: [1, 2, 3] } }) | ||
}) | ||
|
||
it('should drop several elements at the start of the array', () => { | ||
expect(drop({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 2)).toEqual({ nested: { prop: [3, 4] } }) | ||
}) | ||
|
||
it('should replace deep undefined with array', () => { | ||
expect(drop(undefined, 'nested.prop')).toEqual({ 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,17 @@ | ||
import _pull from 'lodash/pull' | ||
import convert from '../util/convert' | ||
|
||
/** | ||
* Replaces an array removing all given values from the former array. | ||
* @function | ||
* @memberof array | ||
* @param {Object} object The object to modify. | ||
* @param {Array|string} path The path of the property to set. | ||
* @param {...*} values The values to remove. | ||
* @return {Object} Returns the updated object. | ||
* @example pull({ nested: { prop: [1, 2, 3, 1, 2, 3] } }, 'nested.prop', 1, 3) // => { nested: { prop: [2, 2] } } | ||
* @see {@link https://lodash.com/docs#pull|lodash.pull} for more information. | ||
* @since 0.2.0 | ||
*/ | ||
const pull = convert(_pull) | ||
export default pull |
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 @@ | ||
/* eslint-env jest */ | ||
import pull from './pull' | ||
|
||
describe('Pull', () => { | ||
|
||
it('should remove matching elements', () => { | ||
const original = { nested: { prop: [1, 2, 3, 1, 2, 3] } } | ||
|
||
const final = pull(original, 'nested.prop', 2) | ||
|
||
expect(final).toEqual({ nested: { prop: [1, 3, 1, 3] } }) | ||
expect(original).toEqual({ nested: { prop: [1, 2, 3, 1, 2, 3] } }) | ||
}) | ||
|
||
it('should remove several matching elements', () => { | ||
expect(pull({ nested: { prop: [1, 2, 3, 1, 2, 3] } }, 'nested.prop', 1, 3)).toEqual({ nested: { prop: [2, 2] } }) | ||
}) | ||
}) |
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,17 @@ | ||
import _remove from 'lodash/fp/remove' | ||
import { convertLodashFp } from '../util/convert' | ||
|
||
/** | ||
* Replaces an array removing elements that predicate returns truthy for from the former array. | ||
* @function | ||
* @memberof array | ||
* @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 iteration. | ||
* @return {Object} Returns the updated object. | ||
* @example remove({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', v => v > 2) // => { nested: { prop: [1, 2] } } | ||
* @see {@link https://lodash.com/docs#remove|lodash.remove} for more information. | ||
* @since 0.2.0 | ||
*/ | ||
const remove = convertLodashFp(_remove) | ||
export default remove |
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 @@ | ||
/* eslint-env jest */ | ||
import remove from './remove' | ||
|
||
describe('Drop', () => { | ||
|
||
it('should remove an element', () => { | ||
const original = { nested: { prop: [1, 2, 3] } } | ||
|
||
const final = remove(original, 'nested.prop', v => v > 2) | ||
|
||
expect(final).toEqual({ nested: { prop: [1, 2] } }) | ||
expect(original).toEqual({ nested: { prop: [1, 2, 3] } }) | ||
}) | ||
|
||
it('should replace deep undefined with array', () => { | ||
expect(remove(undefined, 'nested.prop', () => true)).toEqual({ 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
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