-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: overwrite instead of merging when setting nested paths
Fix #9121
- Loading branch information
Showing
3 changed files
with
123 additions
and
26 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
|
||
const setDottedPath = require('../path/setDottedPath'); | ||
|
||
/** | ||
* Given an object that may contain dotted paths, flatten the paths out. | ||
* For example: `flattenObjectWithDottedPaths({ a: { 'b.c': 42 } })` => `{ a: { b: { c: 42 } } }` | ||
*/ | ||
|
||
module.exports = function flattenObjectWithDottedPaths(obj) { | ||
if (obj == null || typeof obj !== 'object' || Array.isArray(obj)) { | ||
return; | ||
} | ||
const keys = Object.keys(obj); | ||
for (const key of keys) { | ||
const val = obj[key]; | ||
if (key.indexOf('.') !== -1) { | ||
delete obj[key]; | ||
setDottedPath(obj, key, val); | ||
continue; | ||
} | ||
|
||
flattenObjectWithDottedPaths(obj[key]); | ||
} | ||
}; |
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