Skip to content

Commit 2ee2065

Browse files
authored
Update README.md
Possible to save size in BigInt Handling, when using Number if its in SAFE range
1 parent ecdda8f commit 2ee2065

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -361,17 +361,21 @@ import { encode, decode, ExtensionCodec } from "@msgpack/msgpack";
361361
const BIGINT_EXT_TYPE = 0; // Any in 0-127
362362
const extensionCodec = new ExtensionCodec();
363363
extensionCodec.register({
364-
type: BIGINT_EXT_TYPE,
365-
encode: (input: unknown) => {
366-
if (typeof input === "bigint") {
367-
return encode(input.toString());
368-
} else {
369-
return null;
370-
}
371-
},
372-
decode: (data: Uint8Array) => {
373-
return BigInt(decode(data));
374-
},
364+
type: BIGINT_EXT_TYPE,
365+
encode: (input: unknown) => {
366+
if (typeof input === "bigint") {
367+
if (input <= Number.MAX_SAFE_INTEGER && input >= Number.MIN_SAFE_INTEGER) {
368+
return encode(parseInt(input.toString(), 10));
369+
} else {
370+
return encode(input.toString());
371+
}
372+
} else {
373+
return null;
374+
}
375+
},
376+
decode: (data: Uint8Array) => {
377+
return BigInt(decode(data));
378+
},
375379
});
376380

377381
const value = BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1);

0 commit comments

Comments
 (0)