NPellet / visualizer

Other
43 stars 24 forks source link

Add utility methods to convert unit to value #1114

Open lpatiny opened 5 years ago

lpatiny commented 5 years ago

Internally we try to store the values in SI but sometimes we need in a code executor the value in the unit specified by the user.

We should add a utility function like:

function getValue(value, options={}) {
    const {
        customUnit
    } = options;
    let unit = mathjs.unit(String(value.unit));
    unit.value = Number(value.SI);
    let number;
    if (customUnit) {
      unit = unit.to(customUnit);
      number = unit.toNumber(options.unit);
    } else {
      const unitStr = String(value.unit);
      number = unit.toNumber(unitStr);
    }
    return number;
}

@stropitek an idea where to place it ?

stropitek commented 5 years ago

I think the type renderer is already using a similar logic so it should be shared in a utility library for mathjs in the visualizer