Skip to content

Commit f66a494

Browse files
committed
use typeName
1 parent 28d40be commit f66a494

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

packages/ripple-binary-codec/src/types/int-32.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ class Int32 extends Int {
4141
const buf = new Uint8Array(Int32.width)
4242

4343
if (typeof val === 'string') {
44-
const num = Number.parseInt(val, 10)
45-
if (!Number.isInteger(num)) {
44+
const num = Number(val)
45+
if (!Number.isFinite(num) || !Number.isInteger(num)) {
4646
throw new Error(`Cannot construct Int32 from string: ${val}`)
4747
}
48-
Int32.checkIntRange(num, Int32.MIN_VALUE, Int32.MAX_VALUE)
48+
Int32.checkIntRange('Int32', num, Int32.MIN_VALUE, Int32.MAX_VALUE)
4949
writeInt32BE(buf, num, 0)
5050
return new Int32(buf)
5151
}
5252

5353
if (typeof val === 'number' && Number.isInteger(val)) {
54-
Int32.checkIntRange(val, Int32.MIN_VALUE, Int32.MAX_VALUE)
54+
Int32.checkIntRange('Int32', val, Int32.MIN_VALUE, Int32.MAX_VALUE)
5555
writeInt32BE(buf, val, 0)
5656
return new Int32(buf)
5757
}

packages/ripple-binary-codec/src/types/int.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,21 @@ abstract class Int extends Comparable<Int | number> {
5151
/**
5252
* Validate that a number is within the specified signed integer range
5353
*
54+
* @param typeName The name of the type (for error messages)
5455
* @param val The number to validate
5556
* @param min The minimum allowed value
5657
* @param max The maximum allowed value
5758
* @throws Error if the value is out of range
5859
*/
5960
static checkIntRange(
61+
typeName: string,
6062
val: number | bigint,
6163
min: number | bigint,
6264
max: number | bigint,
6365
): void {
6466
if (val < min || val > max) {
6567
throw new Error(
66-
`Invalid ${this.name}: ${val} must be >= ${min} and <= ${max}`,
68+
`Invalid ${typeName}: ${val} must be >= ${min} and <= ${max}`,
6769
)
6870
}
6971
}

packages/ripple-binary-codec/test/int.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ describe('Int32', () => {
9898
)
9999
})
100100

101+
it('should throw error for decimal string', () => {
102+
expect(() => Int32.from('1.23')).toThrow(
103+
new Error('Cannot construct Int32 from string: 1.23'),
104+
)
105+
})
106+
101107
it('should throw error for non-numeric string', () => {
102108
expect(() => Int32.from('abc')).toThrow(
103109
new Error('Cannot construct Int32 from string: abc'),

0 commit comments

Comments
 (0)