-
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
insert()
and insertAll()
operations
- Loading branch information
1 parent
0dce914
commit 819b9ca
Showing
3 changed files
with
102 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 insertAll = require('../insertAll'); | ||
|
||
module.exports = insert; | ||
|
||
/** | ||
* Inserts an element to the specified position of array-item selected by path | ||
* @param {Object | any[]} object object to update | ||
* @param {string | string[]} path path to be updated | ||
* @param {any} value value to be inserted | ||
* @returns {Object | any[]} updated object | ||
* @description one of additional functions to work with array items | ||
* @docs | ||
* ```js | ||
* import { insert } from 'immutable-object-update'; | ||
* | ||
* const state = { | ||
* a: { | ||
* b: [ 1, 2 ] | ||
* } | ||
* }; | ||
* | ||
* const updated = insert(state, [ 'a', 'b', 1 ], 3); | ||
* | ||
* // or | ||
* | ||
* const updated = insert(state, 'a.b.1', 3); | ||
* ``` | ||
* | ||
* As a result we will receive new object with structure below: | ||
* | ||
* ```js | ||
* { | ||
* a: { | ||
* b: [ 1, 3, 2 ] | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
function insert(object, path, value) { | ||
return insertAll(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,56 @@ | ||
const { extractSubPath, isNumber, updateArray } = require('../common'); | ||
|
||
module.exports = insertAll; | ||
|
||
/** | ||
* Inserts number of elements to the specified position 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 inserted | ||
* @returns {Object | any[]} updated object | ||
* @description one of additional functions to work with array items | ||
* @docs | ||
* ```js | ||
* import { insertAll } from 'immutable-object-update'; | ||
* | ||
* const state = { | ||
* a: { | ||
* b: [ 1, 2 ] | ||
* } | ||
* }; | ||
* | ||
* const updated = insertAll(state, [ 'a', 'b', 1 ], [ 3, 4 ]); | ||
* | ||
* // or | ||
* | ||
* const updated = insertAll(state, 'a.b.1', [ 3, 4 ]); | ||
* ``` | ||
* | ||
* As a result we will receive new object with structure below: | ||
* | ||
* ```js | ||
* { | ||
* a: { | ||
* b: [ 1, 3, 4, 2 ] | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
function insertAll(object, path, values) { | ||
const [ itemPath, valuePath ] = extractSubPath(path); | ||
|
||
if (!isNumber(valuePath)) { | ||
return object; | ||
} | ||
|
||
return updateArray( | ||
object, | ||
itemPath, | ||
array => array | ||
.slice(0, valuePath) | ||
.concat( | ||
values, | ||
array.slice(valuePath) | ||
) | ||
); | ||
} |