Skip to content

Commit 432da7c

Browse files
committed
Add static getter for zero value Int
1 parent cb0162a commit 432da7c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/chain/integer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ export class Int implements ABISerializableObject {
5050
return this.isSigned ? this.max.ineg().isubn(1) : new BN(0)
5151
}
5252

53+
/** Return a zero value of this type */
54+
static get zero() {
55+
return this.from(0)
56+
}
57+
5358
/** Add `lhs` to `rhs` and return the resulting value. */
5459
static add(lhs: Int, rhs: Int, overflow: OverflowBehavior = 'truncate'): Int {
5560
return Int.operator(lhs, rhs, overflow, (a, b) => a.add(b))

test/integer.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ suite('integer', function () {
177177
assert.ok(Number(bigValue))
178178
}, /Number can only safely store up to 53 bits/)
179179
})
180+
181+
test('zero', function () {
182+
assert.isTrue(Int8.from(0).equals(Int8.zero))
183+
assert.isTrue(Int16.from(0).equals(Int16.zero))
184+
assert.isTrue(Int32.from(0).equals(Int32.zero))
185+
assert.isTrue(Int64.from(0).equals(Int64.zero))
186+
assert.isTrue(Int128.from(0).equals(Int128.zero))
187+
assert.isTrue(UInt8.from(0).equals(UInt8.zero))
188+
assert.isTrue(UInt16.from(0).equals(UInt16.zero))
189+
assert.isTrue(UInt32.from(0).equals(UInt32.zero))
190+
assert.isTrue(UInt64.from(0).equals(UInt64.zero))
191+
assert.isTrue(UInt128.from(0).equals(UInt128.zero))
192+
})
180193
})
181194

182195
function assertInt(actual: Int, expected: number | string) {

0 commit comments

Comments
 (0)