-
Notifications
You must be signed in to change notification settings - Fork 48
/
Boolean.ts
102 lines (95 loc) · 2.79 KB
/
Boolean.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
import { IonTypes, Writer } from "../Ion";
import {
FromJsConstructor,
FromJsConstructorBuilder,
Primitives,
} from "./FromJsConstructor";
import { _NativeJsBoolean } from "./JsValueConversion";
import { Value } from "./Value";
const _fromJsConstructor: FromJsConstructor = new FromJsConstructorBuilder()
.withPrimitives(Primitives.Boolean)
.withClassesToUnbox(_NativeJsBoolean)
.build();
/**
* Represents a boolean[1] value in an Ion stream.
*
* Because this class extends Javascript's (big-B) Boolean data type, it is subject to the same
* surprising behavior when used for control flow.
*
* From the Mozilla Developer Network documentation[2]:
*
* > Any object of which the value is not undefined or null, including a Boolean object
* whose value is false, evaluates to true when passed to a conditional statement.
*
* ```javascript
* var b = false;
* if (b) {
* // this code will NOT be executed
* }
*
* b = new Boolean(false);
* if (b) {
* // this code WILL be executed
* }
* ```
*
* [1] https://amazon-ion.github.io/ion-docs/docs/spec.html#bool
* [2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean#Description
*/
export class Boolean extends Value(
_NativeJsBoolean,
IonTypes.BOOL,
_fromJsConstructor
) {
/**
* Constructor.
* @param value The boolean value of the new instance.
* @param annotations An optional array of strings to associate with `value`.
*/
constructor(value: boolean, annotations: string[] = []) {
super(value);
this._setAnnotations(annotations);
}
booleanValue(): boolean {
return this.valueOf() as boolean;
}
writeTo(writer: Writer): void {
writer.setAnnotations(this.getAnnotations());
writer.writeBoolean(this.booleanValue());
}
_valueEquals(
other: any,
options: {
epsilon?: number | null;
ignoreAnnotations?: boolean;
ignoreTimestampPrecision?: boolean;
onlyCompareIon?: boolean;
} = {
epsilon: null,
ignoreAnnotations: false,
ignoreTimestampPrecision: false,
onlyCompareIon: true,
}
): boolean {
let isSupportedType: boolean = false;
let valueToCompare: any = null;
// if the provided value is an ion.dom.Boolean instance.
if (other instanceof Boolean) {
isSupportedType = true;
valueToCompare = other.booleanValue();
} else if (!options.onlyCompareIon) {
// We will consider other Boolean-ish types
if (typeof other === "boolean" || other instanceof _NativeJsBoolean) {
isSupportedType = true;
valueToCompare = other.valueOf();
}
}
if (!isSupportedType) {
return false;
}
if (this.booleanValue() !== valueToCompare) {
return false;
}
return true;
}
}