UNPKG

cg-utilities

Version:

This is a simple utility package

2 lines (1 loc) 10 kB
(function(u,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("currency.js")):typeof define=="function"&&define.amd?define(["exports","currency.js"],d):(u=typeof globalThis<"u"?globalThis:u||self,d(u["cg-utils"]=u["cg-utils"]||{},u.currencyjs))})(this,function(u,d){"use strict";function m(e,t){return Array.isArray(e)?e.reduce((n,o)=>d(n,{precision:4}).add(o).value,0):t===void 0?e:d(e,{precision:4}).add(t).value}function y(e,t){if(Array.isArray(e)){let n=0;return e.forEach(o=>{n?n=d(n,{precision:4}).subtract(o).value:n=o}),n}return t===void 0?e:d(e,{precision:4}).subtract(t).value}function p(e,t){if(Array.isArray(e)){let n=0;return e.forEach(o=>{n?n=d(n,{precision:4}).multiply(o).value:n=o}),n}return t===void 0?e:d(e,{precision:4}).multiply(t).value}function w(e,t){if(Array.isArray(e)){let n=0;return e.forEach(o=>{n?n=d(n,{precision:4}).divide(o).value:n=o}),n}return t===void 0?e:d(e,{precision:4}).divide(t).value}const s={DELTA:2654435769,base64EncodeChars:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(""),base64DecodeChars:[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1],btoa(e){let t,n,o,r,i,F,c;for(n=o=0,r=e.length,i=r%3,r=r-i,F=r/3<<2,i>0&&(F+=4),t=new Array(F);n<r;)c=e.charCodeAt(n++)<<16|e.charCodeAt(n++)<<8|e.charCodeAt(n++),t[o++]=s.base64EncodeChars[c>>18]+s.base64EncodeChars[c>>12&63]+s.base64EncodeChars[c>>6&63]+s.base64EncodeChars[c&63];return i===1?(c=e.charCodeAt(n++),t[o++]=`${s.base64EncodeChars[c>>2]+s.base64EncodeChars[(c&3)<<4]}==`):i===2&&(c=e.charCodeAt(n++)<<8|e.charCodeAt(n++),t[o++]=`${s.base64EncodeChars[c>>10]+s.base64EncodeChars[c>>4&63]+s.base64EncodeChars[(c&15)<<2]}=`),t.join("")},atob(e){let t,n,o,r,i,F,c,a,l,h;if(c=e.length,c%4!==0||/[^ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\+\/\=]/.test(e))return"";for(e.charAt(c-2)==="="?a=1:e.charAt(c-1)==="="?a=2:a=0,l=c,a>0&&(l-=4),l=(l>>2)*3+a,h=new Array(l),i=F=0;i<c&&(t=s.base64DecodeChars[e.charCodeAt(i++)],!(t===-1||(n=s.base64DecodeChars[e.charCodeAt(i++)],n===-1)||(h[F++]=String.fromCharCode(t<<2|(n&48)>>4),o=s.base64DecodeChars[e.charCodeAt(i++)],o===-1)||(h[F++]=String.fromCharCode((n&15)<<4|(o&60)>>2),r=s.base64DecodeChars[e.charCodeAt(i++)],r===-1)));)h[F++]=String.fromCharCode((o&3)<<6|r);return h.join("")},toBinaryString(e,t){const n=e.length;let o=n<<2;if(t){const i=e[n-1];if(o-=4,i<o-3||i>o)return null;o=i}for(let i=0;i<n;i++)e[i]=String.fromCharCode(e[i]&255,e[i]>>>8&255,e[i]>>>16&255,e[i]>>>24&255);const r=e.join("");return t?r.substring(0,o):r},toUint32Array(e,t){const n=e.length;let o=n>>2;n&3&&++o;let r;t?(r=new Array(o+1),r[o]=n):r=new Array(o);for(let i=0;i<n;++i)r[i>>2]|=e.charCodeAt(i)<<((i&3)<<3);return r},int32(e){return e&4294967295},mx(e,t,n,o,r,i){return(n>>>5^t<<2)+(t>>>3^n<<4)^(e^t)+(i[o&3^r]^n)},fixk(e){return e.length<4&&(e.length=4),e},encryptUint32Array(e,t){const n=e.length,o=n-1;let r,i,F,c,a,l;for(i=e[o],F=0,l=Math.floor(6+52/n)|0;l>0;--l){for(F=s.int32(F+s.DELTA),c=F>>>2&3,a=0;a<o;++a)r=e[a+1],i=e[a]=s.int32(e[a]+s.mx(F,r,i,a,c,t));r=e[0],i=e[o]=s.int32(e[o]+s.mx(F,r,i,o,c,t))}return e},decryptUint32Array(e,t){const n=e.length,o=n-1;let r,i,F,c,a,l;for(r=e[0],l=Math.floor(6+52/n),F=s.int32(l*s.DELTA);F!==0;F=s.int32(F-s.DELTA)){for(c=F>>>2&3,a=o;a>0;--a)i=e[a-1],r=e[a]=s.int32(e[a]-s.mx(F,r,i,a,c,t));i=e[o],r=e[0]=s.int32(e[0]-s.mx(F,r,i,0,c,t))}return e},utf8Encode(e){if(/^[\x00-\x7F]*$/.test(e))return e;const t=[],n=e.length;for(let o=0,r=0;o<n;++o,++r){const i=e.charCodeAt(o);if(i<128)t[r]=e.charAt(o);else if(i<2048)t[r]=String.fromCharCode(192|i>>6,128|i&63);else if(i<55296||i>57343)t[r]=String.fromCharCode(224|i>>12,128|i>>6&63,128|i&63);else{if(o+1<n){const F=e.charCodeAt(o+1);if(i<56320&&F>=56320&&F<=57343){const c=((i&1023)<<10|F&1023)+65536;t[r]=String.fromCharCode(240|c>>18&63,128|c>>12&63,128|c>>6&63,128|c&63),++o;continue}}throw new Error("Malformed string")}}return t.join("")},utf8DecodeShortString(e,t){const n=new Array(t);let o=0,r=0;for(let i=e.length;o<t&&r<i;o++){const F=e.charCodeAt(r++);switch(F>>4){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:n[o]=F;break;case 12:case 13:if(r<i)n[o]=(F&31)<<6|e.charCodeAt(r++)&63;else throw new Error("Unfinished UTF-8 octet sequence");break;case 14:if(r+1<i)n[o]=(F&15)<<12|(e.charCodeAt(r++)&63)<<6|e.charCodeAt(r++)&63;else throw new Error("Unfinished UTF-8 octet sequence");break;case 15:if(r+2<i){const c=((F&7)<<18|(e.charCodeAt(r++)&63)<<12|(e.charCodeAt(r++)&63)<<6|e.charCodeAt(r++)&63)-65536;if(c>=0&&c<=1048575)n[o++]=c>>10&1023|55296,n[o]=c&1023|56320;else throw new Error(`Character outside valid Unicode range: 0x${c.toString(16)}`)}else throw new Error("Unfinished UTF-8 octet sequence");break;default:throw new Error(`Bad UTF-8 encoding 0x${F.toString(16)}`)}}return o<t&&(n.length=o),String.fromCharCode.apply(String,n)},utf8DecodeLongString(e,t){const n=[],o=new Array(32768);let r=0,i=0;for(let F=e.length;r<t&&i<F;r++){const c=e.charCodeAt(i++);switch(c>>4){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:o[r]=c;break;case 12:case 13:if(i<F)o[r]=(c&31)<<6|e.charCodeAt(i++)&63;else throw new Error("Unfinished UTF-8 octet sequence");break;case 14:if(i+1<F)o[r]=(c&15)<<12|(e.charCodeAt(i++)&63)<<6|e.charCodeAt(i++)&63;else throw new Error("Unfinished UTF-8 octet sequence");break;case 15:if(i+2<F){const a=((c&7)<<18|(e.charCodeAt(i++)&63)<<12|(e.charCodeAt(i++)&63)<<6|e.charCodeAt(i++)&63)-65536;if(a>=0&&a<=1048575)o[r++]=a>>10&1023|55296,o[r]=a&1023|56320;else throw new Error(`Character outside valid Unicode range: 0x${a.toString(16)}`)}else throw new Error("Unfinished UTF-8 octet sequence");break;default:throw new Error(`Bad UTF-8 encoding 0x${c.toString(16)}`)}if(r>=32767-1){const a=r+1;o.length=a,n[n.length]=String.fromCharCode.apply(String,o),t-=a,r=-1}}return r>0&&(o.length=r,n[n.length]=String.fromCharCode.apply(String,o)),n.join("")},utf8Decode(e,t){return(t==null||t<0)&&(t=e.length),t===0?"":/^[\x00-\x7F]*$/.test(e)||!/^[\x00-\xFF]*$/.test(e)?t===e.length?e:e.substr(0,t):t<32767?s.utf8DecodeShortString(e,t):s.utf8DecodeLongString(e,t)},encrypt(e,t){return e==null||e.length===0?e:(e=s.utf8Encode(e),t=s.utf8Encode(t),s.toBinaryString(s.encryptUint32Array(s.toUint32Array(e,!0),s.fixk(s.toUint32Array(t,!1))),!1))},decrypt(e,t){return e==null||e.length===0?e:(t=s.utf8Encode(t),s.utf8Decode(s.toBinaryString(s.decryptUint32Array(s.toUint32Array(e,!1),s.fixk(s.toUint32Array(t,!1))),!0)))}};function g(e,t){return s.btoa(s.encrypt(e,t))}function E(e,t){return e==null||e.length===0?e:s.decrypt(s.atob(e),t)}function f(e={},t){const n=[];for(const i in e){const F=e[i];F!=null&&n.push(`${i}=${encodeURIComponent(F)}`)}const o=t.value;t.showConsole&&console.log("加密前",n.join("&"));const r=g(n.join("&"),o);return t.showConsole&&console.log("加密后",r),r}function S(e,t){const n=E(e,t.value);return JSON.parse(n)}function x(e,t){let n,o,r,i,F,c,a,l;for(n=e.length&3,o=e.length-n,r=t,F=3432918353,c=461845907,l=0;l<o;)a=e.charCodeAt(l)&255|(e.charCodeAt(++l)&255)<<8|(e.charCodeAt(++l)&255)<<16|(e.charCodeAt(++l)&255)<<24,++l,a=(a&65535)*F+(((a>>>16)*F&65535)<<16)&4294967295,a=a<<15|a>>>17,a=(a&65535)*c+(((a>>>16)*c&65535)<<16)&4294967295,r^=a,r=r<<13|r>>>19,i=(r&65535)*5+(((r>>>16)*5&65535)<<16)&4294967295,r=(i&65535)+27492+(((i>>>16)+58964&65535)<<16);switch(a=0,n){case 3:a^=(e.charCodeAt(l+2)&255)<<16;case 2:a^=(e.charCodeAt(l+1)&255)<<8;case 1:a^=e.charCodeAt(l)&255,a=(a&65535)*F+(((a>>>16)*F&65535)<<16)&4294967295,a=a<<15|a>>>17,a=(a&65535)*c+(((a>>>16)*c&65535)<<16)&4294967295,r^=a}return r^=e.length,r^=r>>>16,r=(r&65535)*2246822507+(((r>>>16)*2246822507&65535)<<16)&4294967295,r^=r>>>13,r=(r&65535)*3266489909+(((r>>>16)*3266489909&65535)<<16)&4294967295,r^=r>>>16,r>>>0}function T(e,t,n,o,r){const i=o;let F="",c="";return e.toLocaleString()==="get"?(F=n.lastIndexOf(".json")>-1?n:C(t)?`/${n}&${C(t)}`:`/${n}`,F.includes("//")&&(F=F.replace("//","/")),c=r.value+i+F):e.toLocaleString()==="post"&&(F=`/${n}`,F.includes("//")&&(F=F.replace("//","/")),c=f(t,r)+r.value+i+F),c+="1.0.0",r.showConsole&&(console.groupCollapsed(`🔑🔑签名参数--${n}🔑🔑`),console.log("nonce加密前的字符串",c),console.log(`nonce------- ${r.murmurhash_key||36}`,x(c,r.murmurhash_key||36)),console.groupEnd()),x(c,r.murmurhash_key||36)}function C(e={}){const t=[];for(const n in e){const o=e[n];o!=null&&t.push(`${n}=${o}`)}return t.join("&")}function A(e){const t=e||location.origin+"/date";return new Promise(n=>{const o=new XMLHttpRequest;o.open("head",t,!0),o.send(null),o.timeout=15e3,o.onreadystatechange=()=>{if(o.readyState===4){const r=o.getResponseHeader("Date"),i=new Date(r),F=new Date().getTime(),c=i.getTime()-F,a=new Date(new Date().getTime()+c);localStorage.setItem("__serverTime",a.toUTCString()),n(a.toUTCString())}}})}function D(e){return new Promise(t=>{const n=localStorage.getItem("__serverTime");n?t(n):A(e).then(o=>{t(o)})})}function U(e){if(e===void 0)return{};for(const t in e)(e[t]===""||e[t]===void 0||e[t]===null)&&delete e[t];return e}async function $(e,t){const n=new Date().getTime();b(e);const o=t["X-Ca-Timestamp"],r=T(e.method,U(e.data||e.params),e.url,o,t);return e.headers["request-startTime"]=n,e.headers.d=t.d,e.headers.v="1.0.0",e.headers.u=performance.now(),e.headers.lang="zh-CN",e.headers["X-Ca-Timestamp"]=o,e.headers["X-Ca-Nonce"]=r,(e.method==="post"||e.method==="put")&&(e.data=f(e.data,t)),{}}function b(e){return e.url.indexOf("?")===-1?e.url+="?r="+new Date().getTime():e.url+="&r="+new Date().getTime(),e}u.add=m,u.decryptQuery=S,u.divide=w,u.getServerTime=D,u.multiply=p,u.setServerTime=A,u.setXxetaConfig=$,u.subtract=y,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});