优化错误验证

This commit is contained in:
Harvey Zack committed 2019-05-07 14:22:15 +08:00
1 parent aa156e1516
commit abff0e0331
5 files changed
+118 -113

No files matched your search

+16 -18
View File
@@ -1,19 +1,10 @@
import { ArtPlayerError } from '../utils';
import { errorHandle } from '../utils';
function validElement(paths, value, type) {
if (type === 'string') {
if (value.trim() === '') {
throw new ArtPlayerError(`${paths.join('.')} can not be empty`);
} else {
return true;
}
}
if (value instanceof Element) {
return true;
}
throw new ArtPlayerError(`${paths.join('.')} require 'string' or 'Element' type, but got '${type}'`);
return errorHandle(
type === 'string' || value instanceof Element,
`${paths.join('.')} require 'string' or 'Element' type`,
);
}
export default {
@@ -28,6 +19,7 @@ export default {
poster: 'string',
title: 'string',
theme: 'string',
lang: 'string',
volume: 'number',
isLive: 'boolean',
muted: 'boolean',
@@ -66,6 +58,7 @@ export default {
html: validElement,
style: 'object',
click: 'function',
mounted: 'function',
},
},
contextmenu: {
@@ -78,6 +71,7 @@ export default {
html: validElement,
style: 'object',
click: 'function',
mounted: 'function',
},
},
quality: {
@@ -98,7 +92,14 @@ export default {
html: validElement,
style: 'object',
click: 'function',
position: (paths, value) => ['top', 'left', 'right'].includes(value),
mounted: 'function',
position: (paths, value) => {
const position = ['top', 'left', 'right'];
return errorHandle(
position.includes(value),
`${paths.join('.')} only accept ${position.toString()} as parameters`,
);
},
},
},
highlight: {
@@ -112,7 +113,6 @@ export default {
thumbnails: {
type: 'object',
child: {
type: 'object',
url: 'string',
number: 'number',
width: 'number',
@@ -123,7 +123,6 @@ export default {
subtitle: {
type: 'object',
child: {
type: 'object',
url: 'string',
style: 'object',
},
@@ -131,5 +130,4 @@ export default {
moreVideoAttr: 'object',
icons: 'object',
customType: 'object',
lang: 'string',
};
@@ -0,0 +1,9 @@
export default class ArtPlayerError extends Error {
constructor(message, context) {
super(message);
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, context || this.constructor);
}
this.name = 'ArtPlayerError';
}
}
+15 -35
View File
@@ -1,17 +1,10 @@
export class ArtPlayerError extends Error {
constructor(message, context) {
super(message);
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, context || this.constructor);
}
this.name = 'ArtPlayerError';
}
}
import ArtPlayerError from './ArtPlayerError';
export function errorHandle(condition, msg) {
if (!condition) {
throw new ArtPlayerError(msg);
}
return condition;
}
export function clamp(num, a, b) {
@@ -64,35 +57,22 @@ export function debounce(func, wait, context) {
return fn;
}
export function isObject(item) {
return item && typeof item === 'object' && !Array.isArray(item);
}
export function isElement(item) {
return item instanceof Element;
}
export function mergeDeep(target, source) {
const output = Object.assign({}, target);
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach(key => {
const value = source[key];
if (isObject(value) && !isElement(value)) {
if (!(key in target)) {
Object.assign(output, {
[key]: value,
});
} else {
output[key] = mergeDeep(target[key], value);
}
export function mergeDeep(...objects) {
const isObject = item => item && typeof item === 'object' && !Array.isArray(item);
return objects.reduce((prev, obj) => {
Object.keys(obj).forEach(key => {
const pVal = prev[key];
const oVal = obj[key];
if (Array.isArray(pVal) && Array.isArray(oVal)) {
prev[key] = pVal.concat(...oVal);
} else if (isObject(pVal) && isObject(oVal) && !(oVal instanceof Element)) {
prev[key] = mergeDeep(pVal, oVal);
} else {
Object.assign(output, {
[key]: value,
});
prev[key] = oVal;
}
});
}
return output;
return prev;
}, {});
}
export function append(parent, child) {