-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils-full.min.js
1 lines (1 loc) · 12.3 KB
/
utils-full.min.js
1
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Utils=t()}(this,(function(){"use strict";function e(...e){console.error(...e)}function t(e){throw new Error(e)}var n=Object.freeze({__proto__:null,reportError:e,throwError:t});function r(e,t,n){if("string"!=typeof e)return e;let r=document;return null===t&&n?r=n:t&&t instanceof Node&&"querySelector"in t?r=t:n&&n instanceof Node&&"querySelector"in n&&(r=n),"all"===t?r.querySelectorAll(e):r.querySelector(e)}function o(e,t={},n=""){let r=document.createElement(e);for(let e in t)Object.prototype.hasOwnProperty.call(t,e)&&("textContent"===e||"innerText"===e?r.textContent=t[e]:r.setAttribute(e,t[e]));return n&&(r.textContent=n),r}var s=Object.freeze({__proto__:null,addClass:function(e,t){return e.classList.add(t),e},createElem:o,findChild:function(e,t){return e.querySelector(t)},findChilds:function(e,t,n=1/0){const r=[];return function e(o,s){s>n||Array.from(o.children).forEach((n=>{n.matches(t)&&r.push(n),e(n,s+1)}))}(e,0),r},findParent:function(e,t){return e.closest(t)},findParents:function(e,t,n=1/0){const r=[];let o=e.parentElement,s=0;for(;o&&s<n;)o.matches(t)&&r.push(o),o=o.parentElement,s++;return r},getElem:r,hasChild:function(e,t){return null!==e.querySelector(t)},hasClass:function(e,t){return e.classList.contains(t)},hasParent:function(e,t,n=1/0,r=!1){let o=e.parentElement,s=0;for(;o&&s<n;){if(o.matches(t))return!r||o;o=o.parentElement,s++}return!!r&&null},insertAfter:function(e,n){if("string"==typeof n){let e=o("div");e.innerHTML=n,(n=e.firstChild)||t("The new node (string) provided did not produce a valid DOM element.")}e.parentNode.insertBefore(n,e.nextSibling)},insertBefore:function(e,n){if("string"==typeof n){let e=o("div");e.innerHTML=n,(n=e.firstChild)||t("The new node (string) provided did not produce a valid DOM element.")}e.parentNode.insertBefore(n,e)},removeClass:function(e,t){return e.classList.remove(t),e},toggleClass:function(e,t,n){return e.classList.toggle(t,n),e}});let a="utils-style";const i={from:".utils",to:".utils-"};function c(e){return"object"==typeof e&&null!==e&&!p(e)}function l(e){return"function"==typeof e}function f(e){return"string"==typeof e}function u(e){return"number"==typeof e}function d(e){return"boolean"==typeof e}function p(e){return Array.isArray(e)}function h(e){return"number"!=typeof e&&("string"==typeof e&&0===e.length||(!(!p(e)||0!==e.length)||(!(!c(e)||0!==Object.keys(e).length)||!e)))}function m(e,...t){if(!t.length)return e;const n=t.shift();if(n)for(const t in n)if(Object.prototype.hasOwnProperty.call(n,t)){const r=n[t],o=t;c(r)||p(r)?(e[o]&&"object"==typeof e[o]||(e[o]=p(r)?[]:{}),m(e[o],r)):e[o]=r}return m(e,...t)}function y(e,...t){return t.forEach((t=>{t&&Object.keys(t).forEach((n=>{const r=n,o=t[r];c(o)&&"function"==typeof e[r]?.constructor&&o instanceof e[r].constructor?e[r]=Object.assign(Object.create(Object.getPrototypeOf(o),{}),o):e[r]=o}))})),e}function g(e){let t;if(p(e))t=e.map((e=>g(e)));else if(c(e)){t={...e};for(let e in t)t.hasOwnProperty(e)&&(t[e]=g(t[e]))}else t=e;return t}function b(e){if(c(e)||p(e)){const t=p(e)?[]:Object.create(Object.getPrototypeOf(e));for(const n in e)if(e.hasOwnProperty(n)){const r=e[n];t[n]=c(r)?b(r):p(r)?[...r]:r}return t}return e}function v(e,t){if(typeof e!=typeof t)return!1;if(null===e||null===t)return e===t;if("object"!=typeof e||"object"!=typeof t||null===e||null===t)return e===t;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(Array.isArray(e)&&Array.isArray(t))return e.length===t.length&&e.every(((e,n)=>v(e,t[n])));if(Array.isArray(e)||Array.isArray(t))return!1;if(e instanceof Set&&t instanceof Set){if(e.size!==t.size)return!1;for(const n of e)if(!t.has(n))return!1;return!0}if(e instanceof Map&&t instanceof Map){if(e.size!==t.size)return!1;for(const[n,r]of e)if(!v(r,t.get(n)))return!1;return!0}if(Object.getPrototypeOf(e)!==Object.getPrototypeOf(t))return!1;const n=Reflect.ownKeys(e),r=Reflect.ownKeys(t);if(n.length!==r.length)return!1;for(const r of n)if(!v(e[r],t[r]))return!1;return!0}function E(e,t){if(typeof e!=typeof t)return!1;if(null===e||null===t)return e===t;if(e===t)return!0;if("object"!=typeof e||"object"!=typeof t)return e===t;const n=Reflect.ownKeys(e),r=Reflect.ownKeys(t);if(n.length!==r.length)return!1;for(const r of n)if(e[r]!==t[r])return!1;return!0}function w(e){a=e}function S(e,t){i.from=e,i.to=t}function x(e,t=null){t=h(t)?"":t;let n=o("style");n.id=a+t,n.textContent="",document.head.append(n);let r=n.sheet;for(let n in e)e.hasOwnProperty(n)&&O(r,n,C(e[n]),t)}function C(e){let t="";for(let[n,r]of Object.entries(e))n=n.replace(/([A-Z])/g,(e=>`-${e[0].toLowerCase()}`)),t+=`${n}:${r};`;return t}function O(e,t,n,r=null){r=h(r)?"":r;let o=t.replace(i.from,i.to+r);e.insertRule(o+"{"+n+"}",0)}function j(e=null){const t=h(e)?"":e;let n=r("#"+a+t);n&&n.parentNode&&n.parentNode.removeChild(n)}function D(e=8){let t="";const n="abcdefghijklmnopqrstuvwxyz0123456789";for(let r=0;r<e;r++){t+=n[Math.floor(36*Math.random())]}return t}function L(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(e=>{const t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)}))}function P(e,t=window.location.href){const n=e.startsWith("#");let r;if(n)r=t.substring(t.indexOf("#")+1);else{r=t.includes("#")?t.substring(t.indexOf("?"),t.indexOf("#")):t.substring(t.indexOf("?"))}const o=new URLSearchParams(r),s=n?e.substring(1):e,a=o.get(s);return null===a?null:decodeURIComponent(a)}function R(e,t,n=!0){const r=new URL(e);for(const[e,o]of Object.entries(t)){const t=null===o?"":String(o);!n&&r.searchParams.has(e)||r.searchParams.set(e,t)}return r.toString()}function T(e,t,n){let r=encodeURIComponent(e)+"="+encodeURIComponent(t)+";";if((n=m({},{expires:new Date(Date.now()+864e5),path:"/",secure:!1,sameSite:"Lax"},n||{})).expires){let e="";e=n.expires instanceof Date?n.expires.toUTCString():new Date(String(n.expires)).toUTCString(),r+="expires="+e+";"}r+="path="+n.path+";",n.domain&&(r+="domain="+n.domain+";"),n.secure&&(r+="secure;"),r+="SameSite="+n.sameSite+";",document.cookie=r}var U=Object.freeze({__proto__:null,getCookie:function(e){const t=encodeURIComponent(e)+"=",n=document.cookie.split(";");for(let e=0;e<n.length;e++){let r=n[e];for(;" "===r.charAt(0);)r=r.substring(1,r.length);if(0===r.indexOf(t))return decodeURIComponent(r.substring(t.length,r.length))}return null},getLocalValue:function(t,n=!0){let r=window.localStorage.getItem(t);if(n)try{r=JSON.parse(r)}catch(t){e("Error while parsing stored json value: ",t)}return r},getSessionValue:function(t,n=!0){let r=window.sessionStorage.getItem(t);if(n)try{r=JSON.parse(r)}catch(t){e("Error while parsing stored json value: ",t)}return r},removeCookie:function(e){T(e,"",{expires:new Date(0)})},removeLocalValue:function(e){window.localStorage.removeItem(e)},removeSessionValue:function(e){window.sessionStorage.removeItem(e)},setCookie:T,setLocalValue:function(e,t,n=!0){n&&(t=JSON.stringify(t)),window.localStorage.setItem(e,t)},setSessionValue:function(e,t,n=!0){n&&(t=JSON.stringify(t)),window.sessionStorage.setItem(e,t)}});function F(e,t,n){return new CustomEvent(e,{detail:t,...n})}var _=Object.freeze({__proto__:null,addEventListener:function(e,t,n,r){e.addEventListener(t,n,r)},createEvent:F,dispatchEvent:function(n,r=document,o,s){try{if("string"==typeof n){let e=F(n,o,s);return r.dispatchEvent(e)}if(n instanceof Event)return r.dispatchEvent(n);t("Invalid event type")}catch(t){return e("Dispatch Event Error:",t),!1}},removeEventListener:function(e,t,n,r){e.removeEventListener(t,n,r)}});function A(e,t=new FormData){const{data:n,parentKey:r=""}=e;if(n instanceof FormData)n.forEach(((e,n)=>{t.append(n,e)}));else if(null!==n&&"object"==typeof n)if(n instanceof Blob||n instanceof File){const e=r||"file";t.append(e,n)}else Object.keys(n).forEach((e=>{const o=n[e],s=r?`${r}[${e}]`:e;null!==o&&"object"==typeof o?A({data:o,parentKey:s},t):null!==o&&t.append(s,String(o))}));else null!==n&&t.append(r,n);return t}function V(e,t=""){if(e instanceof FormData)return e;return A({data:e,parentKey:t})}function I(e){const t={};return e.forEach(((e,n)=>{t[n]="string"==typeof e||"boolean"==typeof e||"number"==typeof e||null===e?e:e.toString()})),t}function k(e){const t={};return e instanceof FormData?I(e):("object"==typeof e&&Object.entries(e).forEach((([e,n])=>{t[e]="string"==typeof n||"number"==typeof n||"boolean"==typeof n||null===n?n:JSON.stringify(n)})),t)}var N=Object.freeze({__proto__:null,appendFormData:A,bodyToURLParams:k,decodeFormData:function(e){const t={};return e.forEach(((e,n)=>{n in t?Array.isArray(t[n])?t[n].push(e):t[n]=[t[n],e]:t[n]=e})),t},encodeFormData:V,formDataToURLParams:I});async function q(e){const{url:t,method:n="GET",headers:r={},cache:o="no-cache",mode:s="cors",credentials:a="same-origin",body:i=null,beforeSend:c=null,success:l=null,error:f=null}=e;let u=t;const d={method:n,mode:s,headers:r instanceof Headers?r:new Headers(r),cache:o,credentials:a};if(null!==i&&"GET"===n.toUpperCase()){const e=k(i);u=R("string"==typeof t?t:t.toString(),e,!0)}else if(null!==i&&["PUT","POST","DELETE"].includes(n.toUpperCase())){let e=i;i instanceof FormData||(e=JSON.stringify(i),d.headers instanceof Headers||(d.headers=new Headers(d.headers)),d.headers.append("Content-Type","application/json")),d.body=e}let p;if("string"==typeof u||u instanceof URL)p=new Request(u,d);else{if(!(u instanceof Request))throw new Error("Invalid URL type");p=u}try{const e=await new Promise((e=>{c?.(),e(p)})),t=await fetch(e);if(!t.ok)throw new Error(`HTTP error! status: ${t.status}`);if("function"==typeof l){const e=t.clone(),n=await e.json();l?.(n)}return t}catch(e){const t=e instanceof Error?e:new Error(String(e));throw f?.(t),t}}async function z(e){const{url:t,data:n,method:r="POST",headers:o,cache:s,mode:a,credentials:i,success:c,error:l,beforeSend:f,encode:u=!0}=e,d={url:t,method:r,headers:o,cache:s,mode:a,credentials:i,body:u&&"GET"!==r.toUpperCase()?V(n):n,beforeSend:f,success:e=>{c?.(e)},error:e=>{l?.(e)}};return(await q(d)).json()}async function M(e){const{url:t,data:n,method:r="POST",headers:o,cache:s,mode:a,credentials:i,success:c,error:l,beforeSend:f}=e;return q({url:t,method:r,headers:o,cache:s,mode:a,credentials:i,body:V(n),beforeSend:f,success:e=>{c?.(e)},error:e=>{l?.(e)}}).then((()=>!0)).catch((()=>!1))}const B=z,H=M;var K=Object.freeze({__proto__:null,doFetch:q,fetchData:B,sendData:z,sendForm:H,sendFormData:M});class J{constructor(e){Object.assign(this,e)}static version="1.4.5";static utilsVersion="3.7.2";static stylesheetId=a;static replaceRule={from:i.from,to:i.to};static setStylesheetId=w;static setReplaceRule=S;static isObject=c;static isArray=p;static isFunction=l;static isString=f;static isNumber=u;static isBoolean=d;static isEmpty=h;static deepMerge=m;static shallowMerge=y;static deepClone=g;static shallowClone=b;static deepEqual=v;static shallowEqual=E;static injectStylesheet=x;static buildRules=C;static compatInsertRule=O;static removeStylesheet=j;static generateRandom=D;static generateUUID=L;static getUrlParam=P;static setUrlParam=R;static getElem=s.getElem;static createElem=s.createElem;static insertAfter=s.insertAfter;static insertBefore=s.insertBefore;static addClass=s.addClass;static removeClass=s.removeClass;static toggleClass=s.toggleClass;static hasClass=s.hasClass;static hasParent=s.hasParent;static findParent=s.findParent;static findParents=s.findParents;static hasChild=s.hasChild;static findChild=s.findChild;static findChilds=s.findChilds;static createEvent=_.createEvent;static dispatchEvent=_.dispatchEvent;static addEventListener=_.addEventListener;static removeEventListener=_.removeEventListener;static setLocalValue=U.setLocalValue;static getLocalValue=U.getLocalValue;static removeLocalValue=U.removeLocalValue;static setSessionValue=U.setSessionValue;static getSessionValue=U.getSessionValue;static removeSessionValue=U.removeSessionValue;static setCookie=U.setCookie;static getCookie=U.getCookie;static removeCookie=U.removeCookie;static doFetch=K.doFetch;static sendData=K.sendData;static sendFormData=K.sendFormData;static fetchData=K.fetchData;static sendForm=K.sendForm;static appendFormData=N.appendFormData;static encodeFormData=N.encodeFormData;static decodeFormData=N.decodeFormData;static formDataToURLParams=N.formDataToURLParams;static bodyToURLParams=N.bodyToURLParams;static reportError=n.reportError;static throwError=n.throwError}return Object.defineProperty(J,"version",{writable:!1,configurable:!0}),J}));