ant-design/components/theme/util/calc/NumCalculator.ts
MadCcc bacaad0262
feat: calc support no unit (#45775)
* chore: calc support no unit

* test: add test case

* test: add test case
2023-11-10 14:54:16 +08:00

55 lines
1.3 KiB
TypeScript

import AbstractCalculator from './calculator';
export default class NumCalculator extends AbstractCalculator {
result: number = 0;
constructor(num: number | string | AbstractCalculator) {
super();
if (num instanceof NumCalculator) {
this.result = num.result;
} else if (typeof num === 'number') {
this.result = num;
}
}
add(num: number | string | AbstractCalculator): this {
if (num instanceof NumCalculator) {
this.result += num.result;
} else if (typeof num === 'number') {
this.result += num;
}
return this;
}
sub(num: number | string | AbstractCalculator): this {
if (num instanceof NumCalculator) {
this.result -= num.result;
} else if (typeof num === 'number') {
this.result -= num;
}
return this;
}
mul(num: number | string | AbstractCalculator): this {
if (num instanceof NumCalculator) {
this.result *= num.result;
} else if (typeof num === 'number') {
this.result *= num;
}
return this;
}
div(num: number | string | AbstractCalculator): this {
if (num instanceof NumCalculator) {
this.result /= num.result;
} else if (typeof num === 'number') {
this.result /= num;
}
return this;
}
equal(): number {
return this.result;
}
}