diginext-utils
Version:
README.md
106 lines (105 loc) • 3.33 kB
JavaScript
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) => {
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 = currLocation.split("?")[1].split("&"), returnBool = true;
for (var i = 0; i < parArr.length; i++) {
var parr = parArr[i].split("=");
if (parr[0] == parameter) {
return decode ? decodeURIComponent(parr[1]) : parr[1];
returnBool = true;
}
else {
returnBool = false;
}
}
if (!returnBool)
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) => {
if (!url)
return "";
url = url.replace(/\\/g, "/");
const _url = decodeURIComponent(url);
const m = `${_url.toString().match(/(?:.*\/)?([^\/]+)/)}`;
if (m && m.split(",").length > 1) {
return m.split(",")[1].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;