UNPKG

@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.23 kB
var t,e;t=this,e=()=>(()=>{"use strict";var t={d:(e,r)=>{for(var n in r){t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})}},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 r(t){switch(typeof t){case"boolean":return t;case"string":return"false"!==t.toLowerCase()&&"0"!==t;case"number":return 0!==t;default:return Boolean(t)}}function n(t){const e=a(t);return null!==e?e:t}function o(t,e=null){return null!==a(t)||"boolean"==typeof t?t:s(t)?e:t}t.r(e),t.d(e,{addQuotes:()=>p,cleanStr:()=>g,convertKeysToSymbols:()=>y,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 r="",n=0;n<t/3+1;n++){r+=e()}return r.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 a(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 l(t,e){if(null===t){return e?null===e||"null"===e:"null"}let r;switch(typeof t){case"number":case"string":case"boolean":case"undefined":case"bigint":case"symbol":case"function":r=typeof t;break;case"object":r=Array.isArray(t)?"array":"object";break;default:r="unknown"}if(e){if(e.includes("|")){for(let t of e.split("|")){if(r===t){return t}}return!1}return e===r}return r}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 g(t,...e){if(t){return"string"!=typeof t?t:e.reduce((t,e)=>{const r=e instanceof RegExp?e:new RegExp(E(e));return t.replace(r,"")},t).trim()}}function y(t){if(null===o(t,null)){return{}}const e={},r={};for(const n in t){const o=Symbol(n);e[o]=t[n],r[n]=o}return e.keyToSymbolMap=r,e}function d(t,e,r){let n=!1;const o=t.map(t=>{if(Array.isArray(t)){const o=d(t,e,r);return o?(n=!0,o):t}return t===e?(n=!0,Array.isArray(r)?r.map(t=>Array.isArray(t)?t:t.trim()):r):t});return n?o:null}function b(t,e="[",r="]"){if("string"!=typeof t){return t}const n=t.lastIndexOf(e);if(-1===n){return null}const o=t.substring(n),u=o.indexOf(r);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(l(t,"object")||l(t,"array")||!t){return t}const e=t=>$(t,"{","}"),r=t=>$(t,"[","]"),u=(e(t)?"object":null)||(r(t)?"array":null);if(!u){return t}const i={},s=t=>{if(!t){return}let n=(t=>{const e=b(t,"{","}"),r=b(t,"[","]");return t.indexOf(e)>t.indexOf(r)?e||null:r||null})(f(t));return n?(t=((t,n)=>{let o=`__${c()}__`,u=(e(n)?"object":null)||(r(n)?"array":null)||"string";return t=t.replace(n,o),i[o]={type:u,matched:n},t})(t,n),s(t)||t):void 0},a=(t,e)=>{t=f(t);let r="object"===e?{}:[];return S(t).forEach((t,u)=>{const c=t.includes(":")&&"object"===e,s=c?S(t,":"):[],l=O(o(s[0],u));if((t=c?s[1]:t)in i){const e=i[t];t=a(e.matched,e.type)}t=n(O(t)),"object"===e?r[l]=t:r.push(t)}),r};return a(s(t)||t,u)}function w(t){const e=t;if(!o(e)){return null}const r=(t=null,e=null)=>({type:t,directive:e}),n=/^\[((.|\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 a=typeof e;if("object"===a||"array"===a){return r(a,e)}switch(!0){case!!e.match(n):a="array";break;case!!e.match(u):a="object";break;case!!e.match(i):const t=e.split("(")[0].trim();return r("idOrClassWithDirective",{[t]:x(e,"(",")")});case!!e.match(c):case!!e.match(s):a="dotObject";break;default:return r("string",e)}if("array"===a||"object"===a){let t=m(e);try{return r(a,JSON.parse(t))}catch(t){}return r(a,j(t))}if("dotObject"===a){let t,n,o;const u={};return S(e,"&&").forEach(e=>{e.match(s)?(t=x(e,"](",")"),n=x(e,"[","]"),o=e.split("[")[0].trim()):(t=x(e,"(",")"),e=e.replace(A(e,"(",")"),""),[o,n]=S(e,".")),t=j(t),u[o]||(u[o]={}),S(n,"|").forEach(e=>{u[o][e]=t})}),r("dotObject",u)}}function A(t,e,r,n=!1){if("string"!=typeof t){return t}e=E(e),r=E(r);let o=new RegExp(_(e,r),"gm");const u=t.match(o);return u?n?u:u[0]:null}function S(t,e=","){if("string"!=typeof t){return t}if(s(t)){return[]}let r=(t=g(t)).split(e).map(t=>g(t));return 1===r.length&&""===r[0]?[t]:r}function x(t,e,r,n=!1){if("string"!=typeof t){return t}const o=A(t,e,r,n)??(n?[]:t);return n?o.map(t=>g(t,e,r)):g(o,e,r)}function O(t){return"string"!=typeof t?t:t.replace(/`|'|"/g,"")}function $(t,e=null,r=null){return(!e||t.startsWith(e))&&(!r||t.endsWith(r))}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,r)=>{t instanceof RegExp&&(t=t.source),0===r?e=t:e+=`((.|\n)*?)${t}`}),e}function v(t,e=!1,n=!1){if("string"!=typeof t){return t}if(!t){return null}e=r(e),n=r(n);let o=t.replace(/([.+?^${}()|\[\]\/\\])/g,"\\$&"),u=e?"^":"",i=n?"$":"";return o=o.replace(/\*\*/g,"[_g_]").replace(/\*/g,"(.*?)").replace(/\[_g_\]/g,".*"),`${u}${o}${i}`}function k(t,e,r=!1,n=!1){if(!t||!e){return null}const u=new RegExp(v(t,r,n));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();