I am developing an application that needs to programmatically determine the complete state of a DC motor, given a limited number of details about the state. For example:
{
voltage: Measurement(12, "V"),
rpm: Measurement(5000, "rpm"),
currentDraw: undefined,
torque: undefined
}
This object would be passed to a function that calculates the values of currentDraw
and torque
according to the defined properties of the object. However, it is not always voltage
and rpm
provided - it may be any combination of properties, and the ones listed above are not the only ones available (simplified for the question).
I would like to avoid this logic:
constructor(state) {
if (state.voltage) {
if (state.rpm) {
state.torque = ... ;
state.currentDraw = ... ;
} else if (state.torque) { ... }
...
}
...
}
As it's not clean and difficult to read. Plus with a large number of possible variables, the function would get very large very quickly.
Has anyone solved a similar problem before? All advice welcome.