2023-11-06 10:31:51 +08:00
|
|
|
import AbstractCalculator from './calculator';
|
|
|
|
import { unit } from '@ant-design/cssinjs';
|
|
|
|
|
|
|
|
export default class CSSCalculator extends AbstractCalculator {
|
|
|
|
result: string = '';
|
|
|
|
|
|
|
|
lowPriority?: boolean;
|
|
|
|
|
2023-11-08 14:56:15 +08:00
|
|
|
constructor(num: number | string | AbstractCalculator) {
|
2023-11-06 10:31:51 +08:00
|
|
|
super();
|
|
|
|
if (num instanceof CSSCalculator) {
|
|
|
|
this.result = `(${num.result})`;
|
|
|
|
} else if (typeof num === 'number') {
|
|
|
|
this.result = unit(num);
|
2023-11-08 14:56:15 +08:00
|
|
|
} else if (typeof num === 'string') {
|
|
|
|
this.result = num;
|
2023-11-06 10:31:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 14:56:15 +08:00
|
|
|
add(num: number | string | AbstractCalculator): this {
|
2023-11-06 10:31:51 +08:00
|
|
|
if (num instanceof CSSCalculator) {
|
|
|
|
this.result = `${this.result} + ${num.getResult()}`;
|
2023-11-08 14:56:15 +08:00
|
|
|
} else if (typeof num === 'number' || typeof num === 'string') {
|
2023-11-06 10:31:51 +08:00
|
|
|
this.result = `${this.result} + ${unit(num)}`;
|
|
|
|
}
|
|
|
|
this.lowPriority = true;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-11-08 14:56:15 +08:00
|
|
|
sub(num: number | string | AbstractCalculator): this {
|
2023-11-06 10:31:51 +08:00
|
|
|
if (num instanceof CSSCalculator) {
|
|
|
|
this.result = `${this.result} - ${num.getResult()}`;
|
2023-11-08 14:56:15 +08:00
|
|
|
} else if (typeof num === 'number' || typeof num === 'string') {
|
2023-11-06 10:31:51 +08:00
|
|
|
this.result = `${this.result} - ${unit(num)}`;
|
|
|
|
}
|
|
|
|
this.lowPriority = true;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-11-08 14:56:15 +08:00
|
|
|
mul(num: number | string | AbstractCalculator): this {
|
2023-11-06 10:31:51 +08:00
|
|
|
if (this.lowPriority) {
|
|
|
|
this.result = `(${this.result})`;
|
|
|
|
}
|
|
|
|
if (num instanceof CSSCalculator) {
|
|
|
|
this.result = `${this.result} * ${num.getResult(true)}`;
|
2023-11-08 14:56:15 +08:00
|
|
|
} else if (typeof num === 'number' || typeof num === 'string') {
|
2023-11-06 10:31:51 +08:00
|
|
|
this.result = `${this.result} * ${num}`;
|
|
|
|
}
|
|
|
|
this.lowPriority = false;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-11-08 14:56:15 +08:00
|
|
|
div(num: number | string | AbstractCalculator): this {
|
2023-11-06 10:31:51 +08:00
|
|
|
if (this.lowPriority) {
|
|
|
|
this.result = `(${this.result})`;
|
|
|
|
}
|
|
|
|
if (num instanceof CSSCalculator) {
|
|
|
|
this.result = `${this.result} / ${num.getResult(true)}`;
|
2023-11-08 14:56:15 +08:00
|
|
|
} else if (typeof num === 'number' || typeof num === 'string') {
|
2023-11-06 10:31:51 +08:00
|
|
|
this.result = `${this.result} / ${num}`;
|
|
|
|
}
|
|
|
|
this.lowPriority = false;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
getResult(force?: boolean): string {
|
|
|
|
return this.lowPriority || force ? `(${this.result})` : this.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
equal(): string {
|
|
|
|
if (typeof this.lowPriority !== 'undefined') {
|
|
|
|
return `calc(${this.result})`;
|
|
|
|
}
|
|
|
|
return this.result;
|
|
|
|
}
|
|
|
|
}
|