-
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
push()
and pushAll()
operations
- Loading branch information
1 parent
bb899ff
commit d7a527c
Showing
4 changed files
with
97 additions
and
2 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
const update = require('./update'); | ||
const set = require('./set'); | ||
const remove = require('./remove'); | ||
const push = require('./push'); | ||
const pushAll = require('./pushAll'); | ||
|
||
module.exports = { update, set, remove }; | ||
module.exports = { update, set, remove, push, pushAll }; |
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 pushAll = require('../pushAll'); | ||
|
||
module.exports = push; | ||
|
||
/** | ||
* Adds an element to the end 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 { push } from 'immutable-object-update'; | ||
* | ||
* const state = { | ||
* a: { | ||
* b: [ 1 ] | ||
* } | ||
* }; | ||
* | ||
* const updated = push(state, [ 'a', 'b' ], 2); | ||
* | ||
* // or | ||
* | ||
* const updated = push(state, 'a.b', 2); | ||
* ``` | ||
* | ||
* As a result we will receive new object with structure below: | ||
* | ||
* ```js | ||
* { | ||
* a: { | ||
* b: [ 1, 2 ] | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
function push(object, path, value) { | ||
return pushAll(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,45 @@ | ||
const { updateArray } = require('../common'); | ||
|
||
module.exports = pushAll; | ||
|
||
/** | ||
* Adds number of elements to the end 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 { pushAll } from 'immutable-object-update'; | ||
* | ||
* const state = { | ||
* a: { | ||
* b: [ 1 ] | ||
* } | ||
* }; | ||
* | ||
* const updated = pushAll(state, [ 'a', 'b' ], [ 2, 3, 4 ]); | ||
* | ||
* // or | ||
* | ||
* const updated = pushAll(state, 'a.b', [ 2, 3, 4 ]); | ||
* ``` | ||
* | ||
* As a result we will receive new object with structure below: | ||
* | ||
* ```js | ||
* { | ||
* a: { | ||
* b: [ 1, 2, 3, 4 ] | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
function pushAll(object, path, values) { | ||
return updateArray( | ||
object, | ||
path, | ||
array => array.concat(values) | ||
); | ||
} |