Skip to content

Commit a03e611

Browse files
authored
Fixed #507 (#541)
1 parent fbdbc0e commit a03e611

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

src/core/format/RawUInt64.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class RawUInt64 {
3131
* @returns {number|module:coders/uint64~uint64}
3232
* A numeric if the uint64 is no greater than Number.MAX_SAFE_INTEGER or the original uint64 value otherwise.
3333
*/
34-
public static compact = (uint64): any => {
34+
public static compact = (uint64: number[]): number | number[] => {
3535
const low = uint64[0];
3636
const high = uint64[1];
3737

src/model/UInt64.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ export class UInt64 {
124124
* @returns {number}
125125
*/
126126
public compact(): number {
127-
return uint64.compact(this.toDTO());
127+
const result = uint64.compact(this.toDTO());
128+
if (Array.isArray(result)) {
129+
throw new Error('Compacted value is greater than Number.Max_Value.');
130+
}
131+
return result;
128132
}
129133

130134
/**

test/model/UInt64.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ describe('Uint64', () => {
6666
expect(uint64Compact).to.be.equal(51110867862);
6767
});
6868

69+
it('should compact UInt64 number', () => {
70+
expect(() => {
71+
new UInt64([3866227606, 0x00200000 + 1]).compact();
72+
}).to.throw(Error, 'Compacted value is greater than Number.Max_Value.');
73+
});
74+
6975
it('should fromUint throw exception with negative uint value', () => {
7076
expect(() => {
7177
UInt64.fromUint(-1);

0 commit comments

Comments
 (0)