mon-entreprise/publicodes/source/nodeUnits.ts

34 lines
768 B
TypeScript
Raw Normal View History

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