-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added
unshift()
and unshiftAll()
operations
- Loading branch information
1 parent
e0ff8e6
commit 9c47290
Showing
3 changed files
with
103 additions
and
1 deletion.
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,41 @@ | ||
const unshiftAll = require('../unshiftAll'); | ||
|
||
module.exports = unshift; | ||
|
||
/** | ||
* Adds an element to the beginning of array-item selected by path | ||
* @param {Object | any[]} object object to update | ||
* @param {string | string[]} path path to be updated | ||
* @param {*} value value to be added | ||
* @returns {Object | any[]} updated object | ||
* @description one of additional functions to work with array items | ||
* @docs | ||
* ```js | ||
* import { unshift } from 'immutable-object-update'; | ||
* | ||
* const state = { | ||
* a: { | ||
* b: [ 1 ] | ||
* } | ||
* }; | ||
* | ||
* const updated = unshift(state, [ 'a', 'b' ], 0); | ||
* | ||
* // or | ||
* | ||
* const updated = unshift(state, 'a.b', 0); | ||
* ``` | ||
* | ||
* As a result we will receive new object with structure below: | ||
* | ||
* ```js | ||
* { | ||
* a: { | ||
* b: [ 0, 1 ] | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
function unshift(object, path, value) { | ||
return unshiftAll(object, path, [ value ]); | ||
} |
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,47 @@ | ||
const { updateArray } = require('../common'); | ||
|
||
module.exports = unshiftAll; | ||
|
||
/** | ||
* Adds number of elements to the beginning of array-item selected by path | ||
* @param {Object | any[]} object object to update | ||
* @param {string | string[]} path path to be updated | ||
* @param {any[]} values values to be added | ||
* @returns {Object | any[]} updated object | ||
* @description one of additional functions to work with array items | ||
* @docs | ||
* ```js | ||
* import { unshiftAll } from 'immutable-object-update'; | ||
* | ||
* const state = { | ||
* a: { | ||
* b: [ 1 ] | ||
* } | ||
* }; | ||
* | ||
* const updated = unshiftAll(state, [ 'a', 'b' ], [ 2, 3, 4 ]); | ||
* | ||
* // or | ||
* | ||
* const updated = unshiftAll(state, 'a.b', [ 2, 3, 4 ]); | ||
* ``` | ||
* | ||
* As a result we will receive new object with structure below: | ||
* | ||
* ```js | ||
* { | ||
* a: { | ||
* b: [ 2, 3, 4, 1 ] | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* > Please note: items will be added all at once, so the original order will be saved | ||
*/ | ||
function unshiftAll(object, path, values) { | ||
return updateArray( | ||
object, | ||
path, | ||
array => values.concat(array) | ||
); | ||
} |