-
Notifications
You must be signed in to change notification settings - Fork 0
/
relativeUnit.ts
32 lines (24 loc) · 1.41 KB
/
relativeUnit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Unit from './unit'
import { UnitNameConfig } from './nameConstruct'
import { UnitShapeMap } from './unitShape'
export class RelativeUnit<ThisUnitShapeMap extends UnitShapeMap> extends Unit<ThisUnitShapeMap> {
relativeTo: Unit<ThisUnitShapeMap>
relativeUnitPerThisUnit: number
constructor(relativeTo:Unit<ThisUnitShapeMap>, relativeUnitPerThisUnit:number, nameConfig: UnitNameConfig){
super(relativeTo.shape, relativeTo.mathConfig, nameConfig)
this.relativeUnitPerThisUnit = relativeUnitPerThisUnit
this.relativeTo = relativeTo
}
fromBaseSI(quantityInBaseSI: number): number {
return this.relativeTo.fromBaseSI(quantityInBaseSI)/this.relativeUnitPerThisUnit
}
toBaseSI(quantityInThisUnit: number): number {
return this.relativeTo.toBaseSI(quantityInThisUnit*this.relativeUnitPerThisUnit)
}
static MultipleOf<ThisUnitShapeMap extends UnitShapeMap>(relativeTo:Unit<ThisUnitShapeMap>, relativeUnitPerThisUnit:number, nameConfig: UnitNameConfig){
return new RelativeUnit<ThisUnitShapeMap>(relativeTo, relativeUnitPerThisUnit, nameConfig)
}
static FractionOf<ThisUnitShapeMap extends UnitShapeMap>(relativeTo:Unit<ThisUnitShapeMap>, thisUnitPerRelativeUnit: number, nameConfig: UnitNameConfig): RelativeUnit<ThisUnitShapeMap> {
return new RelativeUnit<ThisUnitShapeMap>(relativeTo, 1/thisUnitPerRelativeUnit, nameConfig)
}
}