1
0
Fork 0

feat(core): New mergeOptions method and passing mergedOtions to toAbs

When using toAbs for all but the most trivial cases, one needs access to
the options. Unlike measurements which are always there, options aren't
always set since they are only set when they differ from the default.

This would shift a lot of responsibility on the pattern designer who
would have to check whether an option is set, and if it's not use the
default which in turn depends on what type of an option it is (and it's
pct for example you have to remember to divide it by 100 and so on).

This feels footgun-y so instead I've added a `mergeOptions` method that
merges the options passed by the user with the defaults from the pattern
config so that you have a simple object with all option values.

This should get passed to `toAbs()` as the 3rd parameter.
This commit is contained in:
joostdecock 2023-06-11 14:10:17 +02:00
parent 55b76d03f1
commit 631ea8a50d
5 changed files with 39 additions and 5 deletions

View file

@ -441,6 +441,29 @@ export function lineIntersectsCurve(start, end, from, cp1, cp2, to) {
else return intersections
}
/**
* Helper method to merge passed in options with default options from the pattern config
*
* @param {object} settings - The settings passed to the pattern
* @param {object} optionsConfig - The pattern's options config
* @return {object} result - An object with the merged options and their values
*/
export function mergeOptions(settings, optionsConfig) {
const merged = typeof settings.options === 'undefined' ? {} : { ...settings.option }
for (const [key, option] of Object.entries(optionsConfig)) {
if (typeof option === 'object') {
if (typeof option.pct !== 'undefined') merged[key] = option.pct / 100
else if (typeof option.mm !== 'undefined') merged[key] = option.mm
else if (typeof option.deg !== 'undefined') merged[key] = option.deg
else if (typeof option.count !== 'undefined') merged[key] = option.count
else if (typeof option.bool !== 'undefined') merged[key] = option.bool
else if (typeof option.dflt !== 'undefined') merged[key] = option.dflt
} else merged[key] = option
}
return merged
}
/**
* Helper method to calculate abolute option value based on a measurement
*