UNPKG

atools-js

Version:

现代 JavaScript 实用工具库🔧

18 lines (15 loc) 9.14 kB
!function(o,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((o="undefined"!=typeof globalThis?globalThis:o||self).atools={})}(this,(function(o){"use strict";var n=function(o){return Object.prototype.toString.call(o).slice(8,-1).toLowerCase()},t=function(o,t){return n(o)===t.toLowerCase()}; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function e(o,n,t,e){return new(t||(t=Promise))((function(r,c){function i(o){try{a(e.next(o))}catch(o){c(o)}}function u(o){try{a(e.throw(o))}catch(o){c(o)}}function a(o){var n;o.done?r(o.value):(n=o.value,n instanceof t?n:new t((function(o){o(n)}))).then(i,u)}a((e=e.apply(o,n||[])).next())}))}function r(o,n){var t,e,r,c,i={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return c={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function u(c){return function(u){return function(c){if(t)throw new TypeError("Generator is already executing.");for(;i;)try{if(t=1,e&&(r=2&c[0]?e.return:c[0]?e.throw||((r=e.return)&&r.call(e),0):e.next)&&!(r=r.call(e,c[1])).done)return r;switch(e=0,r&&(c=[2&c[0],r.value]),c[0]){case 0:case 1:r=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,e=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(r=i.trys,(r=r.length>0&&r[r.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!r||c[1]>r[0]&&c[1]<r[3])){i.label=c[1];break}if(6===c[0]&&i.label<r[1]){i.label=r[1],r=c;break}if(r&&i.label<r[2]){i.label=r[2],i.ops.push(c);break}r[2]&&i.ops.pop(),i.trys.pop();continue}c=n.call(o,i)}catch(o){c=[6,o],e=0}finally{t=r=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,u])}}}function c(o,n,t){if(t||2===arguments.length)for(var e,r=0,c=n.length;r<c;r++)!e&&r in n||(e||(e=Array.prototype.slice.call(n,0,r)),e[r]=n[r]);return o.concat(e||Array.prototype.slice.call(n))}function i(o){return i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(o){return typeof o}:function(o){return o&&"function"==typeof Symbol&&o.constructor===Symbol&&o!==Symbol.prototype?"symbol":typeof o},i(o)}var u="object"===("undefined"==typeof window?"undefined":i(window))&&"object"===("undefined"==typeof document?"undefined":i(document)),a=function(o){if(!u)throw new Error("Non-browser environment, unavailable 'getCookie'");if(!document.cookie)throw new Error("No Cookie Found");if(o){var n=new RegExp("(^| )".concat(o,"=([^;]*)(;|$)")),t=document.cookie.match(n);return t?t[2]:void 0}return document.cookie.split(";")},l={info:function(){for(var o=[],n=0;n<arguments.length;n++)o[n]=arguments[n];return console.log.apply(console,c(["%c%s","color: #9E9E9E"],o,!1))},error:function(){for(var o=[],n=0;n<arguments.length;n++)o[n]=arguments[n];return console.log.apply(console,c(["%c%s","color: #d81e06"],o,!1))},warn:function(){for(var o=[],n=0;n<arguments.length;n++)o[n]=arguments[n];return console.log.apply(console,c(["%c%s","color: #ffc107"],o,!1))},debug:function(){for(var o=[],n=0;n<arguments.length;n++)o[n]=arguments[n];return console.log.apply(console,c(["%c%s","color: #2196f3"],o,!1))},success:function(){for(var o=[],n=0;n<arguments.length;n++)o[n]=arguments[n];return console.log.apply(console,c(["%c%s","color: #4caf50"],o,!1))},color:function(o){return function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];return console.log.apply(console,c(["%c%s","color: ".concat(o)],n,!1))}}};var f=function(o,n){return Math.floor(Math.random()*(n-o+1)+o)};o.average=function(o){return o.reduce((function(o,n){return o+n}),0)/o.length},o.clearCookie=function(){if(!u)throw new Error("Non-browser environment, unavailable 'cleanCookies'");if(!document.cookie)throw new Error("No Cookie Found");for(var o=0;o<a().length;o++){var n=a()[o];document.cookie=n.replace(/^ +/,"").replace(n.match(/=(\S*)/)[1],"")}},o.coorDistance=function(o,n){var t=o.x,e=o.y,r=n.x,c=n.y;return Math.sqrt(Math.pow(r-t,2)+Math.pow(c-e,2))},o.copyToClipboard=function(o){return new Promise((function(n,t){var e=document.createElement("textarea");e.value=o,e.style.position="fixed",e.style.top="0",e.style.left="0",e.style.opacity="0",document.body.appendChild(e),e.focus(),e.select();try{document.queryCommandValue("copy"),n()}catch(o){t(o)}document.body.removeChild(e)}))},o.diffCount=function(o,n){return o>n?o-n:n-o},o.diffDays=function(o,n){var t=o.getTime(),e=n.getTime(),r=Math.abs(t>=e?t-e:e-t);return Math.floor(r/864e5)},o.formatSeconds=function(o,n){if(!o&&!t(o,"number"))return"00:00";var e=Math.floor(o/3600),r=Math.floor(o%3600/60),c=o%60;switch(n){case"hh:mm:ss":return"".concat(e<10?"0"+e:e,":").concat(r<10?"0"+r:r,":").concat(c<10?"0"+c:c);case"mm:ss":return e?"".concat(60*e+r,":").concat(c<10?"0"+c:c):"".concat(r,":").concat(c<10?"0"+c:c);default:return e?"".concat(60*e+r,":").concat(c<10?"0"+c:c):"".concat(r<10?"0"+r:r,":").concat(c<10?"0"+c:c)}},o.getBaseUrl=function(o){return o.includes("?")?o.split("?")[0]:o},o.getCookie=a,o.getFromClipboard=function(){return new Promise((function(o,n){navigator.clipboard.readText().then((function(n){o(n)})).catch((function(o){n(o)}))}))},o.getTypeOf=n,o.getUrlParams=function(o){var n={},t=o.split("?")[1];return t?(t.split("&").forEach((function(o){var t=o.split("="),e=t[0],r=t[1];n[e]?Array.isArray(n[e])?n[e].push(r):n[e]=[n[e],r]:n[e]=r})),n):n},o.goToTop=function o(){var n,t,e,r=null!==(t=null===(n=null===document||void 0===document?void 0:document.documentElement)||void 0===n?void 0:n.scrollTop)&&void 0!==t?t:null===(e=null===document||void 0===document?void 0:document.body)||void 0===e?void 0:e.scrollTop;r>0&&(window.requestAnimationFrame(o),window.scrollTo(0,r-r/8))},o.is=t,o.isBrowser=u,o.isChinese=function(o){return/^[\u4e00-\u9fa5]+$/.test(o)},o.isEmail=function(o){return/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(o)},o.isIdCard=function(o,n){void 0===n&&(n=0);switch(n){case 1:return/^[1-9]\d{7}(?:0\d|10|11|12)(?:0[1-9]|[1-2][\d]|30|31)\d{3}$/.test(o);case 2:return/^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/.test(o);default:return/^\d{6}((((((19|20)\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(((19|20)\d{2})(0[13578]|1[02])31)|((19|20)\d{2})02(0[1-9]|1\d|2[0-8])|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))0229))\d{3})|((((\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|((\d{2})(0[13578]|1[02])31)|((\d{2})02(0[1-9]|1\d|2[0-8]))|(([13579][26]|[2468][048]|0[048])0229))\d{2}))(\d|X|x)$/.test(o)}},o.isMobile=function(o){return/^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/.test(o)},o.isRegexWith=function(o,n){return o.test(n)},o.isUrl=function(o){return/^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?/.test(o)},o.log=l,o.randomColor=function(o){void 0===o&&(o=0);var n="rgb(".concat(f(0,255),", ").concat(f(0,255),", ").concat(f(0,255),")"),t="rgba(".concat(f(0,255),", ").concat(f(0,255),", ").concat(f(0,255),", ").concat((f(0,255)/255).toFixed(2),")"),e="hsl(".concat(f(0,360),", ").concat(f(0,100),"%, ").concat(f(0,100),"%)"),r="hsla(".concat(f(0,360),", ").concat(f(0,100),"%, ").concat(f(0,100),"%, ").concat((f(0,100)/255).toFixed(1),")"),c="#".concat(f(0,255).toString(16)).concat(f(0,255).toString(16)).concat(f(0,255).toString(16));return o?1===o?t:2===o?e:3===o?r:c:n},o.randomIP=function(o){void 0===o&&(o=0);var n=f(0,255)+"."+f(0,255)+"."+f(0,255)+"."+f(0,255),t=f(0,65535)+":"+f(0,65535)+":"+f(0,65535)+":"+f(0,65535)+":"+f(0,65535)+":"+f(0,65535)+":"+f(0,65535)+":"+f(0,65535);return o?t:n},o.randomInt=f,o.removeHTMLTag=function(o){return o.replace(/<[^>]+>/g,"")},o.sum=function(o){return o.reduce((function(o,n){return o+n}),0)},o.throttle=function(o,n){void 0===n&&(n=1e3);var t=!1;return function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];t||(t=!0,setTimeout((function(){o.apply(void 0,e),t=!1}),n))}},o.wait=function(o){return e(void 0,void 0,void 0,(function(){return r(this,(function(n){return[2,new Promise((function(n){return setTimeout(n,o)}))]}))}))},Object.defineProperty(o,"__esModule",{value:!0})})); //# sourceMappingURL=index.min.js.map