Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

chore: merge options.debug and options #718

20 changes: 18 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ export function start(
options?: DebugAgentConfig | StackdriverConfig
): Debuglet | IsReady {
options = options || {};
const agentConfig: DebugAgentConfig =
(options as StackdriverConfig).debug || (options as DebugAgentConfig);
const agentConfig: DebugAgentConfig = mergeConfigs(options);

// forceNewAgent_ is for testing purposes only.
if (debuglet && !agentConfig.forceNewAgent_) {
Expand All @@ -53,3 +52,20 @@ export function start(

return agentConfig.testMode_ ? debuglet : debuglet.isReadyManager;
}

/**
* If the given `options` object has a `debug` property
* of the same type, this function returns the union of the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should document which one takes precedence if both objects have identically named members, here and anywhere else where the debug field is referenced.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* properties in `options.debug` and `options` except that
* the returned object no longer has a `debug` property.
* If a field exists in both `options` and `options.debug`,
* the value in `option.debug` takes precedence.
*/
function mergeConfigs<T>(options: T & {debug?: T}): T {
if (!options.debug) {
return options;
}
const result = Object.assign({}, options);
delete result.debug;
return Object.assign(result, options.debug);
}