Skip to content

Commit

Permalink
feat: return a number if it is valid when decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Nesopie committed Aug 11, 2024
1 parent d9178e2 commit 95ff37a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function decode(buffer, offset) {
}
else {
const number = tools.readUInt64(buffer, offset + 1, 'LE');
return { numberValue: null, bigintValue: number, bytes: 9 };
return { numberValue: number <= Number.MAX_SAFE_INTEGER ? Number(number) : null, bigintValue: number, bytes: 9 };
}
}
function encodingLength(n) {
Expand Down
2 changes: 1 addition & 1 deletion src/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function decode(buffer, offset) {
}
else {
const number = tools.readUInt64(buffer, offset + 1, 'LE');
return { numberValue: null, bigintValue: number, bytes: 9 };
return { numberValue: number <= Number.MAX_SAFE_INTEGER ? Number(number) : null, bigintValue: number, bytes: 9 };
}
}
export function encodingLength(n) {
Expand Down
24 changes: 23 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ valid.forEach(function (fixture, i) {

tape("valid decode #" + (i + 1), function (t) {
const res = decode(Buffer.from(fixture.hex, "hex"));
if(fixture.dec <= 0xffffffff) {
if(fixture.dec <= Number.MAX_SAFE_INTEGER) {
t.same(res.numberValue, fixture.dec);
}else {
t.same(res.numberValue, null);
Expand Down Expand Up @@ -91,5 +91,27 @@ tape("decode", function (t) {
t.end();
});

t.test("should return a number if it is valid", function (t) {
var buffer = Buffer.alloc(18);
buffer.writeUIntBE(0xff, 0, 1);
buffer.writeBigUint64LE(BigInt(Number.MAX_SAFE_INTEGER), 1);
const res = decode(buffer, 0);
t.same(res.numberValue, Number.MAX_SAFE_INTEGER);
t.same(res.bigintValue, BigInt(Number.MAX_SAFE_INTEGER));
t.same(res.bytes, 9);
t.end();
});

t.test("should return null if a number is invalid", function (t) {
var buffer = Buffer.alloc(18);
buffer.writeUIntBE(0xff, 0, 1);
buffer.writeBigUint64LE(BigInt(Number.MAX_SAFE_INTEGER) + 1n, 1);
const res = decode(buffer, 0);
t.same(res.numberValue, null);
t.same(res.bigintValue, BigInt(Number.MAX_SAFE_INTEGER) + 1n);
t.same(res.bytes, 9);
t.end();
});

t.end();
});
2 changes: 1 addition & 1 deletion ts_src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function decode (
} else {
const number = tools.readUInt64(buffer, offset + 1, 'LE')

return { numberValue: null, bigintValue: number, bytes: 9 }
return { numberValue: number <= Number.MAX_SAFE_INTEGER ? Number(number) : null, bigintValue: number, bytes: 9 }
}
}

Expand Down

0 comments on commit 95ff37a

Please sign in to comment.