Skip to content

Commit 059ce3b

Browse files
committed
add tests for toType
add error if output number is greater than MAX_SAFE_INTEGER
1 parent 243fd47 commit 059ce3b

File tree

2 files changed

+121
-2
lines changed

2 files changed

+121
-2
lines changed

src/types.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export enum TypeOutput {
7575
}
7676

7777
export type TypeOutputReturnType = {
78-
[TypeOutput.Number]: Number
78+
[TypeOutput.Number]: number
7979
[TypeOutput.BN]: BN
8080
[TypeOutput.Buffer]: Buffer
8181
[TypeOutput.PrefixedHexString]: PrefixedHexString
@@ -105,7 +105,14 @@ export function toType<T extends TypeOutput>(
105105
} else if (outputType === TypeOutput.BN) {
106106
return new BN(input) as any
107107
} else if (outputType === TypeOutput.Number) {
108-
return new BN(input).toNumber() as any
108+
const bn = new BN(input)
109+
const max = new BN(Number.MAX_SAFE_INTEGER.toString())
110+
if (bn.gt(max)) {
111+
throw new Error(
112+
'The provided number is greater than MAX_SAFE_INTEGER (please use an alternative output type)'
113+
)
114+
}
115+
return bn.toNumber() as any
109116
} else {
110117
// outputType === TypeOutput.PrefixedHexString
111118
return `0x${input.toString('hex')}` as any

test/types.spec.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import assert from 'assert'
2+
import BN from 'bn.js'
3+
import { toType, TypeOutput, intToBuffer, bufferToHex, intToHex, bnToHex, toBuffer } from '../src'
4+
5+
describe('toType', function() {
6+
describe('from Number', function() {
7+
const num = 1000
8+
it('should convert to Number', function() {
9+
const result = toType(num, TypeOutput.Number)
10+
assert.strictEqual(result, num)
11+
})
12+
it('should convert to BN', function() {
13+
const result = toType(num, TypeOutput.BN)
14+
assert.ok(result.eq(new BN(num)))
15+
})
16+
it('should convert to Buffer', function() {
17+
const result = toType(num, TypeOutput.Buffer)
18+
assert.ok(result.equals(intToBuffer(num)))
19+
})
20+
it('should convert to PrefixedHexString', function() {
21+
const result = toType(num, TypeOutput.PrefixedHexString)
22+
assert.strictEqual(result, bufferToHex(new BN(num).toArrayLike(Buffer)))
23+
})
24+
it('should throw an error if greater than MAX_SAFE_INTEGER', function() {
25+
assert.throws(
26+
() => {
27+
const num = Number.MAX_SAFE_INTEGER + 1
28+
toType(num, TypeOutput.BN)
29+
},
30+
{
31+
message:
32+
'The provided number is greater than MAX_SAFE_INTEGER (please use an alternative input type)'
33+
}
34+
)
35+
})
36+
})
37+
describe('from BN', function() {
38+
const num = new BN(1000)
39+
it('should convert to Number', function() {
40+
const result = toType(num, TypeOutput.Number)
41+
assert.strictEqual(result, num.toNumber())
42+
})
43+
it('should convert to BN', function() {
44+
const result = toType(num, TypeOutput.BN)
45+
assert.ok(result.eq(num))
46+
})
47+
it('should convert to Buffer', function() {
48+
const result = toType(num, TypeOutput.Buffer)
49+
assert.ok(result.equals(num.toArrayLike(Buffer)))
50+
})
51+
it('should convert to PrefixedHexString', function() {
52+
const result = toType(num, TypeOutput.PrefixedHexString)
53+
assert.strictEqual(result, bufferToHex(num.toArrayLike(Buffer)))
54+
})
55+
it('should throw an error if converting to Number and greater than MAX_SAFE_INTEGER', function() {
56+
const num = new BN(Number.MAX_SAFE_INTEGER).addn(1)
57+
assert.throws(
58+
() => {
59+
toType(num, TypeOutput.Number)
60+
},
61+
{
62+
message:
63+
'The provided number is greater than MAX_SAFE_INTEGER (please use an alternative output type)'
64+
}
65+
)
66+
})
67+
})
68+
describe('from Buffer', function() {
69+
const num = intToBuffer(1000)
70+
it('should convert to Number', function() {
71+
const result = toType(num, TypeOutput.Number)
72+
assert.ok(intToBuffer(result).equals(num))
73+
})
74+
it('should convert to BN', function() {
75+
const result = toType(num, TypeOutput.BN)
76+
assert.ok(result.eq(new BN(num)))
77+
})
78+
it('should convert to Buffer', function() {
79+
const result = toType(num, TypeOutput.Buffer)
80+
assert.ok(result.equals(num))
81+
})
82+
it('should convert to PrefixedHexString', function() {
83+
const result = toType(num, TypeOutput.PrefixedHexString)
84+
assert.strictEqual(result, bufferToHex(num))
85+
})
86+
})
87+
describe('from HexPrefixedString', function() {
88+
const num = intToHex(1000)
89+
it('should convert to Number', function() {
90+
const result = toType(num, TypeOutput.Number)
91+
assert.strictEqual(intToHex(result), num)
92+
})
93+
it('should convert to BN', function() {
94+
const result = toType(num, TypeOutput.BN)
95+
assert.strictEqual(bnToHex(result), num)
96+
})
97+
it('should convert to Buffer', function() {
98+
const result = toType(num, TypeOutput.Buffer)
99+
assert.ok(result.equals(toBuffer(num)))
100+
})
101+
it('should throw an error if is not 0x-prefixed', function() {
102+
assert.throws(
103+
() => {
104+
toType('1', TypeOutput.Number)
105+
},
106+
{
107+
message: 'A string must be provided with a 0x-prefix, given: 1'
108+
}
109+
)
110+
})
111+
})
112+
})

0 commit comments

Comments
 (0)