feat: update @idraw/core check methods

This commit is contained in:
chenshenhai 2021-06-17 10:44:25 +08:00
parent b9d47dd259
commit 1f4ea809e1
3 changed files with 173 additions and 7 deletions

View file

@ -19,7 +19,6 @@ describe("@idraw/core static check", () => {
angle: 0
})).toStrictEqual(false);
expect(Core.check.attrs({
x: 0,
y: 100,
@ -29,4 +28,107 @@ describe("@idraw/core static check", () => {
})).toStrictEqual(false);
});
test('Core.check.rectDesc', () => {
expect(Core.check.rectDesc({
color: '#ffffff',
})).toStrictEqual(true);
expect(Core.check.rectDesc({
color: 123,
})).toStrictEqual(false);
expect(Core.check.rectDesc({
borderRadius: 12,
borderWidth: 10,
borderColor: '#123abf',
color: '#ffffff',
})).toStrictEqual(true);
expect(Core.check.rectDesc({
borderRadius: 12,
borderWidth: 10,
borderColor: '#123af',
})).toStrictEqual(false);
});
test('Core.check.imageDesc', () => {
expect(Core.check.imageDesc({
src: 'https://xxxxxx',
})).toStrictEqual(true);
expect(Core.check.imageDesc({
src: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOg',
})).toStrictEqual(true);
expect(Core.check.imageDesc({
src: '/xx/xxx/xxx',
})).toStrictEqual(true);
expect(Core.check.imageDesc({
src: './xx/xxx/xxx',
})).toStrictEqual(true);
expect(Core.check.imageDesc({
src: 'abcdefg',
})).toStrictEqual(false);
expect(Core.check.imageDesc({
src: 1234,
})).toStrictEqual(false);
expect(Core.check.imageDesc({})).toStrictEqual(false);
});
test('Core.check.svgDesc', () => {
expect(Core.check.svgDesc({
svg: `
<svg t="12231231">
<g></g>
</svg>
`,
})).toStrictEqual(true);
expect(Core.check.svgDesc({
svg: `
<svg>
<g></g>
</ svg>
`,
})).toStrictEqual(true);
expect(Core.check.svgDesc({
svg: './xxxxx/xxx',
})).toStrictEqual(false);
expect(Core.check.svgDesc({})).toStrictEqual(false);
});
test('Core.check.textDesc', () => {
expect(Core.check.textDesc({
text: 'abcdefg',
color: '#af1234',
fontSize: 12,
lineHeight: 12,
fontFamily: 'abc',
textAlign: 'center',
borderRadius: 12,
borderWidth: 10,
borderColor: '#123abf',
})).toStrictEqual(true);
expect(Core.check.textDesc({
text: 'abcdefg',
color: '#af1234',
fontSize: 12,
})).toStrictEqual(true);
expect(Core.check.textDesc({})).toStrictEqual(false);
});
})

View file

@ -16,20 +16,81 @@ function attrs(
return true;
}
function box(
desc: any = {},
): boolean {
const { borderColor, borderRadius, borderWidth } = desc;
if (desc.hasOwnProperty('borderColor') && !is.color(borderColor)) {
return false;
}
if (desc.hasOwnProperty('borderRadius') && !is.number(borderRadius)) {
return false;
}
if (desc.hasOwnProperty('borderWidth') && !is.number(borderWidth)) {
return false;
}
return true;
}
function rectDesc(
desc: any
): boolean {
const { borderColor, borderRadius, borderWidth, color } = desc;
if (typeof borderColor === 'string' && !is.color(color)) {
const { color } = desc;
if (desc.hasOwnProperty('color') && !is.color(color)) {
return false;
}
if (typeof borderColor === 'string' && !is.color(borderColor)) {
if (!box(desc)) {
return false;
}
if (typeof borderRadius === 'number' && !is.number(borderRadius)) {
return true;
}
function imageDesc(
desc: any
): boolean {
const { src } = desc;
if (!is.imageSrc(src)) {
return false;
}
if (typeof borderWidth === 'number' && !is.number(borderWidth)) {
return true;
}
function svgDesc(
desc: any
): boolean {
const { svg } = desc;
if (!is.svg(svg)) {
return false;
}
return true;
}
function textDesc(
desc: any
): boolean {
const {
text, color, fontSize, lineHeight, fontFamily, textAlign,
} = desc;
if (!is.text(text)){
return false;
}
if (!is.color(color)){
return false;
}
if (!is.fontSize(fontSize)){
return false;
}
if (desc.hasOwnProperty('lineHeight') && !is.lineHeight(lineHeight)){
return false;
}
if (desc.hasOwnProperty('fontFamily') && !is.fontFamily(fontFamily)){
return false;
}
if (desc.hasOwnProperty('textAlign') && !is.textAlign(textAlign)){
return false;
}
if (!box(desc)) {
return false;
}
return true;
@ -38,6 +99,9 @@ function rectDesc(
const check = {
attrs,
rectDesc,
imageDesc,
svgDesc,
textDesc,
}

View file

@ -39,7 +39,7 @@ type TypeElemDescText = {
color: string;
fontSize: number;
lineHeight?: number;
fontWeight?: string;
// fontWeight?: string;
fontFamily?: string;
textAlign?: 'center' | 'left' | 'right';
} & TypeElemBoxDesc