Skip to content

Commit 47ae1d4

Browse files
committed
add BigInt support in JavaScriptCodec
1 parent 0bfb144 commit 47ae1d4

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/JavaScriptCodec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const enum JSData {
99
Set,
1010
Date,
1111
RegExp,
12+
BigInt,
1213
}
1314

1415
export function encodeJavaScriptData(input: unknown): Uint8Array | null {
@@ -22,6 +23,8 @@ export function encodeJavaScriptData(input: unknown): Uint8Array | null {
2223
return encode([JSData.Date, input.getTime()]);
2324
} else if (input instanceof RegExp) {
2425
return encode([JSData.RegExp, [input.source, input.flags]]);
26+
} else if (typeof input === "bigint") {
27+
return encode([JSData.BigInt, input.toString()]);
2528
} else {
2629
return null;
2730
}
@@ -44,6 +47,9 @@ export function decodeJavaScriptData(data: Uint8Array) {
4447
const [pattern, flags] = source;
4548
return new RegExp(pattern, flags);
4649
}
50+
case JSData.BigInt: {
51+
return BigInt(source);
52+
}
4753
default: {
4854
throw new Error(`Unknown data type: ${jsDataType}`);
4955
}

test/javascript-codec.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ describe("JavaScriptCodec", () => {
1414
arr: [1, 2, 3],
1515
bool: true,
1616
nil: null,
17-
// undef: undefined,
17+
// undef: undefined, // not supported
1818
date: new Date("Thu, 28 Apr 2016 22:02:17 GMT"),
1919
map: new Map([["foo", 10], ["bar", 20]]),
2020
set: new Set([123, 456]),
2121
regexp: /foo\n/i,
22+
bigint: typeof(BigInt) !== "undefined" ? BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1) : null,
2223
};
2324
const encoded = encode(object, { extensionCodec: JavaScriptCodec });
2425

0 commit comments

Comments
 (0)