-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathdouble.ts
115 lines (100 loc) · 3.42 KB
/
double.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
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import type { EJSONOptions } from './extended_json';
import { type InspectFn, defaultInspect } from './parser/utils';
/** @public */
export interface DoubleExtended {
$numberDouble: string;
}
/**
* A class representation of the BSON Double type.
* @public
* @category BSONType
*/
export class Double extends BSONValue {
get _bsontype(): 'Double' {
return 'Double';
}
value!: number;
/**
* Create a Double type
*
* @param value - the number we want to represent as a double.
*/
constructor(value: number) {
super();
if ((value as unknown) instanceof Number) {
value = value.valueOf();
}
this.value = +value;
}
/**
* Attempt to create an double type from string.
*
* This method will throw a BSONError on any string input that is not representable as a IEEE-754 64-bit double.
* Notably, this method will also throw on the following string formats:
* - Strings in non-decimal and non-exponential formats (binary, hex, or octal digits)
* - Strings with characters other than numeric, floating point, or leading sign characters (Note: 'Infinity', '-Infinity', and 'NaN' input strings are still allowed)
* - Strings with leading and/or trailing whitespace
*
* Strings with leading zeros, however, are also allowed
*
* @param value - the string we want to represent as a double.
*/
static fromString(value: string): Double {
const coercedValue = Number(value);
if (value === 'NaN') return new Double(NaN);
if (value === 'Infinity') return new Double(Infinity);
if (value === '-Infinity') return new Double(-Infinity);
if (!Number.isFinite(coercedValue)) {
throw new BSONError(`Input: ${value} is not representable as a Double`);
}
if (value.trim() !== value) {
throw new BSONError(`Input: '${value}' contains whitespace`);
}
if (value === '') {
throw new BSONError(`Input is an empty string`);
}
if (/[^-0-9.+eE]/.test(value)) {
throw new BSONError(`Input: '${value}' is not in decimal or exponential notation`);
}
return new Double(coercedValue);
}
/**
* Access the number value.
*
* @returns returns the wrapped double number.
*/
valueOf(): number {
return this.value;
}
toJSON(): number {
return this.value;
}
toString(radix?: number): string {
return this.value.toString(radix);
}
/** @internal */
toExtendedJSON(options?: EJSONOptions): number | DoubleExtended {
if (options && (options.legacy || (options.relaxed && isFinite(this.value)))) {
return this.value;
}
if (Object.is(Math.sign(this.value), -0)) {
// NOTE: JavaScript has +0 and -0, apparently to model limit calculations. If a user
// explicitly provided `-0` then we need to ensure the sign makes it into the output
return { $numberDouble: '-0.0' };
}
return {
$numberDouble: Number.isInteger(this.value) ? this.value.toFixed(1) : this.value.toString()
};
}
/** @internal */
static fromExtendedJSON(doc: DoubleExtended, options?: EJSONOptions): number | Double {
const doubleValue = parseFloat(doc.$numberDouble);
return options && options.relaxed ? doubleValue : new Double(doubleValue);
}
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
inspect ??= defaultInspect;
return `new Double(${inspect(this.value, options)})`;
}
}