Skip to content

Commit a83a933

Browse files
committed
we *did* have a "simplify", toBaseUnits. Add simplify template
1 parent 15cc056 commit a83a933

2 files changed

Lines changed: 4 additions & 10 deletions

File tree

src/unchained/units.nim

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ macro toBaseUnits*[T: SomeUnit](x: T): untyped =
534534
let resType = xCT.flatten().toBaseType(true).simplify(mergePrefixes = true).toNimType
535535
result = quote do:
536536
`x`.toDef(`resType`)
537+
template simplify*[T: SomeUnit](x: T): untyped =
538+
## Alias for `toBaseUnits`. Converts the given input to base SI units
539+
toBaseUnits(x)
537540

538541
## Natural unit stuff
539542
proc toNaturalUnitImpl(t: UnitProduct): UnitProduct
@@ -594,13 +597,3 @@ macro toNaturalUnit*[T: SomeUnit](t: T): untyped =
594597
result = quote do:
595598
defUnit(`resType`)
596599
`resType`(`t`.FloatType * `scale`)
597-
598-
macro simplify*[T: SomeUnit](t: T): untyped =
599-
## Returns the simplest form in SI base units of the given input.
600-
var typ = t.parseDefinedUnit()
601-
let resType = typ.flatten().simplify()
602-
let scale = typ.toBaseTypeScale() / resType.toBaseTypeScale()
603-
let resTypeNim = resType.toNimType()
604-
result = quote do:
605-
defUnit(`resTypeNim`)
606-
`resTypeNim`(`t`.FloatType * `scale`)

tests/tunchained.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ suite "Unchained - Conversion between units":
481481
let x = 1.kJ
482482
check typeof(x.simplify()) is kg•m²•s⁻²
483483
check x.simplify() == 1000.kg•m²•s⁻²
484+
check x.toBaseUnits() == 1000.kg•m²•s⁻²
484485

485486
suite "Unchained - CT errors":
486487
test "Error on regular digit as exponent":

0 commit comments

Comments
 (0)