UNPKG

@fanam-pkg/core-utils

Version:

Core Functions are managed here for quick web development

33 lines (27 loc) 961 B
import {FileType, IAttachment} from "../model" import {REG_VALID_IMAGE, REG_VALID_VIDEO, REG_VALID_DOCUMENT} from "../utils" export const useCommon = () => { const isDocumentReady = () => typeof window !== "undefined" const isValidInputByRegex = (input: string, regex: RegExp | string) => new RegExp(regex).test(input) const findFileType = ({url}: IAttachment) => { let type: FileType = null if (isValidInputByRegex(url, REG_VALID_IMAGE)) type = "image" else if (isValidInputByRegex(url, REG_VALID_VIDEO)) type = "video" else if (isValidInputByRegex(url, REG_VALID_DOCUMENT)) type = "document" return type } const getFileType = (base64: string) => { let mime = "" if (base64) { const arr = base64.split(",") mime = arr[0].match(/:(.*?);/)?.[1] || "" } return mime } return { isDocumentReady, findFileType, isValidInputByRegex, getFileType, } }