2020-04-23 09:30:03 +02:00
|
|
|
import { mapTemporal } from './temporal'
|
2020-05-08 12:04:00 +02:00
|
|
|
import { convertUnit, simplifyUnit } from './units'
|
|
|
|
import { EvaluatedNode, Unit } from './types'
|
2019-11-28 12:03:23 +01:00
|
|
|
|
|
|
|
export function simplifyNodeUnit(node) {
|
2020-04-23 09:30:03 +02:00
|
|
|
if (!node.unit) {
|
2019-11-28 12:03:23 +01:00
|
|
|
return node
|
|
|
|
}
|
2020-04-23 09:30:03 +02:00
|
|
|
const unit = simplifyUnit(node.unit)
|
2020-03-16 18:09:41 +01:00
|
|
|
|
2020-04-23 09:30:03 +02:00
|
|
|
return convertNodeToUnit(unit, node)
|
2019-11-28 12:03:23 +01:00
|
|
|
}
|
|
|
|
|
2020-04-23 09:30:03 +02:00
|
|
|
export function convertNodeToUnit<Names extends string>(
|
|
|
|
to: Unit,
|
|
|
|
node: EvaluatedNode<Names, number>
|
|
|
|
) {
|
|
|
|
const temporalValue =
|
|
|
|
node.temporalValue && node.unit
|
|
|
|
? mapTemporal(
|
|
|
|
value => convertUnit(node.unit, to, value),
|
|
|
|
node.temporalValue
|
|
|
|
)
|
|
|
|
: node.temporalValue
|
2019-11-28 12:03:23 +01:00
|
|
|
return {
|
|
|
|
...node,
|
|
|
|
nodeValue: node.unit
|
|
|
|
? convertUnit(node.unit, to, node.nodeValue)
|
|
|
|
: node.nodeValue,
|
2020-04-23 09:30:03 +02:00
|
|
|
...(temporalValue && { temporalValue }),
|
2019-11-28 12:03:23 +01:00
|
|
|
unit: to
|
|
|
|
}
|
|
|
|
}
|