2 lines
12 KiB
JavaScript
2 lines
12 KiB
JavaScript
(function(i,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(i=typeof globalThis<"u"?globalThis:i||self,f(i.momentum={}))})(this,function(i){"use strict";var Et=Object.defineProperty;var yt=(i,f,m)=>f in i?Et(i,f,{enumerable:!0,configurable:!0,writable:!0,value:m}):i[f]=m;var d=(i,f,m)=>(yt(i,typeof f!="symbol"?f+"":f,m),m);function f(t,n=!1){if(t==null)throw new Error("Cannot clean a NULL value");return Array.isArray(t)?t=t.filter(e=>e!=null):Object.entries(t).forEach(([e,r])=>{(r===void 0||n||r===null)&&delete t[e]}),t}function m(t){return JSON.parse(JSON.stringify(t))}function C(t,n,e){if(!(t==null||!n))return n.split(/[.[\]]/g).filter(r=>r.length).reduce((r,o,c,s)=>{if((o[0]=='"'||o[0]=="'")&&(o=o.slice(1,-1)),!(r!=null&&r.hasOwnProperty(o))){if(e==null)return;r[o]={}}return e!==void 0&&c==s.length-1?r[o]=e:r[o]},t)}function R(t,n,e=!1){if(t==null)return e;if(Array.isArray(n))return n.findIndex((o,c)=>!R(t[c],n[c],e))==-1;const r=typeof n;return r!=typeof t?!1:r=="object"?Object.keys(n).find(o=>!R(t[o],n[o],e))==null:r=="function"?t.toString()==n.toString():t==n}function w(t,n){const e=typeof t,r=typeof n;return e!="object"||t==null||r!="object"||n==null?e=="function"&&r=="function"?t.toString()==n.toString():t===n:Object.keys(t).length!=Object.keys(n).length?!1:Object.keys(t).every(c=>w(t[c],n[c]))}function q(t,n){return t.indexOf(n)===-1&&t.push(n),t}function P(t,n){return k([...t.filter(e=>!n.includes(r=>w(e,r))),...n.filter(e=>!t.includes(r=>w(e,r)))])}function Y(t){return function(n,e){const r=C(n,t),o=C(e,t);return typeof r!="string"||typeof o!="string"?1:r.toLowerCase().localeCompare(o.toLowerCase())}}function j(t,n=[]){return t.forEach(e=>Array.isArray(e)?j(e,n):n.push(e)),n}function G(t,n=!1){return function(e,r){const o=C(e,t),c=C(r,t);return typeof o=="number"&&typeof c=="number"?(n?-1:1)*(o-c):o>c?n?-1:1:o<c?n?1:-1:0}}function W(t,n){return e=>w(e[t],n)}function k(t){for(let n=t.length-1;n>=0;n--)t.slice(0,n).find(e=>w(e,t[n]))&&t.splice(n,1);return t}function x(t){return Array.isArray(t)?t:[t]}class H{constructor(){d(this,"listeners",{})}emit(n){Object.values(this.listeners).forEach(e=>e(n))}listen(n,e){const r=e||n,o=typeof n=="string"?n:`_${Object.keys(this.listeners).length.toString()}`;return this.listeners[o]=r,()=>delete this.listeners[o]}once(n){const e=this.listen(r=>{n(r),e()})}}const g=class g{constructor(n,e={}){d(this,"interceptors",{});this.baseUrl=n,this.headers=e}static addInterceptor(n,e){const r=e||n,o=typeof n=="string"?n:`_${Object.keys(g.interceptors).length.toString()}`;return g.interceptors[o]=r,()=>delete g.interceptors[o]}addInterceptor(n,e){const r=e||n,o=typeof n=="string"?n:`_${Object.keys(this.interceptors).length.toString()}`;return this.interceptors[o]=r,()=>delete this.interceptors[o]}getInterceptors(){return[...Object.values(g.interceptors),...Object.values(this.interceptors)]}fetch(n,e,r={}){const o={"Content-Type":e&&!(e instanceof FormData)?"application/json":void 0,...g.headers,...this.headers,...r.headers};return Object.keys(o).forEach(c=>{o[c]||delete o[c]}),fetch(`${this.baseUrl}${n||""}`.replace(/([^:]\/)\/+/g,"$1"),{headers:o,method:r.method||(e?"POST":"GET"),body:o["Content-Type"].startsWith("application/json")&&e?JSON.stringify(e):e}).then(async c=>{for(let s of this.getInterceptors())await new Promise(S=>s(c,()=>S(null)));return c.headers["Content-Type"]&&c.headers.get("Content-Type").startsWith("application/json")?await c.json():c.headers["Content-Type"]&&c.headers.get("Content-Type").startsWith("text/plain")?await c.text():c})}delete(n,e){return this.fetch(n,null,{method:"delete",...e})}get(n,e){return this.fetch(n,null,{method:"get",...e})}patch(n,e,r){return this.fetch(e,n,{method:"patch",...r})}post(n,e,r){return this.fetch(e,n,{method:"post",...r})}put(n,e,r){return this.fetch(e,n,{method:"put",...r})}new(n,e){const r=new g(`${this.baseUrl}${n}`,{...this.headers,...e});return Object.entries(this.interceptors).map(([o,c])=>r.addInterceptor(o,c)),r}};d(g,"interceptors",{}),d(g,"headers",{});let T=g;T.addInterceptor((t,n)=>{if(t.status==200)return n();throw t.status==400?new I(t.statusText):t.status==401?new N(t.statusText):t.status==403?new $(t.statusText):t.status==404?new v(t.statusText):t.status==500?new O(t.statusText):new E(t.statusText,t.status)});class E extends Error{constructor(e,r){super(e);d(this,"_code");r!=null&&(this._code=r)}get code(){return this._code||this.constructor.code}set code(e){this._code=e}static from(e){const r=Number(e.statusCode)??Number(e.code),o=new this(e.message||e.toString());return Object.assign(o,{stack:e.stack,...e,code:r??void 0})}static instanceof(e){return e.constructor.code!=null}toString(){return this.message||super.toString()}}d(E,"code",500);class I extends E{constructor(n="Bad Request"){super(n)}static instanceof(n){return n.constructor.code==this.code}}d(I,"code",400);class N extends E{constructor(n="Unauthorized"){super(n)}static instanceof(n){return n.constructor.code==this.code}}d(N,"code",401);class $ extends E{constructor(n="Forbidden"){super(n)}static instanceof(n){return n.constructor.code==this.code}}d($,"code",403);class v extends E{constructor(n="Not Found"){super(n)}static instanceof(n){return n.constructor.code==this.code}}d(v,"code",404);class O extends E{constructor(n="Internal Server Error"){super(n)}static instanceof(n){return n.constructor.code==this.code}}d(O,"code",500);const A={CLEAR:"\x1B[0m",BRIGHT:"\x1B[1m",DIM:"\x1B[2m",UNDERSCORE:"\x1B[4m",BLINK:"\x1B[5m",REVERSE:"\x1B[7m",HIDDEN:"\x1B[8m"},B={BLACK:"\x1B[30m",RED:"\x1B[31m",GREEN:"\x1B[32m",YELLOW:"\x1B[33m",BLUE:"\x1B[34m",MAGENTA:"\x1B[35m",CYAN:"\x1B[36m",WHITE:"\x1B[37m",GREY:"\x1B[90m"},V={BLACK:"\x1B[40m",RED:"\x1B[41m",GREEN:"\x1B[42m",YELLOW:"\x1B[43m",BLUE:"\x1B[44m",MAGENTA:"\x1B[45m",CYAN:"\x1B[46m",WHITE:"\x1B[47m",GREY:"\x1B[100m"};class z{constructor(n){this.namespace=n}format(...n){return`${new Date().toISOString()} [${this.namespace}] ${n.join(" ")}`}debug(...n){console.log(B.MAGENTA+this.format(...n)+A.CLEAR)}error(...n){console.log(B.RED+this.format(...n)+A.CLEAR)}info(...n){console.log(B.CYAN+this.format(...n)+A.CLEAR)}log(...n){console.log(A.CLEAR+this.format(...n))}warn(...n){console.log(B.YELLOW+this.format(...n)+A.CLEAR)}verbose(...n){console.log(B.WHITE+this.format(...n)+A.CLEAR)}}function F(t,n){return t.length-t.replaceAll(n,"").length}function J(t){return Array(t).fill(null).map(()=>Math.round(Math.random()*15).toString(16)).join("")}const b="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",D="0123456789",U="~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/",K=b+D+U;function Z(t){const n=/(\+?1)?.*?(\d{3}).*?(\d{3}).*?(\d{4})/g.exec(t);if(!n)throw new Error(`Number cannot be parsed: ${t}`);return`${n[1]??""} (${n[2]}) ${n[3]}-${n[4]}`.trim()}function Q(t,n,e){return`${t.slice(0,e)}${n}${t.slice(e+1)}`}function X(t,n=K){return Array(t).fill(null).map(()=>{const e=~~(Math.random()*n.length);return n[e]}).join("")}function _(t,n=!1,e=!1,r=!1){if(!n&&!e&&!r)throw new Error("Must enable at least one: letters, numbers, symbols");return Array(t).fill(null).map(()=>{let o;do{const c=~~(Math.random()*3);n&&c==0?o=b[~~(Math.random()*b.length)]:e&&c==1?o=D[~~(Math.random()*D.length)]:r&&c==2&&(o=U[~~(Math.random()*U.length)])}while(!o);return o}).join("")}function tt(t,n){if(typeof n=="string"&&(n=new RegExp(n,"g")),!n.global)throw new TypeError("Regular expression must be global.");let e=[],r;for(;(r=n.exec(t))!==null;)e.push(r);return e}function M(t){var n=et(rt(ot(nt(t),8*t.length)));return n.toLowerCase()}function et(t){for(var n,e="0123456789ABCDEF",r="",o=0;o<t.length;o++)n=t.charCodeAt(o),r+=e.charAt(n>>>4&15)+e.charAt(15&n);return r}function nt(t){for(var n=Array(t.length>>2),e=0;e<n.length;e++)n[e]=0;for(e=0;e<8*t.length;e+=8)n[e>>5]|=(255&t.charCodeAt(e/8))<<e%32;return n}function rt(t){for(var n="",e=0;e<32*t.length;e+=8)n+=String.fromCharCode(t[e>>5]>>>e%32&255);return n}function ot(t,n){t[n>>5]|=128<<n%32,t[14+(n+64>>>9<<4)]=n;for(var e=1732584193,r=-271733879,o=-1732584194,c=271733878,s=0;s<t.length;s+=16){var L=e,S=r,dt=o,gt=c;r=h(r=h(r=h(r=h(r=l(r=l(r=l(r=l(r=a(r=a(r=a(r=a(r=u(r=u(r=u(r=u(r,o=u(o,c=u(c,e=u(e,r,o,c,t[s+0],7,-680876936),r,o,t[s+1],12,-389564586),e,r,t[s+2],17,606105819),c,e,t[s+3],22,-1044525330),o=u(o,c=u(c,e=u(e,r,o,c,t[s+4],7,-176418897),r,o,t[s+5],12,1200080426),e,r,t[s+6],17,-1473231341),c,e,t[s+7],22,-45705983),o=u(o,c=u(c,e=u(e,r,o,c,t[s+8],7,1770035416),r,o,t[s+9],12,-1958414417),e,r,t[s+10],17,-42063),c,e,t[s+11],22,-1990404162),o=u(o,c=u(c,e=u(e,r,o,c,t[s+12],7,1804603682),r,o,t[s+13],12,-40341101),e,r,t[s+14],17,-1502002290),c,e,t[s+15],22,1236535329),o=a(o,c=a(c,e=a(e,r,o,c,t[s+1],5,-165796510),r,o,t[s+6],9,-1069501632),e,r,t[s+11],14,643717713),c,e,t[s+0],20,-373897302),o=a(o,c=a(c,e=a(e,r,o,c,t[s+5],5,-701558691),r,o,t[s+10],9,38016083),e,r,t[s+15],14,-660478335),c,e,t[s+4],20,-405537848),o=a(o,c=a(c,e=a(e,r,o,c,t[s+9],5,568446438),r,o,t[s+14],9,-1019803690),e,r,t[s+3],14,-187363961),c,e,t[s+8],20,1163531501),o=a(o,c=a(c,e=a(e,r,o,c,t[s+13],5,-1444681467),r,o,t[s+2],9,-51403784),e,r,t[s+7],14,1735328473),c,e,t[s+12],20,-1926607734),o=l(o,c=l(c,e=l(e,r,o,c,t[s+5],4,-378558),r,o,t[s+8],11,-2022574463),e,r,t[s+11],16,1839030562),c,e,t[s+14],23,-35309556),o=l(o,c=l(c,e=l(e,r,o,c,t[s+1],4,-1530992060),r,o,t[s+4],11,1272893353),e,r,t[s+7],16,-155497632),c,e,t[s+10],23,-1094730640),o=l(o,c=l(c,e=l(e,r,o,c,t[s+13],4,681279174),r,o,t[s+0],11,-358537222),e,r,t[s+3],16,-722521979),c,e,t[s+6],23,76029189),o=l(o,c=l(c,e=l(e,r,o,c,t[s+9],4,-640364487),r,o,t[s+12],11,-421815835),e,r,t[s+15],16,530742520),c,e,t[s+2],23,-995338651),o=h(o,c=h(c,e=h(e,r,o,c,t[s+0],6,-198630844),r,o,t[s+7],10,1126891415),e,r,t[s+14],15,-1416354905),c,e,t[s+5],21,-57434055),o=h(o,c=h(c,e=h(e,r,o,c,t[s+12],6,1700485571),r,o,t[s+3],10,-1894986606),e,r,t[s+10],15,-1051523),c,e,t[s+1],21,-2054922799),o=h(o,c=h(c,e=h(e,r,o,c,t[s+8],6,1873313359),r,o,t[s+15],10,-30611744),e,r,t[s+6],15,-1560198380),c,e,t[s+13],21,1309151649),o=h(o,c=h(c,e=h(e,r,o,c,t[s+4],6,-145523070),r,o,t[s+11],10,-1120210379),e,r,t[s+2],15,718787259),c,e,t[s+9],21,-343485551),e=y(e,L),r=y(r,S),o=y(o,dt),c=y(c,gt)}return Array(e,r,o,c)}function p(t,n,e,r,o,c){return y(ct(y(y(n,t),y(r,c)),o),e)}function u(t,n,e,r,o,c,s){return p(n&e|~n&r,t,n,o,c,s)}function a(t,n,e,r,o,c,s){return p(n&r|e&~r,t,n,o,c,s)}function l(t,n,e,r,o,c,s){return p(n^e^r,t,n,o,c,s)}function h(t,n,e,r,o,c,s){return p(e^(n|~r),t,n,o,c,s)}function y(t,n){var e=(65535&t)+(65535&n);return(t>>16)+(n>>16)+(e>>16)<<16|65535&e}function ct(t,n){return t<<n|t>>>32-n}function st(t){return/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(t)}function it(t){return Object.entries(t).map(([n,e])=>encodeURIComponent(n)+"="+encodeURIComponent(e)).join("&")}function ut(t){return t?`https://www.gravatar.com/avatar/${M(t)}`:""}function at(t){const n=new RegExp("(?:(?<protocol>[\\w\\d]+)\\:\\/\\/)?(?:(?<user>.+)\\@)?(?<host>(?<domain>[^:\\/\\?#@\\n]+)(?:\\:(?<port>\\d*))?)(?<path>\\/.*?)?(?:\\?(?<query>.*?))?(?:#(?<fragment>.*?))?$","gm").exec(t),e=(n==null?void 0:n.groups)??{},r=e.domain.split(".");if(e.port!=null&&(e.port=Number(e.port)),r.length>2&&(e.domain=r.splice(-2,2).join("."),e.subdomain=r.join(".")),e.query){const o=e.query.split("&"),c={};o.forEach(s=>{const[L,S]=s.split("=");c[L]=S}),e.query=c}return e}function lt(t){return(t instanceof Date?t.getTime():t)-new Date().getTime()}function ht(t){return new Promise(n=>setTimeout(n,t))}function ft(t){const n=t instanceof Date?t:new Date(t);return new Intl.DateTimeFormat("en-us",{weekday:"long",month:"short",day:"numeric",hour:"numeric",minute:"numeric",hour12:!0}).format(n)}i.BadRequestError=I,i.CliBackground=V,i.CliEffects=A,i.CliForeground=B,i.CustomError=E,i.Emitter=H,i.ForbiddenError=$,i.InternalServerError=O,i.Logger=z,i.NotFoundError=v,i.UnauthorizedError=N,i.XHR=T,i.addUnique=q,i.arrayDiff=P,i.caseInsensitiveSort=Y,i.clean=f,i.countChars=F,i.createHex=J,i.deepCopy=m,i.dotNotation=C,i.findByProp=W,i.flattenArr=j,i.formEncode=it,i.formatDate=ft,i.formatPhoneNumber=Z,i.gravatar=ut,i.includes=R,i.insertAt=Q,i.isEqual=w,i.makeArray=x,i.makeUnique=k,i.matchAll=tt,i.md5=M,i.randomString=X,i.randomStringBuilder=_,i.sleep=ht,i.sortByProp=G,i.timeUntil=lt,i.urlParser=at,i.validateEmail=st,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
|