mirror of
https://github.com/apache/superset.git
synced 2024-09-19 20:19:37 -04:00
feat: add mergeMargin() function (#114)
* feat: add mergeMargin() * fix: typings * refactor: address chris' comments
This commit is contained in:
parent
b162aacb90
commit
e5d7e50ee2
@ -1,2 +1,3 @@
|
|||||||
export { default as getTextDimension } from './getTextDimension';
|
export { default as getTextDimension } from './getTextDimension';
|
||||||
export { default as computeMaxFontSize } from './computeMaxFontSize';
|
export { default as computeMaxFontSize } from './computeMaxFontSize';
|
||||||
|
export { default as mergeMargin } from './mergeMargin';
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
import { Margin } from './types';
|
||||||
|
|
||||||
|
export default function mergeMargin(
|
||||||
|
margin1: Partial<Margin> = {},
|
||||||
|
margin2: Partial<Margin> = {},
|
||||||
|
mode: 'expand' | 'shrink' = 'expand',
|
||||||
|
) {
|
||||||
|
const { top = 0, left = 0, bottom = 0, right = 0 } = margin1;
|
||||||
|
const { top: top2 = 0, left: left2 = 0, bottom: bottom2 = 0, right: right2 = 0 } = margin2;
|
||||||
|
|
||||||
|
const func = mode === 'expand' ? Math.max : Math.min;
|
||||||
|
|
||||||
|
return {
|
||||||
|
bottom: func(bottom, bottom2),
|
||||||
|
left: func(left, left2),
|
||||||
|
right: func(right, right2),
|
||||||
|
top: func(top, top2),
|
||||||
|
};
|
||||||
|
}
|
@ -8,3 +8,10 @@ export interface TextStyle {
|
|||||||
fontWeight?: string | number;
|
fontWeight?: string | number;
|
||||||
letterSpacing?: string | number;
|
letterSpacing?: string | number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Margin {
|
||||||
|
top: number;
|
||||||
|
left: number;
|
||||||
|
bottom: number;
|
||||||
|
right: number;
|
||||||
|
}
|
||||||
|
@ -0,0 +1,166 @@
|
|||||||
|
import { mergeMargin } from '../src';
|
||||||
|
|
||||||
|
describe('mergeMargin(margin1, margin2, mode?)', () => {
|
||||||
|
it('combines two given margin', () => {
|
||||||
|
expect(
|
||||||
|
mergeMargin(
|
||||||
|
{
|
||||||
|
top: 1,
|
||||||
|
left: 1,
|
||||||
|
bottom: 2,
|
||||||
|
right: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
top: 2,
|
||||||
|
left: 2,
|
||||||
|
bottom: 1,
|
||||||
|
right: 1,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
).toEqual({
|
||||||
|
top: 2,
|
||||||
|
left: 2,
|
||||||
|
bottom: 2,
|
||||||
|
right: 2,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('default values', () => {
|
||||||
|
it('works if margin1 is not defined', () => {
|
||||||
|
expect(
|
||||||
|
mergeMargin(undefined, {
|
||||||
|
top: 2,
|
||||||
|
left: 2,
|
||||||
|
bottom: 1,
|
||||||
|
right: 1,
|
||||||
|
}),
|
||||||
|
).toEqual({
|
||||||
|
top: 2,
|
||||||
|
left: 2,
|
||||||
|
bottom: 1,
|
||||||
|
right: 1,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it('works if margin2 is not defined', () => {
|
||||||
|
expect(
|
||||||
|
mergeMargin(
|
||||||
|
{
|
||||||
|
top: 1,
|
||||||
|
left: 1,
|
||||||
|
bottom: 2,
|
||||||
|
right: 2,
|
||||||
|
},
|
||||||
|
undefined,
|
||||||
|
),
|
||||||
|
).toEqual({
|
||||||
|
top: 1,
|
||||||
|
left: 1,
|
||||||
|
bottom: 2,
|
||||||
|
right: 2,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it('use 0 for the side that is not specified', () => {
|
||||||
|
expect(mergeMargin({}, {})).toEqual({
|
||||||
|
top: 0,
|
||||||
|
left: 0,
|
||||||
|
bottom: 0,
|
||||||
|
right: 0,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('mode', () => {
|
||||||
|
it('if mode=expand, returns the larger margin for each side', () => {
|
||||||
|
expect(
|
||||||
|
mergeMargin(
|
||||||
|
{
|
||||||
|
top: 1,
|
||||||
|
left: 1,
|
||||||
|
bottom: 2,
|
||||||
|
right: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
top: 2,
|
||||||
|
left: 2,
|
||||||
|
bottom: 1,
|
||||||
|
right: 1,
|
||||||
|
},
|
||||||
|
'expand',
|
||||||
|
),
|
||||||
|
).toEqual({
|
||||||
|
top: 2,
|
||||||
|
left: 2,
|
||||||
|
bottom: 2,
|
||||||
|
right: 2,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it('if mode=shrink, returns the smaller margin for each side', () => {
|
||||||
|
expect(
|
||||||
|
mergeMargin(
|
||||||
|
{
|
||||||
|
top: 1,
|
||||||
|
left: 1,
|
||||||
|
bottom: 2,
|
||||||
|
right: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
top: 2,
|
||||||
|
left: 2,
|
||||||
|
bottom: 1,
|
||||||
|
right: 1,
|
||||||
|
},
|
||||||
|
'shrink',
|
||||||
|
),
|
||||||
|
).toEqual({
|
||||||
|
top: 1,
|
||||||
|
left: 1,
|
||||||
|
bottom: 1,
|
||||||
|
right: 1,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it('expand by default', () => {
|
||||||
|
expect(
|
||||||
|
mergeMargin(
|
||||||
|
{
|
||||||
|
top: 1,
|
||||||
|
left: 1,
|
||||||
|
bottom: 2,
|
||||||
|
right: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
top: 2,
|
||||||
|
left: 2,
|
||||||
|
bottom: 1,
|
||||||
|
right: 1,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
).toEqual({
|
||||||
|
top: 2,
|
||||||
|
left: 2,
|
||||||
|
bottom: 2,
|
||||||
|
right: 2,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it('works correctly for negative margins', () => {
|
||||||
|
expect(
|
||||||
|
mergeMargin(
|
||||||
|
{
|
||||||
|
top: -3,
|
||||||
|
left: -3,
|
||||||
|
bottom: -2,
|
||||||
|
right: -2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
top: -2,
|
||||||
|
left: -2,
|
||||||
|
bottom: 0,
|
||||||
|
right: -1,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
).toEqual({
|
||||||
|
top: -2,
|
||||||
|
left: -2,
|
||||||
|
bottom: 0,
|
||||||
|
right: -1,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user