-
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: implemented
remove()
operation
- Loading branch information
1 parent
07c746d
commit b72cf7d
Showing
4 changed files
with
60 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module.exports = { | ||
isNumber, | ||
extractSubPath | ||
}; | ||
|
||
function isNumber(value) { | ||
return !Number.isNaN(+value); | ||
} | ||
|
||
function extractSubPath(path) { | ||
return [ | ||
path.slice(0, -1), | ||
path[path.length - 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 |
---|---|---|
@@ -1,7 +1,24 @@ | ||
const update = require('./update'); | ||
const set = require('./set'); | ||
const remove = require('./remove'); | ||
|
||
module.exports = { | ||
set, | ||
update | ||
...mapValues({ update, set, remove }, preparePath) | ||
}; | ||
|
||
function mapValues(object, map) { | ||
return Object.entries(object) | ||
.reduce( | ||
(reduced, [ key, value ]) => Object.assign(reduced, { | ||
[key]: map(value) | ||
}), {} | ||
); | ||
} | ||
|
||
function preparePath(fn) { | ||
return (object, path, ...rest) => fn( | ||
object, | ||
Array.isArray(path) ? path : path.split('.'), | ||
...rest | ||
); | ||
} |
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,23 @@ | ||
const update = require('../update'); | ||
const { isNumber, extractSubPath } = require('../common'); | ||
|
||
module.exports = remove; | ||
|
||
function remove(object, path) { | ||
const [ itemPath, valuePath ] = extractSubPath(path); | ||
|
||
return update(object, itemPath, (item) => { | ||
if (Array.isArray(item) && isNumber(valuePath)) { | ||
return item | ||
.slice(0, valuePath) | ||
.concat(item.slice(+valuePath + 1)); | ||
} | ||
|
||
const { | ||
[valuePath]: temp, | ||
...newItem | ||
} = item; | ||
|
||
return newItem; | ||
}); | ||
} |
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