Skip to content

Commit

Permalink
Create ParameterType.Object
Browse files Browse the repository at this point in the history
This is mostly a clone of `mixed` except that it accepts and processes the `oldValue` function param
  • Loading branch information
citkane committed Aug 26, 2022
1 parent 29234dc commit fa012f0
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/lib/utils/options/declaration.ts
Expand Up @@ -225,6 +225,10 @@ export enum ParameterType {
* Resolved according to the config directory unless it starts with `**`, after skipping any leading `!` and `#` characters.
*/
GlobArray,
/**
* An unopinionated object that preserves default settings unless explicitly overridden
*/
Object,
/**
* An object with true/false flags
*/
Expand Down Expand Up @@ -341,6 +345,20 @@ export interface MixedDeclarationOption extends DeclarationOptionBase {
validate?: (value: unknown) => void;
}

export interface ObjectDeclarationOption extends DeclarationOptionBase {
type: ParameterType.Object;

/**
* If not specified defaults to undefined.
*/
defaultValue?: unknown;

/**
* An optional validation function that validates a potential value of this option.
* The function must throw an Error if the validation fails and should do nothing otherwise.
*/
validate?: (value: unknown) => void;
}
export interface MapDeclarationOption<T> extends DeclarationOptionBase {
type: ParameterType.Map;

Expand Down Expand Up @@ -378,6 +396,7 @@ export type DeclarationOption =
| NumberDeclarationOption
| BooleanDeclarationOption
| MixedDeclarationOption
| ObjectDeclarationOption
| MapDeclarationOption<unknown>
| ArrayDeclarationOption
| FlagsDeclarationOption<Record<string, boolean>>;
Expand All @@ -388,6 +407,7 @@ export interface ParameterTypeToOptionTypeMap {
[ParameterType.Number]: number;
[ParameterType.Boolean]: boolean;
[ParameterType.Mixed]: unknown;
[ParameterType.Object]: unknown;
[ParameterType.Array]: string[];
[ParameterType.PathArray]: string[];
[ParameterType.ModuleArray]: string[];
Expand Down Expand Up @@ -503,6 +523,12 @@ const converters: {
option.validate?.(value);
return value;
},
[ParameterType.Object](value, option, _configPath, oldValue) {
option.validate?.(value);
if (typeof oldValue !== "undefined")
value = { ...(oldValue as {}), ...(value as {}) };
return value;
},
[ParameterType.Flags](value, option) {
if (typeof value === "boolean") {
value = Object.fromEntries(
Expand Down Expand Up @@ -596,6 +622,9 @@ const defaultGetters: {
[ParameterType.Mixed](option) {
return option.defaultValue;
},
[ParameterType.Object](option) {
return option.defaultValue;
},
[ParameterType.Array](option) {
return option.defaultValue?.slice() ?? [];
},
Expand Down

0 comments on commit fa012f0

Please sign in to comment.