mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-11 13:59:11 +08:00
bacaad0262
* chore: calc support no unit * test: add test case * test: add test case
55 lines
1.3 KiB
TypeScript
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;
|
|
}
|
|
}
|