kwikid-components-react
Version:
KwikID's Component Library in React
73 lines (68 loc) • 2.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isValidDocumentUrl = exports.isValidBase64Image = exports.formatData = exports.fixImageFormat = exports.assignTypeToValue = void 0;
var _kwikidToolkit = require("kwikid-toolkit");
var _LabelValuePairs = require("./LabelValuePairs.definition");
const formatData = data => {
const formattedData = [];
for (const key in data) {
if ((0, _kwikidToolkit.checkObjectKeyExists)(data, key)) {
if (!(0, _kwikidToolkit.isObject)(data[key])) {
formattedData.push({
key,
value: data[key]
});
} else if ((0, _kwikidToolkit.checkObjectKeyExists)(data[key], "key")) {
formattedData.push(data[key]);
}
}
}
return formattedData;
};
exports.formatData = formatData;
const assignTypeToValue = value => {
const stringifiedValue = value.toString();
return (0, _kwikidToolkit.isBoolean)(value) ? _LabelValuePairs.EKwikUILabelValuePairType.BOOLEAN : (0, _kwikidToolkit.isVideo)(stringifiedValue) ? _LabelValuePairs.EKwikUILabelValuePairType.VIDEO : (0, _kwikidToolkit.isPdf)(stringifiedValue) ? _LabelValuePairs.EKwikUILabelValuePairType.PDF : isValidBase64Image(stringifiedValue) || (0, _kwikidToolkit.isImage)(stringifiedValue) ? _LabelValuePairs.EKwikUILabelValuePairType.IMAGE : isValidDocumentUrl(stringifiedValue) ? _LabelValuePairs.EKwikUILabelValuePairType.DOC : "";
};
// TOOLKIT
exports.assignTypeToValue = assignTypeToValue;
const fixImageFormat = image => {
var _image;
image = image.toString();
if (!((_image = image) !== null && _image !== void 0 && _image.startsWith("data:image")) && isValidBase64Image(image)) {
image = "data:image/png;base64,".concat(image);
}
return image;
};
// TOOLKIT
exports.fixImageFormat = fixImageFormat;
const isValidBase64Image = url => {
if (url.startsWith("data:image/")) {
const base64Index = url.indexOf(";base64,");
let slicedBase64String = "";
if (base64Index !== -1) {
slicedBase64String = url.slice(base64Index);
}
}
const imageTypes = ["PNG", "JPG", "JPEG", "BMP", "GIF", "SVG"];
let binaryValue = "";
try {
binaryValue = atob(url);
} catch (error) {
console.log("Not a valid Base64 input: ", error);
}
return !!imageTypes.some(format => binaryValue.includes(format));
};
exports.isValidBase64Image = isValidBase64Image;
const isValidDocumentUrl = url => {
// Array of document formats
const documentFormats = ["doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"];
// Get the file extension from the URL
const urlParts = url.split(".");
const extension = urlParts[urlParts.length - 1].toLowerCase();
// Check if the extension exists in the documentFormats array
return documentFormats.includes(extension);
};
exports.isValidDocumentUrl = isValidDocumentUrl;