-
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: re-implemented basic feature as a
set
operation
BREAKING CHANGE: export structure updated
- Loading branch information
1 parent
4761789
commit 315df41
Showing
5 changed files
with
70 additions
and
59 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 |
---|---|---|
@@ -1 +1 @@ | ||
module.exports = require('./src/update'); | ||
module.exports = require('./src'); |
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,5 @@ | ||
const set = require('./set'); | ||
|
||
module.exports = { | ||
set | ||
}; |
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 @@ | ||
module.exports = set; | ||
|
||
function set(object, path, value) { | ||
const pathArray = Array.isArray(path) | ||
? path | ||
: path.split('.'); | ||
|
||
return doSet(object, pathArray, 0, value); | ||
} | ||
|
||
function doSet(object, path, index, value) { | ||
if (index === path.length) { | ||
return value; | ||
} | ||
|
||
const key = path[index]; | ||
|
||
if (!key) { | ||
return doSet(object, path, index + 1, value); | ||
} | ||
|
||
const source = object || createSource(key); | ||
const item = source[key]; | ||
const newItem = doSet(item, path, index + 1, value); | ||
|
||
if (item === newItem) { | ||
return source; | ||
} | ||
|
||
if (!Array.isArray(source)) { | ||
return Object.assign({}, source, { | ||
[key]: newItem | ||
}); | ||
} | ||
|
||
if (isNumber(key)) { | ||
return source | ||
.slice(0, key) | ||
.concat( | ||
[ newItem ], | ||
source.slice(key + 1) | ||
); | ||
} | ||
|
||
return Object.assign(source.slice(), { | ||
[key]: newItem | ||
}); | ||
} | ||
|
||
function createSource(key) { | ||
return isNumber(key) ? [] : {}; | ||
} | ||
|
||
function isNumber(value) { | ||
return value.toString() === parseInt(value, 0).toString(); | ||
} |
This file was deleted.
Oops, something went wrong.
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