-
-
Notifications
You must be signed in to change notification settings - Fork 957
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use our own merge instead of lodash
We have a narrow set of rules for merging objects and lodash's more aggressive merge may have unintended consequences when the values being merged are not plain objects.
- Loading branch information
Showing
4 changed files
with
30 additions
and
36 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
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,36 +1,30 @@ | ||
const url = require('url'); | ||
const mergeWith = require('lodash.mergewith'); | ||
const cloneDeep = require('lodash.clonedeep'); | ||
const is = require('@sindresorhus/is'); | ||
|
||
module.exports = (defaults, options = {}) => { | ||
return mergeWith( | ||
{opts: cloneDeep(defaults)}, | ||
{opts: options}, | ||
customizer | ||
).opts; | ||
return merge({}, defaults, options); | ||
}; | ||
|
||
function customizer(objValue, srcValue) { | ||
if (is.array(srcValue) || is.array(objValue)) { | ||
return cloneDeep(srcValue); | ||
} | ||
if (objValue instanceof url.URL) { | ||
return new url.URL(srcValue, objValue); | ||
} | ||
if (![objValue, srcValue].some(is.array) && [objValue, srcValue].every(is.object)) { | ||
// When both args are non-array objects, delete keys for which the source | ||
// value is undefined (null is a significant value places, e.g. `encoding`). | ||
const deleteKeys = []; | ||
for (const key in srcValue) { | ||
if (is.undefined(srcValue[key])) { | ||
deleteKeys.push(key); | ||
function merge(target = {}, ...sources) { | ||
for (const source of sources) { | ||
for (const [key, sourceValue] of Object.entries(source)) { | ||
const targetValue = target[key]; | ||
if (is.undefined(sourceValue)) { | ||
delete target[key]; | ||
} else if (is.urlInstance(targetValue) && ( | ||
is.urlInstance(sourceValue) || is.string(sourceValue) | ||
)) { | ||
target[key] = new url.URL(sourceValue, targetValue); | ||
} else if (is.plainObject(sourceValue)) { | ||
if (is.plainObject(targetValue)) { | ||
target[key] = merge({}, targetValue, sourceValue); | ||
} else { | ||
target[key] = merge({}, sourceValue); | ||
} | ||
} else { | ||
target[key] = sourceValue; | ||
} | ||
} | ||
const result = mergeWith(objValue, srcValue, customizer); | ||
for (const key of deleteKeys) { | ||
delete result[key]; | ||
} | ||
return result; | ||
} | ||
return target; | ||
} |
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