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.
This commit is contained in:
thepaperpilot 2021-09-19 23:10:01 -05:00
parent bc8622f219
commit e499447cf5
4 changed files with 67 additions and 1 deletions

View file

@ -1,3 +1,11 @@
module.exports = { module.exports = {
presets: ["@vue/cli-plugin-babel/preset"] presets: ["@vue/cli-plugin-babel/preset"],
plugins: [
[
"module:@jetblack/operator-overloading",
{
enabled: true
}
]
]
}; };

13
package-lock.json generated
View file

@ -21,6 +21,7 @@
}, },
"devDependencies": { "devDependencies": {
"@ivanv/vue-collapse-transition": "^1.0.2", "@ivanv/vue-collapse-transition": "^1.0.2",
"@jetblack/operator-overloading": "^0.2.0",
"@types/lodash.clonedeep": "^4.5.6", "@types/lodash.clonedeep": "^4.5.6",
"@typescript-eslint/eslint-plugin": "^4.18.0", "@typescript-eslint/eslint-plugin": "^4.18.0",
"@typescript-eslint/parser": "^4.18.0", "@typescript-eslint/parser": "^4.18.0",
@ -13073,6 +13074,12 @@
"integrity": "sha512-eWEameFXJM/1khcoKbITvKjYYXDP1WKQ/Xf9ItJVPoEjCiOdocR3AgDAERzDrNNg4oWK28gRGi+0ft8Te27zxw==", "integrity": "sha512-eWEameFXJM/1khcoKbITvKjYYXDP1WKQ/Xf9ItJVPoEjCiOdocR3AgDAERzDrNNg4oWK28gRGi+0ft8Te27zxw==",
"dev": true "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": { "node_modules/@mrmlnc/readdir-enhanced": {
"version": "2.2.1", "version": "2.2.1",
"resolved": "https://registry.npmjs.org/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz", "resolved": "https://registry.npmjs.org/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz",
@ -29717,6 +29724,12 @@
"integrity": "sha512-eWEameFXJM/1khcoKbITvKjYYXDP1WKQ/Xf9ItJVPoEjCiOdocR3AgDAERzDrNNg4oWK28gRGi+0ft8Te27zxw==", "integrity": "sha512-eWEameFXJM/1khcoKbITvKjYYXDP1WKQ/Xf9ItJVPoEjCiOdocR3AgDAERzDrNNg4oWK28gRGi+0ft8Te27zxw==",
"dev": true "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": { "@mrmlnc/readdir-enhanced": {
"version": "2.2.1", "version": "2.2.1",
"resolved": "https://registry.npmjs.org/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz", "resolved": "https://registry.npmjs.org/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz",

View file

@ -21,6 +21,7 @@
}, },
"devDependencies": { "devDependencies": {
"@ivanv/vue-collapse-transition": "^1.0.2", "@ivanv/vue-collapse-transition": "^1.0.2",
"@jetblack/operator-overloading": "^0.2.0",
"@types/lodash.clonedeep": "^4.5.6", "@types/lodash.clonedeep": "^4.5.6",
"@typescript-eslint/eslint-plugin": "^4.18.0", "@typescript-eslint/eslint-plugin": "^4.18.0",
"@typescript-eslint/parser": "^4.18.0", "@typescript-eslint/parser": "^4.18.0",

View file

@ -882,6 +882,50 @@ export default class Decimal {
return cost.div(currentRpS).add(cost.div(deltaRpS)); 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 { public normalize(): this {
/* /*
PSEUDOCODE: PSEUDOCODE: