UNPKG

@motojs_sdk/utils

Version:
2 lines (1 loc) 2.54 kB
"use strict";const nativeTryCatch=(e,t)=>{try{e()}catch(r){console.error("\u51FA\u9519\u5566\uFF5E\uFF01",r),t&&t(r)}},on=(e,t,r,n=!1)=>{e.addEventListener(t,r,n)},off=(e,t,r,n=!1)=>{e.removeEventListener(t,r,n)},getLocationHref=()=>typeof document=="undefined"||document.location==null?"":document.location.href,getTimestamp=()=>Date.now(),interceptStr=(e,t)=>typeof e=="string"?e.slice(0,t)+(e.length>t?`;slice the first ${t} characters`:""):"",safeStringify=e=>{const t=new Set,r=JSON.stringify(e,function(n,o){return o in File?"File":t.has(o)?"Loop":(typeof o=="object"&&t.add(o),o)});return t.clear(),r},unknowtoString=e=>typeof e=="object"&&e!==null?safeStringify(e):String(e),replaceOld=(e,t,r,n)=>{if(e&&(Reflect.has(e,t)||n)){const o=e[t],s=r(o);typeof s=="function"&&(e[t]=s)}},htmlElementAsString=e=>{const t=e.tagName.toLowerCase();if(t==="body")return"";let r=e.classList.value;r=r!==""?` class="${r}"`:"";const n=e.id?` id="${e.id}"`:"",o=e.innerText;return`<${t}${n}${r!==""?r:""}>${o}</${t}>`},supportsHistory=()=>{const e=window.chrome,t=e&&e.app&&e.app.runtime,r="history"in window&&!!window.history.pushState&&!!window.history.replaceState;return!t&&r},targetHasUnknow=(e,t)=>Reflect.has(e,t)||t in e,f=e=>t=>{let r="",n=t;for(;n--;)r+=e[Math.random()*e.length|0];return r},nanoid=f("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"),l=(e,t)=>t===0?e:l(t,e%t),a=(...e)=>{if(!(e!=null&&e.length))return 1;if(e.length===1)return e[0];let t=e[0];for(let r=1;r<e.length;r++){const n=e[r];t=t*n/l(n,t)}return t},createFeId=()=>{const e=getTimestamp(),t=e.toString().slice(0,5).split("").reduce((i,c)=>i+parseInt(c),0),r=e.toString().slice(5,10).split("").reduce((i,c)=>i+parseInt(c),0),n=e.toString().slice(10,13).split("").reduce((i,c)=>i+parseInt(c),0),o=btoa(e.toString()),s=a(t,r,n).toString(),p=s.length,u=nanoid(10);return s+u.slice(p)+p+o},timeDifference=(e,t)=>{typeof e=="string"&&(e=Number(e)),typeof t=="string"&&(t=Number(t));const r=new Date(e).getTime(),n=new Date(t).getTime();return Math.abs(r-n)};exports.createFeId=createFeId,exports.getLocationHref=getLocationHref,exports.getTimestamp=getTimestamp,exports.htmlElementAsString=htmlElementAsString,exports.interceptStr=interceptStr,exports.nanoid=nanoid,exports.nativeTryCatch=nativeTryCatch,exports.off=off,exports.on=on,exports.replaceOld=replaceOld,exports.safeStringify=safeStringify,exports.supportsHistory=supportsHistory,exports.targetHasUnknow=targetHasUnknow,exports.timeDifference=timeDifference,exports.unknowtoString=unknowtoString;