-
-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use lodash to merge options and document assignOptions behaviors
Option merging is now consistent, so null header removal is done during options normalization. null could not be used to indicate property removal because null is a significant value for some settings.
- Loading branch information
Jonathan Stewmon
committed
Jul 25, 2018
1 parent
da7f055
commit 2a9611f
Showing
5 changed files
with
81 additions
and
28 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,27 +1,36 @@ | ||
const extend = require('extend'); | ||
const url = require('url'); | ||
const mergeWith = require('lodash.mergewith'); | ||
const cloneDeep = require('lodash.clonedeep'); | ||
const is = require('@sindresorhus/is'); | ||
|
||
module.exports = (defaults, options = {}) => { | ||
const returnOptions = extend(true, {}, defaults, options); | ||
return mergeWith( | ||
{opts: cloneDeep(defaults)}, | ||
{opts: options}, | ||
customizer | ||
).opts; | ||
}; | ||
|
||
if (Reflect.has(options, 'headers')) { | ||
for (const [key, value] of Object.entries(options.headers)) { | ||
if (is.nullOrUndefined(value)) { | ||
delete returnOptions.headers[key]; | ||
} | ||
} | ||
function customizer(objValue, srcValue) { | ||
if (is.array(srcValue) || is.array(objValue)) { | ||
return cloneDeep(srcValue); | ||
} | ||
|
||
// Override these arrays because we don't want to extend them | ||
if (is.object(options.retry)) { | ||
if (Reflect.has(options.retry, 'methods')) { | ||
returnOptions.retry.methods = options.retry.methods; | ||
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); | ||
} | ||
} | ||
|
||
if (Reflect.has(options.retry, 'statusCodes')) { | ||
returnOptions.retry.statusCodes = options.retry.statusCodes; | ||
const result = mergeWith(objValue, srcValue, customizer); | ||
for (const key of deleteKeys) { | ||
delete result[key]; | ||
} | ||
return result; | ||
} | ||
|
||
return returnOptions; | ||
}; | ||
} |
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