Skip to content

Commit

Permalink
Adding gt/gte/lt/lte comparison operators (#109)
Browse files Browse the repository at this point in the history
* Adding gt/gte/lt/lte comparison operators

* Version 1.0.9-rc1
  • Loading branch information
aaroncox authored Jul 30, 2024
1 parent e5d1938 commit e001047
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@wharfkit/antelope",
"description": "Library for working with Antelope powered blockchains.",
"version": "1.0.8",
"version": "1.0.9-rc1",
"homepage": "https://github.com/wharfkit/antelope",
"license": "BSD-3-Clause-No-Military-License",
"main": "lib/antelope.js",
Expand Down
40 changes: 40 additions & 0 deletions src/chain/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,26 @@ export class Int implements ABISerializableObject {
})
}

/** Compare `lhs` to `rhs` and return true if `lhs` is greater than `rhs`. */
static gt(lhs: Int, rhs: Int) {
return lhs.value.gt(rhs.value)
}

/** Compare `lhs` to `rhs` and return true if `lhs` is less than `rhs`. */
static lt(lhs: Int, rhs: Int) {
return lhs.value.lt(rhs.value)
}

/** Compare `lhs` to `rhs` and return true if `lhs` is greater than or equal to `rhs`. */
static gte(lhs: Int, rhs: Int) {
return lhs.value.gte(rhs.value)
}

/** Compare `lhs` to `rhs` and return true if `lhs` is less than or equal to `rhs`. */
static lte(lhs: Int, rhs: Int) {
return lhs.value.lte(rhs.value)
}

/**
* Can be used to implement custom operator.
* @internal
Expand Down Expand Up @@ -306,6 +326,26 @@ export class Int implements ABISerializableObject {
return this.operator(by, op)
}

/** Greater than comparision operator */
gt(other: Int) {
return Int.gt(this, other)
}

/** Less than comparision operator */
lt(other: Int) {
return Int.lt(this, other)
}

/** Greater than or equal comparision operator */
gte(other: Int) {
return Int.gte(this, other)
}

/** Less than or equal comparision operator */
lte(other: Int) {
return Int.lte(this, other)
}

/**
* Run operator with C++11 implicit conversion.
* @internal
Expand Down
30 changes: 30 additions & 0 deletions test/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,36 @@ suite('integer', function () {
}, /Division by zero/)
})

test('greater than', function () {
assert.isTrue(Int8.from(10).gt(Int8.from(5)))
assert.isTrue(Int64.from(10).gt(Int8.from(5)))
assert.isTrue(Int8.from(10).gt(Int64.from(5)))
})

test('less than', function () {
assert.isTrue(Int8.from(5).lt(Int8.from(10)))
assert.isTrue(Int64.from(5).lt(Int8.from(10)))
assert.isTrue(Int8.from(5).lt(Int64.from(10)))
})

test('greater than or equal', function () {
assert.isTrue(Int8.from(10).gte(Int8.from(5)))
assert.isTrue(Int64.from(10).gte(Int8.from(5)))
assert.isTrue(Int8.from(10).gte(Int64.from(5)))
assert.isTrue(Int8.from(10).gte(Int8.from(10)))
assert.isTrue(Int64.from(10).gte(Int8.from(10)))
assert.isTrue(Int8.from(10).gte(Int64.from(10)))
})

test('less than or equal', function () {
assert.isTrue(Int8.from(5).lte(Int8.from(10)))
assert.isTrue(Int64.from(5).lte(Int8.from(10)))
assert.isTrue(Int8.from(5).lte(Int64.from(10)))
assert.isTrue(Int8.from(10).lte(Int8.from(10)))
assert.isTrue(Int64.from(10).lte(Int8.from(10)))
assert.isTrue(Int8.from(10).lte(Int64.from(10)))
})

test('to primitive', function () {
const smallValue = UInt64.from('1459536')
assert.equal(String(smallValue), '1459536')
Expand Down

0 comments on commit e001047

Please sign in to comment.