-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
94 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
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,24 @@ | ||
import toArray from 'lodash/toArray' | ||
import update from '../object/update' | ||
|
||
/** | ||
* Changes the contents of an array by removing existing elements and/or adding new elements. | ||
* @function splice | ||
* @memberof array | ||
* @param {Object} object The object to modify. | ||
* @param {Array|string} path The path of the property to set. | ||
* @param {number} start Index at which to start changing the array. | ||
* @param {number} [deleteCount] The number of old array elements to remove. | ||
* @param {...*} values The values to add. | ||
* @return {Object} Returns the updated object. | ||
* @example splice({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 1, 2, 5, 6) // => { nested: { prop: [1, 5, 6, 4] } } | ||
* @see {@link https://lodash.com/docs#toArray|lodash.toArray} for more information. | ||
* @see {@link https://mdn.io/Array.prototype.splice|Array.prototype.splice} for more information. | ||
* @see {@link object.update|update} for more information. | ||
* @since 0.1.14 | ||
*/ | ||
export default update((array, ...args) => { | ||
const newArray = toArray(array) | ||
newArray.splice(...args) | ||
return newArray | ||
}) |
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,14 @@ | ||
/* eslint-env jest */ | ||
import splice from './splice' | ||
|
||
describe('Splice', () => { | ||
|
||
it('should replace two elements', () => { | ||
const original = { nested: { prop: [1, 2, 3, 4] } } | ||
|
||
const final = splice(original, 'nested.prop', 1, 2, 5, 6) | ||
|
||
expect(final).toEqual({ nested: { prop: [1, 5, 6, 4] } }) | ||
expect(original).toEqual({ nested: { prop: [1, 2, 3, 4] } }) | ||
}) | ||
}) |
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,25 @@ | ||
import toArray from 'lodash/toArray' | ||
import update from '../object/update' | ||
|
||
/** | ||
* Adds one or more elements at the start of an array. | ||
* @function unshift | ||
* @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 add. | ||
* @return {Object} Returns the updated object. | ||
* @example <caption>Add one element.</caption> | ||
* push({ nested: { prop: [1, 2] } }, 'nested.prop', 3) // => { nested: { prop: [3, 1, 2] } } | ||
* @example <caption>Add several elements.</caption> | ||
* push({ nested: { prop: [1, 2] } }, 'nested.prop', 3, 4) // => { nested: { prop: [3, 4, 1, 2] } } | ||
* @see {@link https://lodash.com/docs#toArray|lodash.toArray} for more information. | ||
* @see {@link https://mdn.io/Array.prototype.unshift|Array.prototype.unshift} for more information. | ||
* @see {@link object.update|update} for more information. | ||
* @since 0.1.7 | ||
*/ | ||
export default update((array, ...values) => { | ||
const newArray = toArray(array) | ||
newArray.unshift(...values) | ||
return newArray | ||
}) |
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 unshift from './unshift' | ||
|
||
describe('Unshift', () => { | ||
|
||
it('should add one element', () => { | ||
const original = { nested: { prop: [1, 2] } } | ||
|
||
const final = unshift(original, 'nested.prop', 3) | ||
|
||
expect(final).toEqual({ nested: { prop: [3, 1, 2] } }) | ||
expect(original).toEqual({ nested: { prop: [1, 2] } }) | ||
}) | ||
|
||
it('should add several elements', () => { | ||
expect(unshift({ nested: { prop: [1, 2] } }, 'nested.prop', 3, 4)).toEqual({ nested: { prop: [3, 4, 1, 2] } }) | ||
}) | ||
|
||
it('should replace deep undefined with array', () => { | ||
expect(unshift(undefined, 'nested.prop', 1)).toEqual({ nested: { prop: [1] } }) | ||
}) | ||
}) |