You can pass arguments to transformations.
(function(data, lowUnit, highUnit, threshold) {
let rval = Quantity(data).toUnit(lowUnit);
if(rval.greaterThan(threshold)) rval = rval.toUnit(highUnit);
return rval.toString().replace(" ", ""); // remove the space between the number and the unit
})(input, lowUnit, highUnit, threshold)
called using
JS(config:js:hasThousandToK?lowUnit="W"&highUnit="kW"&threshold="1000 W"):%s
It doesn’t get more generic than that. So long as you pass in compatible units that will work for any set of units.
Also note, it doesn’t matter what unit data
is when it is passed into the transformation. It’s converted to W
in the original and lowUnit
in this update if it’s something else.
Edit: typo in the transformation, rval shouldn’t be a const since we reassign it.
I’ve posted a version of this without the removal of the space between the value and the unit to the marketplace.
You should be able to install it through Add-on Store → Transformations → Scale Units.