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