Skip to content

Commit

Permalink
fix: BigInt not always available (#407)
Browse files Browse the repository at this point in the history
Selectively add BigInt for supported platforms.
  • Loading branch information
pubkey authored Dec 15, 2023
1 parent fe4a3da commit ec817f3
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ const STRING_CONVERTERS = new Map<AnyVal, Callback<string>>([
[RegExp, toString],
[Function, toString],
[Symbol, toString],
[BigInt, (n: bigint) => "0x" + n.toString(16)],
[Date, (d: Date) => d.toISOString()],
[String, JSON.stringify],
[Null, (_: AnyVal) => "null"],
Expand All @@ -100,10 +99,22 @@ const STRING_CONVERTERS = new Map<AnyVal, Callback<string>>([
[Int32Array, typedArrayToString],
[Uint32Array, typedArrayToString],
[Float32Array, typedArrayToString],
[Float64Array, typedArrayToString],
[BigInt64Array, typedArrayToString],
[BigUint64Array, typedArrayToString]
[Float64Array, typedArrayToString]
]);
/**
* Some types like BigInt are not available on more exotic
* JavaScript runtimes like ReactNative or QuickJS.
* So we fill them in only if they exist so that it does not throw an error.
*/
if (typeof BigInt !== "undefined") {
STRING_CONVERTERS.set(BigInt, (n: bigint) => "0x" + n.toString(16));
}
if (typeof BigInt64Array !== "undefined") {
STRING_CONVERTERS.set(BigInt64Array, typedArrayToString);
}
if (typeof BigUint64Array !== "undefined") {
STRING_CONVERTERS.set(BigUint64Array, typedArrayToString);
}

/** MongoDB sort comparison order. https://www.mongodb.com/docs/manual/reference/bson-type-comparison-order */
const SORT_ORDER_BY_TYPE: Record<JsType, number> = {
Expand Down

0 comments on commit ec817f3

Please sign in to comment.