generated from react-component/footer
-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add util genCalc and type AbstractCalculator #184
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import AbstractCalculator from './calculator'; | ||
|
||
const CALC_UNIT = 'CALC_UNIT'; | ||
|
||
const regexp = new RegExp(CALC_UNIT, 'g'); | ||
|
||
function unit(value: string | number) { | ||
if (typeof value === 'number') { | ||
return `${value}${CALC_UNIT}`; | ||
} | ||
return value; | ||
} | ||
|
||
export default class CSSCalculator extends AbstractCalculator { | ||
result: string = ''; | ||
|
||
unitlessCssVar: Set<string>; | ||
|
||
lowPriority?: boolean; | ||
|
||
constructor( | ||
num: number | string | AbstractCalculator, | ||
unitlessCssVar: Set<string>, | ||
) { | ||
super(); | ||
|
||
const numType = typeof num; | ||
|
||
this.unitlessCssVar = unitlessCssVar; | ||
|
||
if (num instanceof CSSCalculator) { | ||
this.result = `(${num.result})`; | ||
} else if (numType === 'number') { | ||
this.result = unit(num as number); | ||
} else if (numType === 'string') { | ||
this.result = num as string; | ||
} | ||
} | ||
|
||
add(num: number | string | AbstractCalculator): this { | ||
if (num instanceof CSSCalculator) { | ||
this.result = `${this.result} + ${num.getResult()}`; | ||
} else if (typeof num === 'number' || typeof num === 'string') { | ||
this.result = `${this.result} + ${unit(num)}`; | ||
} | ||
this.lowPriority = true; | ||
return this; | ||
} | ||
|
||
sub(num: number | string | AbstractCalculator): this { | ||
if (num instanceof CSSCalculator) { | ||
this.result = `${this.result} - ${num.getResult()}`; | ||
} else if (typeof num === 'number' || typeof num === 'string') { | ||
this.result = `${this.result} - ${unit(num)}`; | ||
} | ||
this.lowPriority = true; | ||
return this; | ||
} | ||
|
||
mul(num: number | string | 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' || typeof num === 'string') { | ||
this.result = `${this.result} * ${num}`; | ||
} | ||
this.lowPriority = false; | ||
return this; | ||
} | ||
|
||
div(num: number | string | 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' || typeof num === 'string') { | ||
this.result = `${this.result} / ${num}`; | ||
} | ||
this.lowPriority = false; | ||
return this; | ||
} | ||
|
||
getResult(force?: boolean): string { | ||
return this.lowPriority || force ? `(${this.result})` : this.result; | ||
} | ||
|
||
equal(options?: { unit?: boolean }): string { | ||
const { unit: cssUnit } = options || {}; | ||
|
||
let mergedUnit: boolean = true; | ||
if (typeof cssUnit === 'boolean') { | ||
mergedUnit = cssUnit; | ||
} else if ( | ||
Array.from(this.unitlessCssVar).some((cssVar) => | ||
this.result.includes(cssVar), | ||
) | ||
) { | ||
mergedUnit = false; | ||
} | ||
|
||
this.result = this.result.replace(regexp, mergedUnit ? 'px' : ''); | ||
if (typeof this.lowPriority !== 'undefined') { | ||
return `calc(${this.result})`; | ||
} | ||
return this.result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
abstract class AbstractCalculator { | ||
/** | ||
* @descCN 计算两数的和,例如:1 + 2 | ||
* @descEN Calculate the sum of two numbers, e.g. 1 + 2 | ||
*/ | ||
abstract add(num: number | string | AbstractCalculator): this; | ||
|
||
/** | ||
* @descCN 计算两数的差,例如:1 - 2 | ||
* @descEN Calculate the difference between two numbers, e.g. 1 - 2 | ||
*/ | ||
abstract sub(num: number | string | AbstractCalculator): this; | ||
|
||
/** | ||
* @descCN 计算两数的积,例如:1 * 2 | ||
* @descEN Calculate the product of two numbers, e.g. 1 * 2 | ||
*/ | ||
abstract mul(num: number | string | AbstractCalculator): this; | ||
|
||
/** | ||
* @descCN 计算两数的商,例如:1 / 2 | ||
* @descEN Calculate the quotient of two numbers, e.g. 1 / 2 | ||
*/ | ||
abstract div(num: number | string | AbstractCalculator): this; | ||
|
||
/** | ||
* @descCN 获取计算结果 | ||
* @descEN Get the calculation result | ||
*/ | ||
abstract equal(options?: { unit?: boolean }): string | number; | ||
} | ||
|
||
export default AbstractCalculator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type AbstractCalculator from './calculator'; | ||
import CSSCalculator from './CSSCalculator'; | ||
import NumCalculator from './NumCalculator'; | ||
|
||
const genCalc = (type: 'css' | 'js', unitlessCssVar: Set<string>) => { | ||
const Calculator = type === 'css' ? CSSCalculator : NumCalculator; | ||
|
||
return (num: number | string | AbstractCalculator) => | ||
new Calculator(num, unitlessCssVar); | ||
}; | ||
|
||
export default genCalc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export { default as genCalc } from './calc'; | ||
export type { default as AbstractCalculator } from './calc/calculator'; | ||
export { default as createTheme } from './createTheme'; | ||
export type { DerivativeFunc, TokenType } from './interface'; | ||
export { default as Theme } from './Theme'; | ||
export { default as ThemeCache } from './ThemeCache'; | ||
export type { TokenType, DerivativeFunc } from './interface'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import type { AbstractCalculator } from '../src'; | ||
import { genCalc } from '../src'; | ||
|
||
describe('calculator', () => { | ||
const cases: [ | ||
( | ||
calc: (num: number | AbstractCalculator) => AbstractCalculator, | ||
) => string | number, | ||
{ js: number; css: string }, | ||
][] = [ | ||
[ | ||
// 1 + 1 | ||
(calc) => calc(1).add(1).equal(), | ||
{ | ||
js: 2, | ||
css: 'calc(1px + 1px)', | ||
}, | ||
], | ||
[ | ||
// (1 + 1) * 4 | ||
(calc) => calc(1).add(1).mul(4).equal(), | ||
{ | ||
js: 8, | ||
css: 'calc((1px + 1px) * 4)', | ||
}, | ||
], | ||
[ | ||
// (2 + 4) / 2 - 2 | ||
(calc) => calc(2).add(4).div(2).sub(2).equal(), | ||
{ | ||
js: 1, | ||
css: 'calc((2px + 4px) / 2 - 2px)', | ||
}, | ||
], | ||
[ | ||
// Bad case | ||
// (2 + 4) / (3 - 2) - 2 | ||
(calc) => calc(2).add(4).div(calc(3).sub(2)).sub(2).equal(), | ||
{ | ||
js: 4, | ||
css: 'calc((2px + 4px) / (3px - 2px) - 2px)', | ||
}, | ||
], | ||
[ | ||
// Bad case | ||
// 2 * (2 + 3) | ||
(calc) => calc(2).mul(calc(2).add(3)).equal(), | ||
{ | ||
js: 10, | ||
css: 'calc(2px * (2px + 3px))', | ||
}, | ||
], | ||
[ | ||
// (1 + 2) * 3 | ||
(calc) => calc(calc(1).add(2)).mul(3).equal(), | ||
{ | ||
js: 9, | ||
css: 'calc((1px + 2px) * 3)', | ||
}, | ||
], | ||
[ | ||
// 1 + (2 - 1) | ||
(calc) => calc(1).add(calc(2).sub(1)).equal(), | ||
{ | ||
js: 2, | ||
css: 'calc(1px + (2px - 1px))', | ||
}, | ||
], | ||
[ | ||
// 1 + 2 * 2 | ||
(calc) => calc(1).add(calc(2).mul(2)).equal(), | ||
{ | ||
js: 5, | ||
css: 'calc(1px + 2px * 2)', | ||
}, | ||
], | ||
[ | ||
// 5 - (2 - 1) | ||
(calc) => calc(5).sub(calc(2).sub(1)).equal(), | ||
{ | ||
js: 4, | ||
css: 'calc(5px - (2px - 1px))', | ||
}, | ||
], | ||
[ | ||
// 2 * 6 / 3 | ||
(calc) => calc(2).mul(6).div(3).equal(), | ||
{ | ||
js: 4, | ||
css: 'calc(2px * 6 / 3)', | ||
}, | ||
], | ||
[ | ||
// 6 / 3 * 2 | ||
(calc) => calc(6).div(3).mul(2).equal(), | ||
{ | ||
js: 4, | ||
css: 'calc(6px / 3 * 2)', | ||
}, | ||
], | ||
[ | ||
// Bad case | ||
// 6 / (3 * 2) | ||
(calc) => calc(6).div(calc(3).mul(2)).equal(), | ||
{ | ||
js: 1, | ||
css: 'calc(6px / (3px * 2))', | ||
}, | ||
], | ||
[ | ||
// 6 | ||
(calc) => calc(6).equal(), | ||
{ | ||
js: 6, | ||
css: '6px', | ||
}, | ||
], | ||
[ | ||
// 1000 + 100 without unit | ||
(calc) => calc(1000).add(100).equal({ unit: false }), | ||
{ | ||
js: 1100, | ||
css: 'calc(1000 + 100)', | ||
}, | ||
], | ||
]; | ||
|
||
cases.forEach(([exp, { js, css }], index) => { | ||
it(`js calc ${index + 1}`, () => { | ||
expect(exp(genCalc('js', new Set()))).toBe(js); | ||
}); | ||
|
||
it(`css calc ${index + 1}`, () => { | ||
expect(exp(genCalc('css', new Set()))).toBe(css); | ||
}); | ||
}); | ||
|
||
it('css calc should work with string', () => { | ||
const calc = genCalc('css', new Set()); | ||
expect(calc('var(--var1)').add('var(--var2)').equal()).toBe( | ||
'calc(var(--var1) + var(--var2))', | ||
); | ||
}); | ||
|
||
it('css calc var should skip zIndex', () => { | ||
const calc = genCalc('css', new Set(['--ant-z-index'])); | ||
expect(calc('var(--ant-z-index)').add(93).equal()).toBe( | ||
'calc(var(--ant-z-index) + 93)', | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个不用 export 了吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/search?q=repo%3Aant-design%2Fant-design%20AbstractCalculator&type=code
这里导出的原因是,除了 calc 目录下还有其他地方引用如:https://github.com/ant-design/ant-design/blob/7389fe8cc4c6341058046b35f75715ed21681926/components/theme/util/genComponentStyleHook.tsx#L20