-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GCodeProcessor: fix
ratos postprocess
with --overwrite-input
but …
…without `--idex`. (#43) * GCodeProcessor: fix ratos postprocess with --overwrite-input but without --idex. * Util: add object-manipulation.ts, notably including strictWithDefaults. * GCodeProcessor: use strictWithDefaults for options sanitization.
- Loading branch information
Showing
4 changed files
with
109 additions
and
13 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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
export type NullableRequiredToPartial<T> = Partial<{ [key in keyof T]: NonNullable<T[key]> }>; | ||
|
||
export const removeNulledProperties = <T extends object>(opts: T): NullableRequiredToPartial<T> => { | ||
const result = { ...opts }; | ||
Object.keys(opts).forEach((opt) => { | ||
if (result[opt as keyof T] === null) { | ||
delete result[opt as keyof T]; | ||
} | ||
}); | ||
return result; | ||
}; | ||
|
||
export type PartialToNullableRequired<T> = Required<{ [key in keyof T]: T[key] | null }>; | ||
|
||
/** | ||
* Typically used with `options` objects where most or all propeties are optional, returns a copy of {@link opts} which | ||
* contains only the properties of {@link T}. Properties of {@link T} that are not defined by {@link opts} will be replaced | ||
* by the default values in {@link defaults}. By design, {@link defaults} must declare all the properties of {@link T}, | ||
* using value `null` if there is no default value for a given property. Properties that are not defined by either | ||
* {@link opts} or {@link defaults} will be absent from the returned object. | ||
* | ||
* Example: | ||
* @example | ||
* const defaultOptions: PartialToNullableRequired<TransformOptions> = { | ||
* abortSignal: null, | ||
* progressTransform: null, | ||
* allowUnsupportedSlicerVersions: false, | ||
* onWarning: () => {}, | ||
* printerHasIdex: false, | ||
* quickInspectionOnly: false, | ||
* }; | ||
* strictWithDefaults(test, defaultOptions); | ||
* */ | ||
export const strictWithDefaults = <T extends object, S extends PartialToNullableRequired<T>>( | ||
opts: Partial<T>, | ||
structure: [PartialToNullableRequired<T>] extends [S] ? S : `STRUCTURE_DOES_NOT_MATCH_INPUT`, | ||
): NullableRequiredToPartial<T> => { | ||
return removeNulledProperties( | ||
Object.fromEntries( | ||
Object.keys(structure).map((k) => [k, opts[k as keyof T] ?? null]), | ||
) as NullableRequiredToPartial<T>, | ||
); | ||
}; |