Skip to content

Commit

Permalink
Merge pull request #168 from Kapsonfire-DE/patch-1
Browse files Browse the repository at this point in the history
Update README.md
  • Loading branch information
gfx authored Apr 1, 2021
2 parents ecdda8f + 2732911 commit 729ad6d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,17 +361,21 @@ import { encode, decode, ExtensionCodec } from "@msgpack/msgpack";
const BIGINT_EXT_TYPE = 0; // Any in 0-127
const extensionCodec = new ExtensionCodec();
extensionCodec.register({
type: BIGINT_EXT_TYPE,
encode: (input: unknown) => {
if (typeof input === "bigint") {
return encode(input.toString());
} else {
return null;
}
},
decode: (data: Uint8Array) => {
return BigInt(decode(data));
},
type: BIGINT_EXT_TYPE,
encode: (input: unknown) => {
if (typeof input === "bigint") {
if (input <= Number.MAX_SAFE_INTEGER && input >= Number.MIN_SAFE_INTEGER) {
return encode(parseInt(input.toString(), 10));
} else {
return encode(input.toString());
}
} else {
return null;
}
},
decode: (data: Uint8Array) => {
return BigInt(decode(data));
},
});

const value = BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1);
Expand Down
14 changes: 9 additions & 5 deletions test/codec-bigint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ const extensionCodec = new ExtensionCodec();
extensionCodec.register({
type: 0,
encode: (input: unknown) => {
if (typeof input === "bigint") {
return encode(input.toString());
} else {
return null;
}
if (typeof input === "bigint") {
if (input <= Number.MAX_SAFE_INTEGER && input >= Number.MIN_SAFE_INTEGER) {
return encode(parseInt(input.toString(), 10));
} else {
return encode(input.toString());
}
} else {
return null;
}
},
decode: (data: Uint8Array) => {
return BigInt(decode(data));
Expand Down

0 comments on commit 729ad6d

Please sign in to comment.