-
Notifications
You must be signed in to change notification settings - Fork 5
/
UnitDimensions.ts
122 lines (114 loc) · 2.14 KB
/
UnitDimensions.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (c) 2020, J2 Innovations. All Rights Reserved
*/
/**
* The unit dimension data.
*/
export interface UnitDimensionsData {
kg: number
m: number
sec: number
K: number
A: number
mol: number
cd: number
}
/**
* A unit's dimension.
*
* https://fantom.org/doc/sys/Unit
*/
export class UnitDimensions {
readonly kg: number
readonly m: number
readonly sec: number
readonly K: number
readonly A: number
readonly mol: number
readonly cd: number
public constructor({
kg,
m,
sec,
K,
A,
mol,
cd,
}: Partial<UnitDimensionsData>) {
this.kg = kg ?? 0
this.m = m ?? 0
this.sec = sec ?? 0
this.K = K ?? 0
this.A = A ?? 0
this.mol = mol ?? 0
this.cd = cd ?? 0
}
/**
* Add this unit to another one and return the result.
*
* @param dim The dimension to add to this one.
* @returns The new dimension.
*/
public add(dim: UnitDimensions): UnitDimensions {
return new UnitDimensions({
kg: this.kg + dim.kg,
m: this.m + dim.m,
sec: this.sec + dim.sec,
K: this.K + dim.K,
A: this.A + dim.A,
mol: this.mol + dim.mol,
cd: this.cd + dim.cd,
})
}
/**
* Subtract the units from one another and return the result.
*
* @param dim The dimension to subtract from this one.
* @returns The new dimension.
*/
public subtract(dim: UnitDimensions): UnitDimensions {
return new UnitDimensions({
kg: this.kg - dim.kg,
m: this.m - dim.m,
sec: this.sec - dim.sec,
K: this.K - dim.K,
A: this.A - dim.A,
mol: this.mol - dim.mol,
cd: this.cd - dim.cd,
})
}
/**
* Dimension equality.
*
* @param dim The dimension to test for equality.
* @returns True if the dimensions match.
*/
public equals(dim?: UnitDimensions): boolean {
if (!dim) {
return false
}
return (
this.kg === dim.kg &&
this.m === dim.m &&
this.sec === dim.sec &&
this.K === dim.K &&
this.A === dim.A &&
this.mol === dim.mol &&
this.cd === dim.cd
)
}
/**
* @returns A JSON representation of the dimension.
*/
public toJSON(): UnitDimensionsData {
return {
kg: this.kg,
m: this.m,
sec: this.sec,
K: this.K,
A: this.A,
mol: this.mol,
cd: this.cd,
}
}
}