@knighttower/utility
Version:
UtilityJs is a utility library that provides a collection of utility functions for various tasks. The library is designed to be easy to use and covers the most common use cases.
1 lines • 6.31 kB
JavaScript
var t,e;t=this,e=()=>(()=>{"use strict";var t={d:(e,n)=>{for(var r in n){t.o(n,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:n[r]})}},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};function n(t){if("boolean"==typeof t){return t}if("string"==typeof t){const e=t.trim().toLowerCase();return!!["true","1","yes","on"].includes(e)||!["false","0","no","off",""].includes(e)&&Boolean(e)}return"number"==typeof t?0!==t:Boolean(t)}function r(t){const e=l(t);return null!==e?e:t}function o(t,e=null){return null!==l(t)||"boolean"==typeof t?t:s(t)?e:t}t.r(e),t.d(e,{addQuotes:()=>p,cleanStr:()=>y,convertKeysToSymbols:()=>g,findAndReplaceInArray:()=>d,findNested:()=>b,fixQuotes:()=>m,getArrObjFromString:()=>j,getChunks:()=>S,getDirectivesFromString:()=>w,getMatchBlock:()=>A,getMatchInBetween:()=>x,getObjectFromPath:()=>h,removeQuotes:()=>O,setExpString:()=>E,setLookUpExp:()=>_,setWildCardString:()=>v,startAndEndWith:()=>$,wildCardStringSearch:()=>k});const u=(t=20)=>{const e=()=>Math.random().toString(36).substring(2,15);t=t||40;for(var n="",r=0;r<t/3+1;r++){n+=e()}return n.substring(0,t)};function i(){return"id__"+u(8)+"__"+(new Date).getTime()}const c=i;function s(t){return null==t||("string"==typeof t||Array.isArray(t)?0===t.length:t instanceof Map||t instanceof Set?0===t.size:ArrayBuffer.isView(t)?0===t.byteLength:"object"==typeof t&&0===Object.keys(t).length)}function l(t){const e=typeof t;switch(t){case null:case void 0:case"":return null;case"0":case 0:return 0;default:if(!("number"!==e&&"string"!==e||"number"!=typeof t&&Number.isNaN(Number(t)))){return+t}}return null}function a(t,e){if(null===t){return e?null===e||"null"===e:"null"}let n;switch(typeof t){case"number":case"string":case"boolean":case"undefined":case"bigint":case"symbol":case"function":n=typeof t;break;case"object":n=Array.isArray(t)?"array":"object";break;default:n="unknown"}if(e){if(e.includes("|")){for(let t of e.split("|")){if(n===t){return t}}return!1}return e===n}return n}function f(t){const e=t.match(/^(\{.*\}|\[.*\])$/);return e?e[0].slice(1,-1).trim():t}function p(t,e='"'){return`${e}${t}${e}`}function y(t,...e){if(t){return"string"!=typeof t?t:e.reduce((t,e)=>{const n=e instanceof RegExp?e:new RegExp(E(e));return t.replace(n,"")},t).trim()}}function g(t){if(null===o(t,null)){return{}}const e={},n={};for(const r in t){const o=Symbol(r);e[o]=t[r],n[r]=o}return e.keyToSymbolMap=n,e}function d(t,e,n){let r=!1;const o=t.map(t=>{if(Array.isArray(t)){const o=d(t,e,n);return o?(r=!0,o):t}return t===e?(r=!0,Array.isArray(n)?n.map(t=>Array.isArray(t)?t:t.trim()):n):t});return r?o:null}function b(t,e="[",n="]"){if("string"!=typeof t){return t}const r=t.lastIndexOf(e);if(-1===r){return null}const o=t.substring(r),u=o.indexOf(n);return-1===u?null:o.substring(0,u+1)}function m(t,e='"'){return"string"!=typeof t?t:t.replace(/`|'|"/g,e)}function h(t,e=window){return t.split(".").reduce((t,e)=>t&&t[e],e)}function j(t){if(a(t,"object")||a(t,"array")||!t){return t}const e=t=>$(t,"{","}"),n=t=>$(t,"[","]"),u=(e(t)?"object":null)||(n(t)?"array":null);if(!u){return t}const i={},s=t=>{if(!t){return}let r=(t=>{const e=b(t,"{","}"),n=b(t,"[","]");return t.indexOf(e)>t.indexOf(n)?e||null:n||null})(f(t));return r?(t=((t,r)=>{let o=`__${c()}__`,u=(e(r)?"object":null)||(n(r)?"array":null)||"string";return t=t.replace(r,o),i[o]={type:u,matched:r},t})(t,r),s(t)||t):void 0},l=(t,e)=>{t=f(t);let n="object"===e?{}:[];return S(t).forEach((t,u)=>{const c=t.includes(":")&&"object"===e,s=c?S(t,":"):[],a=O(o(s[0],u));if((t=c?s[1]:t)in i){const e=i[t];t=l(e.matched,e.type)}t=r(O(t)),"object"===e?n[a]=t:n.push(t)}),n};return l(s(t)||t,u)}function w(t){const e=t;if(!o(e)){return null}const n=(t=null,e=null)=>({type:t,directive:e}),r=/^\[((.|\n)*?)\]$/gm,u=/^\{((.|\n)*?)\:((.|\n)*?)\}$/gm,i=/^([a-zA-Z]+)(\()(\.|\#)(.*)(\))/g,c=/([a-zA-Z]+)\.(.*?)\(((.|\n)*?)\)/gm,s=/([a-zA-Z]+)\[((.|\n)*?)\]\(((.|\n)*?)\)/gm;let l=typeof e;if("object"===l||"array"===l){return n(l,e)}switch(!0){case!!e.match(r):l="array";break;case!!e.match(u):l="object";break;case!!e.match(i):const t=e.split("(")[0].trim();return n("idOrClassWithDirective",{[t]:x(e,"(",")")});case!!e.match(c):case!!e.match(s):l="dotObject";break;default:return n("string",e)}if("array"===l||"object"===l){let t=m(e);try{return n(l,JSON.parse(t))}catch(t){}return n(l,j(t))}if("dotObject"===l){let t,r,o;const u={};return S(e,"&&").forEach(e=>{e.match(s)?(t=x(e,"](",")"),r=x(e,"[","]"),o=e.split("[")[0].trim()):(t=x(e,"(",")"),e=e.replace(A(e,"(",")"),""),[o,r]=S(e,".")),t=j(t),u[o]||(u[o]={}),S(r,"|").forEach(e=>{u[o][e]=t})}),n("dotObject",u)}}function A(t,e,n,r=!1){if("string"!=typeof t){return t}e=E(e),n=E(n);let o=new RegExp(_(e,n),"gm");const u=t.match(o);return u?r?u:u[0]:null}function S(t,e=","){if("string"!=typeof t){return t}if(s(t)){return[]}let n=(t=y(t)).split(e).map(t=>y(t));return 1===n.length&&""===n[0]?[t]:n}function x(t,e,n,r=!1){if("string"!=typeof t){return t}const o=A(t,e,n,r)??(r?[]:t);return r?o.map(t=>y(t,e,n)):y(o,e,n)}function O(t){return"string"!=typeof t?t:t.replace(/`|'|"/g,"")}function $(t,e=null,n=null){return(!e||t.startsWith(e))&&(!n||t.endsWith(n))}function E(t){return t instanceof RegExp?t:t.split("").map(t=>["$","^",".","*","+","?","(",")","[","]","{","}","|","\\"].includes(t)?`\\${t}`:t).join("")}function _(...t){if(t.length<2){throw new Error("You need to pass at least two arguments")}let e="";return t.forEach((t,n)=>{t instanceof RegExp&&(t=t.source),0===n?e=t:e+=`((.|\n)*?)${t}`}),e}function v(t,e=!1,r=!1){if("string"!=typeof t){return t}if(!t){return null}e=n(e),r=n(r);let o=t.replace(/([.+?^${}()|\[\]\/\\])/g,"\\$&"),u=e?"^":"",i=r?"$":"";return o=o.replace(/\*\*/g,"[_g_]").replace(/\*/g,"(.*?)").replace(/\[_g_\]/g,".*"),`${u}${o}${i}`}function k(t,e,n=!1,r=!1){if(!t||!e){return null}const u=new RegExp(v(t,n,r));if("string"==typeof e){return o(e.match(u))}let i=[];return i=e.filter(t=>u.test(t)),o(i)}return e})(),"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("helpers",[],e):"object"==typeof exports?exports.helpers=e():t.helpers=e();