mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-15 17:19:11 +08:00
75 lines
2.0 KiB
TypeScript
75 lines
2.0 KiB
TypeScript
|
import AbstractCalculator from './calculator';
|
||
|
import { unit } from '@ant-design/cssinjs';
|
||
|
|
||
|
export default class CSSCalculator extends AbstractCalculator {
|
||
|
result: string = '';
|
||
|
|
||
|
lowPriority?: boolean;
|
||
|
|
||
|
constructor(num: number | AbstractCalculator) {
|
||
|
super();
|
||
|
if (num instanceof CSSCalculator) {
|
||
|
this.result = `(${num.result})`;
|
||
|
} else if (typeof num === 'number') {
|
||
|
this.result = unit(num);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
add(num: number | AbstractCalculator): this {
|
||
|
if (num instanceof CSSCalculator) {
|
||
|
this.result = `${this.result} + ${num.getResult()}`;
|
||
|
} else if (typeof num === 'number') {
|
||
|
this.result = `${this.result} + ${unit(num)}`;
|
||
|
}
|
||
|
this.lowPriority = true;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
sub(num: number | AbstractCalculator): this {
|
||
|
if (num instanceof CSSCalculator) {
|
||
|
this.result = `${this.result} - ${num.getResult()}`;
|
||
|
} else if (typeof num === 'number') {
|
||
|
this.result = `${this.result} - ${unit(num)}`;
|
||
|
}
|
||
|
this.lowPriority = true;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
mul(num: number | AbstractCalculator): this {
|
||
|
if (this.lowPriority) {
|
||
|
this.result = `(${this.result})`;
|
||
|
}
|
||
|
if (num instanceof CSSCalculator) {
|
||
|
this.result = `${this.result} * ${num.getResult(true)}`;
|
||
|
} else if (typeof num === 'number') {
|
||
|
this.result = `${this.result} * ${num}`;
|
||
|
}
|
||
|
this.lowPriority = false;
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
div(num: number | AbstractCalculator): this {
|
||
|
if (this.lowPriority) {
|
||
|
this.result = `(${this.result})`;
|
||
|
}
|
||
|
if (num instanceof CSSCalculator) {
|
||
|
this.result = `${this.result} / ${num.getResult(true)}`;
|
||
|
} else if (typeof num === 'number') {
|
||
|
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;
|
||
|
}
|
||
|
}
|