UNPKG

diginext-utils

Version:
108 lines 3.76 kB
var urlRegex = /(https?:\/\/[^\s]+)/g; export const addQueryParam = (_url, key, value) => { _url += (_url.split("?")[1] ? "&" : "?") + `${key}=${value}`; return _url; }; export const getUrlParams = (parameter, staticURL, decode = true) => { var _a, _b, _c; if (typeof window == "undefined") return ""; staticURL = staticURL == undefined ? window.location.host : staticURL; var currLocation = staticURL.length > 0 ? staticURL : window.location.search; if (currLocation.split("?").length < 2) return ""; var parArr = ((_b = (_a = currLocation.split("?")) === null || _a === void 0 ? void 0 : _a[1]) === null || _b === void 0 ? void 0 : _b.split("&")) || [], returnBool = true; for (var i = 0; i < (parArr === null || parArr === void 0 ? void 0 : parArr.length); i++) { var parr = (_c = parArr[i]) === null || _c === void 0 ? void 0 : _c.split("="); if ((parr === null || parr === void 0 ? void 0 : parr[0]) == parameter) { return decode ? decodeURIComponent(parr === null || parr === void 0 ? void 0 : parr[1]) : parr[1]; } else { returnBool = false; } } if (!returnBool) return false; return false; }; export const isLink = (str) => { return urlRegex.test(str); }; export const getFileNameWithoutExtension = (url) => { var _a, _b; return ((_b = (_a = getFileNameWithExtension(url)) === null || _a === void 0 ? void 0 : _a.split(".")) === null || _b === void 0 ? void 0 : _b[0]) || ""; }; export const getFileNameWithExtension = (url) => { var _a, _b, _c; if (!url) return ""; url = url.replace(/\\/g, "/"); const _url = decodeURIComponent(url); const m = `${_url.toString().match(/(?:.*\/)?([^\/]+)/)}`; if (m && m.split(",").length > 1) { return ((_c = (_b = (_a = m === null || m === void 0 ? void 0 : m.split) === null || _a === void 0 ? void 0 : _a.call(m, ",")) === null || _b === void 0 ? void 0 : _b[1]) === null || _c === void 0 ? void 0 : _c.replace(/(\?.*)/, "")) || ""; } return ""; }; export const getFileExtension = (url) => { const arr = getFileNameWithExtension(url).split("."); if (arr.length > 1) return getFileNameWithExtension(url).split(".").pop(); return null; }; export const getExtensionFromMimeType = (mimeType) => { const mimeToExt = { "image/jpg": "jpg", "image/jpeg": "jpg", "image/png": "png", "image/gif": "gif", "image/bmp": "bmp", "image/webp": "webp", "image/tiff": "tif", "video/mp4": "mp4", "video/ogg": "ogg", "video/webm": "webm", "video/avi": "avi", "video/mkv": "mkv", "video/flv": "flv", "video/mov": "mov", "video/wmv": "wmv", "video/mpeg": "mpeg", // ... add other MIME types and their respective extensions as needed }; return mimeToExt[mimeType] || null; }; /** * * @param {string} url * @returns */ export const isImage = (url) => { if (!url) return false; const arr = [".png", ".jpg", ".jpeg", ".jpe", ".jif", ".jfif", ".gif", ".svg"]; const index = arr.findIndex((item) => { return url.indexOf(item) >= 0; }); return index >= 0; }; export const isImageByMimeType = (mimeType) => { return mimeType.startsWith("image/"); }; export const isVideoByMimeType = (mimeType) => { return mimeType.startsWith("video/"); }; const xurl = { getExtensionFromMimeType, addQueryParam, getUrlParams, isLink, getFileNameWithoutExtension, getFileNameWithExtension, getFileExtension, isImage, isImageByMimeType, isVideoByMimeType, }; export default xurl; //# sourceMappingURL=url.js.map