Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding gt/gte/lt/lte comparison operators #109

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading