📄 applyDefault¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 1 |
| 📦 Imports | 2 |
| 📑 Type Aliases | 1 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/utils/src/eslint-utils/applyDefault.ts
📦 Imports¶
| Name | Source |
|---|---|
deepMerge |
./deepMerge |
isObjectNotArray |
./deepMerge |
Functions¶
applyDefault(defaultOptions: Readonly<Default>, userOptions: Readonly<User> | null): Default¶
Pure function - doesn't mutate either parameter! Uses the default options and overrides with the options provided by the user
Parameters:
defaultOptionsany: the defaultsuserOptionsany: the user opts
Returns: undefined
the options with defaults
Raw JSDoc
Calls:
structuredClone(options as unknown[]).forEachisObjectNotArray (from ./deepMerge)deepMerge (from ./deepMerge)
Internal Comments:
// clone defaults (x2)
// For avoiding the type error (x4)
// `This expression is not callable. Type 'unknown' has no call signatures.ts(2349)` (x4)
// eslint-disable-next-line @typescript-eslint/internal/eqeq-nullish
Code
export function applyDefault<
User extends readonly unknown[],
Default extends User,
>(
defaultOptions: Readonly<Default>,
userOptions: Readonly<User> | null,
): Default {
// clone defaults
const options = structuredClone(defaultOptions) as AsMutable<Default>;
if (userOptions == null) {
return options;
}
// For avoiding the type error
// `This expression is not callable. Type 'unknown' has no call signatures.ts(2349)`
(options as unknown[]).forEach((opt: unknown, i: number) => {
// eslint-disable-next-line @typescript-eslint/internal/eqeq-nullish
if (userOptions[i] !== undefined) {
const userOpt = userOptions[i];
if (isObjectNotArray(userOpt) && isObjectNotArray(opt)) {
options[i] = deepMerge(opt, userOpt);
} else {
options[i] = userOpt;
}
}
});
return options;
}
Type Aliases¶
AsMutable<T extends readonly unknown[]>¶
Generated by Syntax Scribe