-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ternary.ts
51 lines (45 loc) · 1.12 KB
/
Ternary.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
import {
Scalar,
ScalarLike,
ScalarOf
} from '@main';
/**
* Ternary operation.
*/
export class Ternary<T> implements Scalar<T> {
/**
* Test condition.
*/
private readonly condition: Scalar<boolean>;
/**
* Return when condition is true.
*/
private readonly consequent: Scalar<T>;
/**
* Return when condition is false.
*/
private readonly alternative: Scalar<T>;
/**
* Ctor.
* @param condition Test condition.
* @param consequent Return when condition is true.
* @param alternative Return when condition is false.
*/
constructor(condition: ScalarLike<boolean>, consequent: ScalarLike<T>, alternative: ScalarLike<T>) {
this.condition = new ScalarOf(condition);
this.consequent = new ScalarOf(consequent);
this.alternative = new ScalarOf(alternative);
}
/**
* Type determinant.
*/
public isScalar(): true {
return true;
}
/**
* Gets the value.
*/
public value(): T {
return this.condition.value() ? this.consequent.value() : this.alternative.value();
}
}