-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
unit.ts
57 lines (43 loc) · 1.06 KB
/
unit.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
namespace $ {
export class $mol_unit extends $mol_object {
'valueOf()' : number
constructor( value? : number ) {
super()
if( value !== undefined ) this[ 'valueOf()' ] = value
}
prefix() {
return ''
}
postfix() {
return ''
}
[ Symbol.toPrimitive ]( hint: 'number' | 'string' | 'default' ) {
switch( hint ) {
case 'number': return this.valueOf()
case 'string': return this.toString()
default: return this.toString()
}
}
valueOf() {
return this[ 'valueOf()' ]
}
delimiter() {
return ' '
}
value_view() {
return this.valueOf().toLocaleString()
}
toString() {
return this.prefix() + this.value_view() + this.postfix()
}
static summ( a : $mol_unit , b : $mol_unit ) {
var Class = a.constructor as any
if( Class !== b.constructor ) throw new Error( `Not same measure: ${Class} , ${b.constructor}` )
return new Class( a.valueOf() + b.valueOf() )
}
mult( m : number ) : this {
var Class = this.constructor as any
return new Class( this.valueOf() * m )
}
}
}