yanzhen-design-vue
Version:
38 lines (37 loc) • 916 B
JavaScript
const typeValidator = (data) => {
const dataType = Object.prototype.toString.call(data);
return dataType;
};
const jsonParse = (origin = "", targetType) => {
let targetValue;
const dataType = typeValidator(targetType);
const originDataType = typeValidator(origin);
try {
targetValue = JSON.parse(origin);
} catch {
if (dataType === originDataType) {
targetValue = origin;
} else {
switch (dataType) {
case "[object Array]":
targetValue = origin ? [origin] : [];
break;
case "[object Object]":
targetValue = {};
break;
default:
targetValue = origin;
break;
}
}
}
return targetValue;
};
const getFileName = (filePath) => {
const lastSlashIndex = filePath.lastIndexOf("/");
return decodeURIComponent(filePath.slice(lastSlashIndex + 1));
};
export {
getFileName,
jsonParse
};