From e499447cf56e84258df61f2db072e45dd607e27c Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Sun, 19 Sep 2021 23:10:01 -0500 Subject: [PATCH] Added operator overloading for Decimals Note: This feature is being enabled through babel, and unfortunately doesn't really have any typescript support. Using an overloaded operator will show an error and be typed as "any". If ecmascript ever support operator overloading, then typescript will follow suit and these issues can be resolved. Here's the current proposal for how that could look like, although it's a long way's off from being accepted, if it ever is: https://github.com/tc39/proposal-operator-overloading Alternatively, there's a proposal for declaring that certain types have operator overloads, which would also work just perfectly: https://github.com/microsoft/TypeScript/issues/42218 In the meantime, the errors will unfortunately remain present, although they won't cause any issues in production. BTW, the rhs can be any DecimalSource, but the lhs has to be a Decimal. --- babel.config.js | 10 ++++++++- package-lock.json | 13 ++++++++++++ package.json | 1 + src/lib/break_eternity.ts | 44 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/babel.config.js b/babel.config.js index 4ab6557..bc3098f 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1,3 +1,11 @@ module.exports = { - presets: ["@vue/cli-plugin-babel/preset"] + presets: ["@vue/cli-plugin-babel/preset"], + plugins: [ + [ + "module:@jetblack/operator-overloading", + { + enabled: true + } + ] + ] }; diff --git a/package-lock.json b/package-lock.json index dbfe0eb..095a510 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ }, "devDependencies": { "@ivanv/vue-collapse-transition": "^1.0.2", + "@jetblack/operator-overloading": "^0.2.0", "@types/lodash.clonedeep": "^4.5.6", "@typescript-eslint/eslint-plugin": "^4.18.0", "@typescript-eslint/parser": "^4.18.0", @@ -13073,6 +13074,12 @@ "integrity": "sha512-eWEameFXJM/1khcoKbITvKjYYXDP1WKQ/Xf9ItJVPoEjCiOdocR3AgDAERzDrNNg4oWK28gRGi+0ft8Te27zxw==", "dev": true }, + "node_modules/@jetblack/operator-overloading": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/@jetblack/operator-overloading/-/operator-overloading-0.2.0.tgz", + "integrity": "sha512-Y6iNzYnNUoCUcRMoUL1NflWLHkrzkxCC6VHB5h57pcjiUaPHTidZH9uyz123d6utA9w9VO+cS7S/KLA1DEpS/g==", + "dev": true + }, "node_modules/@mrmlnc/readdir-enhanced": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz", @@ -29717,6 +29724,12 @@ "integrity": "sha512-eWEameFXJM/1khcoKbITvKjYYXDP1WKQ/Xf9ItJVPoEjCiOdocR3AgDAERzDrNNg4oWK28gRGi+0ft8Te27zxw==", "dev": true }, + "@jetblack/operator-overloading": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/@jetblack/operator-overloading/-/operator-overloading-0.2.0.tgz", + "integrity": "sha512-Y6iNzYnNUoCUcRMoUL1NflWLHkrzkxCC6VHB5h57pcjiUaPHTidZH9uyz123d6utA9w9VO+cS7S/KLA1DEpS/g==", + "dev": true + }, "@mrmlnc/readdir-enhanced": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz", diff --git a/package.json b/package.json index 3100357..a40f139 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ }, "devDependencies": { "@ivanv/vue-collapse-transition": "^1.0.2", + "@jetblack/operator-overloading": "^0.2.0", "@types/lodash.clonedeep": "^4.5.6", "@typescript-eslint/eslint-plugin": "^4.18.0", "@typescript-eslint/parser": "^4.18.0", diff --git a/src/lib/break_eternity.ts b/src/lib/break_eternity.ts index 91fdd71..6e68354 100644 --- a/src/lib/break_eternity.ts +++ b/src/lib/break_eternity.ts @@ -882,6 +882,50 @@ export default class Decimal { return cost.div(currentRpS).add(cost.div(deltaRpS)); } + public [Symbol.for('+')](other: DecimalSource): DecimalSource { + return this.add(other); + } + + public [Symbol.for('-')](other: DecimalSource): DecimalSource { + return this.sub(other); + } + + public [Symbol.for('*')](other: DecimalSource): DecimalSource { + return this.times(other); + } + + public [Symbol.for('/')](other: DecimalSource): DecimalSource { + return this.div(other); + } + + public [Symbol.for('minus')](): DecimalSource { + return this.neg(); + } + + public [Symbol.for('==')](other: DecimalSource): boolean { + return this.eq(other); + } + + public [Symbol.for('>')](other: DecimalSource): boolean { + return this.gt(other); + } + + public [Symbol.for('<')](other: DecimalSource): boolean { + return this.lt(other); + } + + public [Symbol.for('>=')](other: DecimalSource): boolean { + return this.gte(other); + } + + public [Symbol.for('<=')](other: DecimalSource): boolean { + return this.lte(other); + } + + public [Symbol.for('!=')](other: DecimalSource): boolean { + return this.neq(other); + } + public normalize(): this { /* PSEUDOCODE: