-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructures.js
108 lines (82 loc) · 2.52 KB
/
Structures.js
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
import OriginalBigNumber from 'bignumber.js';
export const BigNumber = OriginalBigNumber.clone({});
BigNumber.prototype.toJSON = function () {
return `${this.toString()}${this.constructor.suffix}`;
};
export class Byte extends BigNumber {
static suffix = 'b';
static type = 'B';
constructor (n) {
super(n);
if (!(this.gte(new BigNumber('-128')) && this.lte(new BigNumber('127')))) {
throw new RangeError('Byte must be between -128 and 127');
}
}
}
export class Short extends BigNumber {
static suffix = 's';
static type = 'S';
constructor (n) {
super(n);
if (!(this.gte(new BigNumber('-32768')) && this.lte(new BigNumber('32767')))) {
throw new RangeError('Short must be between -32768 and 32767');
}
}
}
export class Int extends BigNumber {
static suffix = '';
static type = 'I';
constructor (n) {
super(n);
if (!(this.gte(new BigNumber('-2147483648')) && this.lte(new BigNumber('2147483647')))) {
throw new RangeError('Int must be between -2147483648 and 2147483647');
}
}
}
export class Long extends BigNumber {
static suffix = 'l';
static type = 'L';
constructor (n) {
super(n);
if (!(this.gte(new BigNumber('-9223372036854775808')) && this.lte(new BigNumber('9223372036854775807')))) {
throw new RangeError('Long must be between -9223372036854775808 and 9223372036854775807');
}
}
toJSON () {
return `${this.toString()}${this.constructor.suffix}`;
}
}
export class Float extends BigNumber {
static suffix = 'f';
static type = 'F';
constructor (n) {
super(n);
if (!(this.gte(new BigNumber('-3.402823e+38')) && this.lte(new BigNumber('3.402823e+38')))) {
throw new RangeError('Float must be between -3.402823e+38 and 3.402823e+38');
}
}
}
export class Double extends BigNumber {
static suffix = 'd';
static type = 'D';
constructor (n) {
super(n);
if (!(this.gte(new BigNumber('-1.7976931348623157e+308')) && this.lte(new BigNumber('1.7976931348623157e+308')))) {
throw new RangeError('Double must be between -1.7976931348623157e+308 and 1.7976931348623157e+308');
}
}
}
export class TypedArray extends Array {
constructor (baseObject, ...args) {
super(...args);
this.baseObject = baseObject;
}
push (...args) {
for (let i = 0; i < args.length; i++) {
if (!(args[i] instanceof this.baseObject)) {
throw new TypeError(`Expected '${this.baseObject.name}' instead of '${args[i].constructor.name}'`);
}
}
return super.push(...args);
}
}