@@ -35,15 +35,41 @@ extension Double: ConstructibleFromJSValue {}
35
35
extension Float : ConstructibleFromJSValue { }
36
36
37
37
extension SignedInteger where Self: ConstructibleFromJSValue {
38
+ /// Construct an instance of `SignedInteger` from the given `JSBigIntExtended`.
39
+ ///
40
+ /// If the value is too large to fit in the `Self` type, `nil` is returned.
41
+ ///
42
+ /// - Parameter bigInt: The `JSBigIntExtended` to decode
43
+ public init ? ( exactly bigInt: JSBigIntExtended ) {
44
+ self . init ( exactly: bigInt. int64Value)
45
+ }
46
+
47
+ /// Construct an instance of `SignedInteger` from the given `JSBigIntExtended`.
48
+ ///
49
+ /// Crash if the value is too large to fit in the `Self` type.
50
+ ///
51
+ /// - Parameter bigInt: The `JSBigIntExtended` to decode
38
52
public init ( _ bigInt: JSBigIntExtended ) {
39
53
self . init ( bigInt. int64Value)
40
54
}
55
+
56
+ /// Construct an instance of `SignedInteger` from the given `JSValue`.
57
+ ///
58
+ /// Returns `nil` if one of the following conditions is met:
59
+ /// - The value is not a number or a bigint.
60
+ /// - The value is a number that does not fit or cannot be represented
61
+ /// in the `Self` type (e.g. NaN, Infinity).
62
+ /// - The value is a bigint that does not fit in the `Self` type.
63
+ ///
64
+ /// If the value is a number, it is rounded towards zero before conversion.
65
+ ///
66
+ /// - Parameter value: The `JSValue` to decode
41
67
public static func construct( from value: JSValue ) -> Self ? {
42
68
if let number = value. number {
43
- return Self ( number)
69
+ return Self ( exactly : number. rounded ( . towardZero ) )
44
70
}
45
71
if let bigInt = value. bigInt as? JSBigIntExtended {
46
- return Self ( bigInt)
72
+ return Self ( exactly : bigInt)
47
73
}
48
74
return nil
49
75
}
@@ -55,15 +81,41 @@ extension Int32: ConstructibleFromJSValue {}
55
81
extension Int64 : ConstructibleFromJSValue { }
56
82
57
83
extension UnsignedInteger where Self: ConstructibleFromJSValue {
84
+
85
+ /// Construct an instance of `UnsignedInteger` from the given `JSBigIntExtended`.
86
+ ///
87
+ /// Returns `nil` if the value is negative or too large to fit in the `Self` type.
88
+ ///
89
+ /// - Parameter bigInt: The `JSBigIntExtended` to decode
90
+ public init ? ( exactly bigInt: JSBigIntExtended ) {
91
+ self . init ( exactly: bigInt. uInt64Value)
92
+ }
93
+
94
+ /// Construct an instance of `UnsignedInteger` from the given `JSBigIntExtended`.
95
+ ///
96
+ /// Crash if the value is negative or too large to fit in the `Self` type.
97
+ ///
98
+ /// - Parameter bigInt: The `JSBigIntExtended` to decode
58
99
public init ( _ bigInt: JSBigIntExtended ) {
59
100
self . init ( bigInt. uInt64Value)
60
101
}
102
+
103
+ /// Construct an instance of `UnsignedInteger` from the given `JSValue`.
104
+ ///
105
+ /// Returns `nil` if one of the following conditions is met:
106
+ /// - The value is not a number or a bigint.
107
+ /// - The value is a number that does not fit or cannot be represented
108
+ /// in the `Self` type (e.g. NaN, Infinity).
109
+ /// - The value is a bigint that does not fit in the `Self` type.
110
+ /// - The value is negative.
111
+ ///
112
+ /// - Parameter value: The `JSValue` to decode
61
113
public static func construct( from value: JSValue ) -> Self ? {
62
114
if let number = value. number {
63
- return Self ( number)
115
+ return Self ( exactly : number. rounded ( . towardZero ) )
64
116
}
65
117
if let bigInt = value. bigInt as? JSBigIntExtended {
66
- return Self ( bigInt)
118
+ return Self ( exactly : bigInt)
67
119
}
68
120
return nil
69
121
}
0 commit comments