UNPKG

snoby-utils

Version:

A javascrpt utils.

3 lines (2 loc) 4.58 kB
(function(t,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("dayjs")):typeof define=="function"&&define.amd?define(["exports","dayjs"],s):(t=typeof globalThis<"u"?globalThis:t||self,s(t.SnobyUtils={},t.dayjs))})(this,function(t,s){"use strict";function M(n){const e=`sno-${n}`;return U(e)}function d(n,e,o,i){return e&&(n+=`-${e}`),o&&(n+=`__${o}`),i&&(n+=`--${i}`),n}function U(n){return{b:c=>d(n,c,"",""),e:c=>d(n,"",c,""),m:c=>d(n,"","",c),be:(c,f)=>d(n,c,f,""),bm:(c,f)=>d(n,c,"",f),bem:(c,f,x)=>d(n,c,f,x),em:(c,f)=>d(n,"",c,f),is:(c,f)=>`is-${f}`}}function u(n,e){return Object.prototype.toString.call(n)===`[object ${e}]`}function D(n){return u(n,"Function")}const b=n=>typeof n<"u",O=n=>!b(n),m=n=>n!==null&&u(n,"Object");function I(n){return u(n,"Date")}function E(n){return u(n,"Number")}function F(n){return u(n,"AsyncFunction")}function _(n){return u(n,"Promise")&&m(n)&&D(n.then)&&D(n.catch)}function R(n){return u(n,"String")}function p(n){return u(n,"Boolean")}function L(n){return n&&Array.isArray(n)}const T=()=>typeof window<"u",$=n=>typeof window<"u"&&u(n,"Window"),C=n=>m(n)&&!!n.tagName;function S(n){return n===null}function N(n){return O(n)||S(n)}const A=n=>/^#?([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/.test(n),H=n=>u(n,"Undefined"),P=(n,e,o=i=>i.toUpperCase())=>{const i={};for(let r in n)Object.prototype.hasOwnProperty.call(n,r)&&(i[o(r)]=n[r]);return{...e,...i}};function v(n){return m(n)?Object.keys(n):[]}function j(n){return m(n)?Object.values(n):[]}const V="YYYY-MM-DD HH:mm";function W(n){const e=s(n);return e.isValid()?e.hour()===0&&e.minute()===0:!1}function k(n){const e=s(n);return e.isValid()?e.hour()===24&&e.minute()===0:!1}function B(n,e=15){const o=s(n);if(!o.isValid())throw new Error("Invalid date format");const i=o.unix(),r=e*60,a=Math.round(i/r)*r;return s.unix(a).format(V)}function Y(n=24){return Array.from({length:n},(e,o)=>String(o).padStart(2,"0"))}function K(n=1440,e=15){const o=[];for(let i=0;i<n;i+=e){const r=Math.floor(i/60),a=i%60;o.push(`${String(r).padStart(2,"0")}:${String(a).padStart(2,"0")}`)}return o}function J(n,e){const o=Math.abs(s(n).valueOf()-s(e).valueOf());return Math.floor(o/(1e3*60*60*24))}function q(n,e){const r=s(n),a=s(e);if(r.isValid()||a.isValid())throw new Error("Invalid date format");const l=Math.abs(a.valueOf()-r.valueOf()),h=Math.floor(l/36e5),g=Math.floor(l%36e5/6e4);return{hours:h,minutes:g}}function z(n,e){(plus.downloader&&plus.downloader.createDownload(n,{filename:`download/${e}`},(i,r)=>{r===200?plus.nativeUI.toast(`文件已保存到: ${i.filename}`):(plus.nativeUI.alert(`下载失败,错误代码: ${r}`),w(n,e))})).start()}function w(n,e=""){const o=document.createElement("a");try{o.href=n,o.download=e,o.style.display="none",document.body.appendChild(o),o.click(),setTimeout(()=>{document.body.removeChild(o),n.startsWith("blob:")&&window.URL.revokeObjectURL(n)},100)}catch{alert("文件下载失败,请检查网络连接或尝试其他方式下载")}}function G(n,e="xlsx"){const o=n["content-disposition"];let i=`${new Date().valueOf()}.${e}`;if(o){const r=o.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);r&&r[1]&&(i=r[1].replace(/['"]/g,""),i.startsWith("UTF-8''")&&(i=decodeURIComponent(i.replace("UTF-8''",""))))}return decodeURIComponent(i)}const Q=(n,e)=>{if(!n||!e)return"";const o=new RegExp(`(^|\\?|&)${e}=([^&]*)(&|$)`),i=n.match(o);return i?decodeURIComponent(i[2]):""};function y(n,e="children"){return JSON.parse(JSON.stringify(n)).flatMap(i=>[i,...i[e]?y(i.children):[]])}function X(n=15){if(n<=0||n>60||60%n!==0)throw new Error("intervalMinutes must be a positive divisor of 60 (e.g., 1, 5, 10, 15, 20, 30, 60)");const e=[],o=24,i=60;for(let r=0;r<o;r++)for(let a=0;a<i;a+=n){const l=String(r).padStart(2,"0"),h=String(a).padStart(2,"0");e.push(`${l}:${h}`)}return e.push("24:00"),e}function Z(){return["testFunction"]}t.calcDurationDate=q,t.createNameSpace=M,t.dayDifference=J,t.findFileNameByContentDisposition=G,t.generateHourOptions=Y,t.generateTimeOptionsWithStep=K,t.getFlatMenuList=y,t.getParam=Q,t.is=u,t.is0000=W,t.is2400=k,t.isArray=L,t.isAsyncFunction=F,t.isBoolean=p,t.isClient=T,t.isDate=I,t.isDef=b,t.isElement=C,t.isFunction=D,t.isHexColor=A,t.isNull=S,t.isNullOrUnDef=N,t.isNumber=E,t.isObject=m,t.isPromise=_,t.isString=R,t.isUnDef=O,t.isUndefined=H,t.isWindow=$,t.mapToNearest15MinInterval=B,t.nativeDownload=z,t.objectKeys=v,t.objectValues=j,t.testFunction=Z,t.timeRangeForm24Hour=X,t.transformKeysAndMerge=P,t.webDownload=w,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}); //# sourceMappingURL=index.cjs.map