-
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
pop()
and popN()
operations
- Loading branch information
1 parent
d7a527c
commit b2b6a23
Showing
3 changed files
with
88 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,42 @@ | ||
const popN = require('../popN'); | ||
|
||
module.exports = pop; | ||
|
||
/** | ||
* Removes an element from the end of array-item selected by path | ||
* @param {Object | any[]} object object to update | ||
* @param {string | string[]} path path to be updated | ||
* @returns {Object | any[]} updated object | ||
* @description one of additional functions to work with array items | ||
* @docs | ||
* ```js | ||
* import { pop } from 'immutable-object-update'; | ||
* | ||
* const state = { | ||
* a: { | ||
* b: [ 1, 2, 3 ] | ||
* } | ||
* }; | ||
* | ||
* const updated = pop(state, [ 'a', 'b' ]); | ||
* | ||
* // or | ||
* | ||
* const updated = pop(state, 'a.b'); | ||
* ``` | ||
* | ||
* As a result we will receive new object with structure below: | ||
* | ||
* ```js | ||
* { | ||
* a: { | ||
* b: [ 1, 2 ] | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* > Please note: `pop()` function won't return removed item | ||
*/ | ||
function pop(object, path) { | ||
return popN(object, path, 1); | ||
} |
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,43 @@ | ||
const { updateArray } = require('../common'); | ||
|
||
module.exports = popN; | ||
|
||
/** | ||
* Removes number of elements from the end of array-item selected by path | ||
* @param {Object | any[]} object object to update | ||
* @param {string | string[]} path path to be updated | ||
* @param {number} n number of elements to remove | ||
* @returns {Object | any[]} updated object | ||
* @description one of additional functions to work with array items | ||
* @docs | ||
* ```js | ||
* import { popN } from 'immutable-object-update'; | ||
* | ||
* const state = { | ||
* a: { | ||
* b: [ 1, 2, 3 ] | ||
* } | ||
* }; | ||
* | ||
* const updated = popN(state, [ 'a', 'b' ], 2); | ||
* | ||
* // or | ||
* | ||
* const updated = popN(state, 'a.b', 2); | ||
* ``` | ||
* | ||
* As a result we will receive new object with structure below: | ||
* | ||
* ```js | ||
* { | ||
* a: { | ||
* b: [ 1 ] | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* > Please note: `popN()` function won't return removed items | ||
*/ | ||
function popN(object, path, n) { | ||
return updateArray(object, path, array => array.slice(0, -n)); | ||
} |