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