always-helper
Version:
前端项目开发中,经常会用到一些工具函数的合集。
2 lines (1 loc) • 10.2 kB
JavaScript
import e from"js-cookie";import*as t from"dayjs";import n from"dom-to-image";var o,r=function(){if(navigator.cookieEnabled)return!0;document.cookie="cookietest=1";var e=-1!==document.cookie.indexOf("cookietest=");return document.cookie="cookietest=1 expires=Thu, 01-Jan-1970 00:00:01 GMT",e},i=function(t){return e.get(t)},u=function(t,n){e.remove(t,n)},c=function(t,n,o){e.set(t,n,o)},a=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD";return t().format(e)},l=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t(),n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1,o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"YYYY-MM-DD";return t(e).add(-1*n,"day").format(o)},d=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD";return l(t(),1,e)},f=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"YYYY-MM-DD",o=t().startOf("month").subtract(e,"month"),r=t().startOf("month").subtract(e,"month").endOf("month");return[o,r].map((function(e){return e.format(n)}))},s=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"YYYY-MM-DD",o=t().day(),r=t().subtract(o+7*e,"day"),i=t().subtract(o+7*(e-1)+1,"day");return[r,i].map((function(e){return e.format(n)}))},m=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"YYYY-MM-DD";return[l(t(),e,n),l(t(),1,n)]},h=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD";return f(1,e)},v=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD";return s(1,e)},g=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD";return f(0,e)},p=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD";return s(0,e)},y=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD";return f(-1,e)},Y=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD";return s(-1,e)},x=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t();return t(e).day()+1},w=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new Date,t=new Date(e.valueOf()),n=(e.getDay()+6)%7;t.setDate(t.getDate()-n+3);var o=new Date(t.getFullYear(),0,4),r=(Number(t)-Number(o))/864e5;return new Date(t.getFullYear(),0,1).getDay()<5?1+Math.ceil(r/7):Math.ceil(r/7)},M=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD";return[t().startOf("year"),t()].map((function(t){return t.format(e)}))},D=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t();return t(e).month()+1},b=function(e){return e instanceof Date&&!isNaN(e.valueOf())},S=[60,60,24,7,365/7/12,12],k=function(e,t,n){var o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"cn";return(o.toUpperCase()+"_RELATIVE")[t][n?0:1].replace("%s",e)},F=function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"cn";b(e)&&(e=new Date(e));var o=((t=(b(e)?t:new Date(t))||new Date)-e)/1e3,r=o>0,i=0;for(o=Math.abs(o);o>=S[i]&&i<S.length;)o/=S[i],i++;return(o=Number(o))>(0===(i*=2)?9:1)&&(i+=1),k(o,i,r,n)},q=function(e,t){var n;"string"==typeof e&&(e=document.querySelector(e)),null===(n=e)||void 0===n||n.classList.add(t)},O=function(e,t){var n;"string"==typeof e&&(e=document.querySelector(e)),null===(n=e)||void 0===n||n.classList.remove(t)},R=function(e,t){var n;return"string"==typeof e&&(e=document.querySelector(e)),null===(n=e)||void 0===n?void 0:n.classList.contains(t)},C=function(e,t){return"string"==typeof e&&(e=document.querySelector(e)),e.currentStyle?e.currentStyle[t]:window.getComputedStyle(e,null)[t]},E=function(e,t){if(!Array.isArray(t))return C(e,t);t.reduce((function(t,n){return t[n]=C(e,n),t}),{})};!function(e){e[e.append=0]="append",e[e.prepend=1]="prepend"}(o||(o={}));var I=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"append",n=document.createElement("style");n.setAttribute("type","text/css"),n.innerHTML=e,"append"===t?document.head.appendChild(n):document.head.insertBefore(n,document.head.childNodes[0])},L=function(e,t){for(var n in"string"==typeof e&&(e=document.querySelector(e)),t)t.hasOwnProperty(n)&&(e.style[n]=t[n])},A=function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"append",o=document.createElement("link");o.rel="stylesheet",o.type="text/css",t&&(o.onerror=function(){return t(!1)},o.onload=function(){return t(!0)}),o.href=e,"append"===n?document.head.appendChild(o):document.head.insertBefore(o,document.head.childNodes[0])},N=function(e,t){var n;"string"==typeof e&&(e=document.querySelector(e)),null===(n=e)||void 0===n||n.classList.toggle(t)},P=function(e,t,n){var o;"string"==typeof e&&(e=document.querySelector(e)),null===(o=e)||void 0===o||o.classList.replace(t,n)},T=function(e){var t=/[?&]([^=#]+)=([^&#]*)/g;e=e||window.location.href;for(var n,o={};null!==(n=t.exec(e));)o[decodeURIComponent(n[1])]=decodeURIComponent(n[2]);return o};function j(e){return(j="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var H=function e(t,n){var o=[];for(var r in t)if(t.hasOwnProperty(r)){var i=n?n+"["+r+"]":r,u=t[r];o.push(null!==u&&"object"===j(u)?e(u,i):encodeURIComponent(i)+"="+encodeURIComponent(u))}return o.join("&")},U=["时","大","地","为","子","中","你","说","生","国","年","着","就","那","和","诗","独","复","痛","消","社","算"],z=function(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:3,t="",n=0;n<e;)t+=U[Math.floor(Math.random()*U.length)],++n;return t},W=function(){return"#"+Math.floor(16777215*Math.random()).toString(16)},B=function(e,t){return e=Math.ceil(e),t=Math.floor(t),Math.floor(Math.random()*(t-e+1))+e},V=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:5,t=5,n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";"object"===j(e)&&(t=e.length||5,n=e.charset||"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");for(var o="",r=n.length,i=0;i<t;++i)o+=n.charAt(Math.floor(Math.random()*r));return o},X=function(){var e=(new Date).getTime(),t="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(t){var n=(e+16*Math.random())%16|0;return e=Math.floor(e/16),("x"===t?n:3&n|8).toString(16)}));return t},G={launchFullscreen:function(e){var t=!0;return e.requestFullscreen?e.requestFullscreen():e.mozRequestFullScreen?e.mozRequestFullScreen():e.msRequestFullscreen?e.msRequestFullscreen():e.webkitRequestFullscreen?e.webkitRequestFullScreen():t=!1,t},exitFullscreen:function(){var e=!0;return document.exitFullscreen?document.exitFullscreen():document.msExitFullscreen?document.msExitFullscreen():document.mozCancelFullScreen?document.mozCancelFullScreen():document.webkitExitFullscreen?document.webkitExitFullscreen():e=!1,e}},J=function(e){"string"==typeof e&&(e=document.querySelector(e)),window.getSelection().removeAllRanges();var t=e,n=document.createRange();n.selectNode(t),window.getSelection().addRange(n);var o=!1;return document.execCommand("copy")&&(o=!0),window.getSelection().removeAllRanges(),o},K=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2;"string"==typeof e&&(e=document.querySelector(e));var o={width:e.clientWidth*t,height:e.clientHeight*t,style:{transform:"scale(".concat(t,")"),transformOrigin:"top left"}};return new Promise((function(t,r){n.toBlob(e,o).then((function(e){try{var n=new ClipboardItem({"image/png":e});navigator.clipboard.write([n]).then((function(){return t()})).catch((function(e){return r(e.message||"暂不支持复制")}))}catch(e){r(e.message||"暂不支持复制")}})).catch((function(e){return r(e)}))}))},Q=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"element-image",o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:2,r={width:e.clientWidth*o,height:e.clientHeight*o,style:{transform:"scale(".concat(o,")"),transformOrigin:"top left"}};return new Promise((function(o,i){n.toPng(e,r).then((function(e){var n=document.createElement("a");n.download=t,n.href=e,n.click(),o(1)})).catch((function(e){return i(e)}))}))},Z=function(){I("html[theme='dark-mode'] {\n filter: invert(1) hue-rotate(180deg);\n }\n html[theme='dark-mode'] img{\n filter: invert(1) hue-rotate(180deg);\n }\n html {\n transition: color 300ms, background-color 300ms;\n }\n "),document.documentElement.setAttribute("theme","dark-mode")},_=function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(e){var n=e.offsetTop,o=e.offsetLeft,r=0,i=0;for(t&&(r=e.offsetWidth,i=e.offsetHeight);e.offsetParent;)n+=(e=e.offsetParent).offsetTop,o+=e.offsetLeft;return n>=window.pageYOffset&&o>=window.pageXOffset&&n+i<=window.pageYOffset+window.innerHeight&&o+r<=window.pageXOffset+window.innerWidth}},$=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"smooth";return document.body.scrollIntoView({behavior:e,block:"start"})},ee=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"smooth";return document.body.scrollIntoView({behavior:e,block:"end"})};export{q as addClass,l as beforeDay,m as beforeDays,f as beforeMonth,s as beforeWeek,r as checkCookie,J as copyElement2Clipboard,K as copyElementImage2Clipboard,g as currentMonth,p as currentWeek,Q as downloadElementImage,G as fullscreen,i as getCookie,w as getCurrentWeek,x as getDay,D as getMonth,E as getStyle,R as hasClass,Z as inDarkMode,I as insertCss,_ as inview,h as lastMonth,v as lastWeek,A as loadCss,y as nextMonth,Y as nextWeek,T as parseQueryString,z as randomCnChar,W as randomColor,B as randomInt,V as randomString,O as removeClass,u as removeCookie,P as replaceClass,ee as scrollBottom,$ as scrollTop,c as setCookie,L as setStyle,M as sofarYear,H as stringifyQueryString,F as timeAgo,a as today,N as toggleClass,X as uuid,d as yesterday};