setDefaults
SetDefaults is a common pattern or function name used to apply a baseline set of configuration values when a consumer provides only a partial configuration. The idea is to ensure that all required fields have sensible values while allowing user-supplied options to override the defaults.
In practice, setDefaults works by defining a defaults object and merging it with the user’s options. The
function setDefaults(options) {
const defaults = { host: 'localhost', port: 3000, useSSL: false, timeout: 5000 };
return { ...defaults, ...options };
}
Here, options override defaults, and unspecified keys fall back to defaults.
Variants of this pattern include shallow versus deep merging. A shallow merge combines top-level fields, which
Common use cases include libraries, frameworks, and components that accept an options object, as well as CLI