From e00104793e24a61e202476a3e65c217a7730b6c0 Mon Sep 17 00:00:00 2001 From: Aaron Cox Date: Mon, 29 Jul 2024 20:53:11 -0700 Subject: [PATCH] Adding gt/gte/lt/lte comparison operators (#109) * Adding gt/gte/lt/lte comparison operators * Version 1.0.9-rc1 --- package.json | 2 +- src/chain/integer.ts | 40 ++++++++++++++++++++++++++++++++++++++++ test/integer.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 8e477bc2..6b2ab406 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/chain/integer.ts b/src/chain/integer.ts index 8cb054ae..b34cb294 100644 --- a/src/chain/integer.ts +++ b/src/chain/integer.ts @@ -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 @@ -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 diff --git a/test/integer.ts b/test/integer.ts index e3abee43..aa834493 100644 --- a/test/integer.ts +++ b/test/integer.ts @@ -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')