Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

numbers: Add proper safety to operator overloading #204

Merged
merged 2 commits into from
Sep 9, 2021
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
10 changes: 10 additions & 0 deletions common/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,25 @@ export class BigInt extends Uint8Array {

@operator('+')
plus(other: BigInt): BigInt {
assert(this !== null, "Failed to sum BigInts because left hand side is 'null'");
return bigInt.plus(this, other)
}

@operator('-')
minus(other: BigInt): BigInt {
assert(this !== null, "Failed to subtract BigInts because left hand side is 'null'");
return bigInt.minus(this, other)
}

@operator('*')
times(other: BigInt): BigInt {
assert(this !== null, "Failed to multiply BigInts because left hand side is 'null'");
return bigInt.times(this, other)
}

@operator('/')
div(other: BigInt): BigInt {
assert(this !== null, "Failed to divide BigInts because left hand side is 'null'");
return bigInt.dividedBy(this, other)
}

Expand All @@ -161,6 +165,7 @@ export class BigInt extends Uint8Array {

@operator('%')
mod(other: BigInt): BigInt {
assert(this !== null, "Failed to apply module to BigInt because left hand side is 'null'");
return bigInt.mod(this, other)
}

Expand Down Expand Up @@ -319,21 +324,25 @@ export class BigDecimal {

@operator('+')
plus(other: BigDecimal): BigDecimal {
assert(this !== null, "Failed to sum BigDecimals because left hand side is 'null'");
return bigDecimal.plus(this, other)
}

@operator('-')
minus(other: BigDecimal): BigDecimal {
assert(this !== null, "Failed to subtract BigDecimals because left hand side is 'null'");
return bigDecimal.minus(this, other)
}

@operator('*')
times(other: BigDecimal): BigDecimal {
assert(this !== null, "Failed to multiply BigDecimals because left hand side is 'null'");
return bigDecimal.times(this, other)
}

@operator('/')
div(other: BigDecimal): BigDecimal {
assert(this !== null, "Failed to divide BigDecimals because left hand side is 'null'");
return bigDecimal.dividedBy(this, other)
}

Expand Down Expand Up @@ -369,6 +378,7 @@ export class BigDecimal {

@operator.prefix('-')
neg(): BigDecimal {
assert(this !== null, "Failed to negate BigDecimal because the value of it is 'null'");
return new BigDecimal(new BigInt(0)) - this
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@graphprotocol/graph-ts",
"description": "TypeScript/AssemblyScript library for writing subgraph mappings for The Graph",
"version": "0.22.0-alpha.2",
"version": "0.22.0-alpha.3",
"module": "index.ts",
"types": "index.ts",
"main": "index.ts",
Expand Down