UNPKG

@luband/jstools

Version:

前端工具包

2 lines (1 loc) 8.15 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@luband/jstools"]={})}(this,(function(e){"use strict";var t=function(e,n){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])},t(e,n)};function n(e,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}var r=function(){return r=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},r.apply(this,arguments)};function o(e,t,n){if(n||2===arguments.length)for(var r,o=0,i=t.length;o<i;o++)!r&&o in t||(r||(r=Array.prototype.slice.call(t,0,o)),r[o]=t[o]);return e.concat(r||Array.prototype.slice.call(t))}function i(e){var t="[object "+e+"]";return function(e){return Object.prototype.toString.call(e)===t}}function u(e){return i("Object")(e)}function c(e){return i("Array")(e)}function l(e){return i("RegExp")(e)}function s(e){return i("Date")(e)}var a=function(){function e(){}return e.prototype.setItem=function(e,t,n){void 0===n&&(n=!1),n&&(t=JSON.stringify(t)),this.storage.setItem(e,t)},e.prototype.getItem=function(e,t){return void 0===t&&(t=!1),t?JSON.parse(this.storage.getItem(e)):this.storage.getItem(e)},e.prototype.removeItem=function(e){this.storage.removeItem(e)},e.prototype.clear=function(){this.storage.clear()},e.prototype.key=function(e){return this.storage.key(e)},e.prototype.length=function(){return this.storage.length},e}(),d=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.storage=localStorage,t}return n(t,e),t.prototype.setItem=function(t,n,r,o){if(o){var i=this.getItem("lb-expireKeys",!0)||[];(null==i?void 0:i.includes(t))||(i.push(t),this.setItem("lb-expireKeys",i,!0)),n={data:n,time:Date.now(),expires:o}}e.prototype.setItem.call(this,t,n,!!o||r)},t.prototype.getItem=function(t,n){var r=e.prototype.getItem.call(this,"lb-expireKeys",!0);if(null==r?void 0:r.includes(t)){var o=e.prototype.getItem.call(this,t,!0),i=o.data;if(o.time+o.expires<Date.now()){this.removeItem(t);var u=r.findIndex((function(e){return e===t}));return r.splice(u,1),this.setItem("lb-expireKeys",r,!0),null}return i}return e.prototype.getItem.call(this,t,n)},t}(a),f=new d,p=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.storage=sessionStorage,t}return n(t,e),t}(a),h=new p;var m=function(){function e(){this.handlers={},this.args={}}return e.prototype.on=function(e,t){this.handlers[e]||(this.handlers[e]=[]),this.handlers[e].push(t)},e.prototype.onLatest=function(e,t){this.handlers[e]||(this.handlers[e]=[]),this.handlers[e].push(t),this.args[e]&&t.apply(void 0,this.args[e])},e.prototype.emit=function(e){for(var t=[],n=1;n<arguments.length;n++)t[n-1]=arguments[n];this.handlers[e]&&this.handlers[e].slice().forEach((function(e){e.apply(void 0,t)}));this.args[e]=o([],t,!0)},e.prototype.off=function(e,t){var n=this.handlers[e],r=n.indexOf(t);-1!==r&&n.splice(r,1)},e.prototype.once=function(e,t){var n=this,r=function(){for(var o=[],i=0;i<arguments.length;i++)o[i]=arguments[i];t.apply(void 0,o),n.off(e,r)};this.on(e,r)},e}(),y=new m;e.clone=function(e){var t=function(e){var t="";return e.global&&(t+="g"),e.ignoreCase&&(t+="i"),e.multiline&&(t+="m"),t},n=[],r=[];return function e(o){if(null===o)return null;if("object"!=typeof o)return o;var i,u;c(o)?i=[]:l(o)?(i=new RegExp(o.source,t(o)),o.lastIndex&&(i.lastIndex=o.lastIndex)):s(o)?i=new Date(o.getTime()):(u=Object.getPrototypeOf(o),i=Object.create(u));var a=n.indexOf(o);if(-1!=a)return r[a];for(var d in n.push(o),r.push(i),o)i[d]=e(o[d]);return i}(e)},e.copyText=function(e){var t=document.createElement("input");t.setAttribute("value",e),document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t)},e.deepSearch=function(e,t,n){void 0===n&&(n="children");var r=Array.isArray(e)?e:[e],i=u(t)?function(e){return Object.keys(t).every((function(n){return e[n]===t[n]}))}:t,c=function(e,t){void 0===t&&(t=[]);for(var r=0,u=e;r<u.length;r++){var l=u[r];if(i(l))return[l,o(o([],t,!0),[l],!1)];if(l[n]&&l[n].length>0){var s=c(l[n],o(o([],t,!0),[l],!1));if(s[0])return s}}return[null,[]]};return c(r)},e.eventEmitter=y,e.exitFullscreen=function(){(document.fullscreenElement||document.webkitFullscreenElement||document.mozFullScreenElement||document.msFullscreenElement)&&(document.exitFullscreen?document.exitFullscreen():document.webkitExitFullscreen?document.webkitExitFullscreen():document.mozCancelFullScreen?document.mozCancelFullScreen():document.msExitFullscreen&&document.msExitFullscreen())},e.isArray=c,e.isDate=s,e.isEmail=function(e){return/^([a-zA-Z0-9\u4e00-\u9fa5]+[_.-]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_.-]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(e)},e.isIdCard=function(e){return/^[1-9]\d{5}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(e)},e.isIp=function(e){return/^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))$/.test(e)},e.isMobilePhone=function(e){return/^(0|86|17951)?(1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$)/.test(e)},e.isObject=u,e.isPort=function(e){return/^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/.test(e)},e.isRegExp=l,e.isString=function(e){return i("String")(e)},e.isTelePhone=function(e){return/^((0\d{2,3})-)?(\d{7,8})$/.test(e)},e.launchFullscreen=function(e){void 0===e&&(e=document.documentElement),e.requestFullscreen?e.requestFullscreen().then():e.mozRequestFullScreen?e.mozRequestFullScreen():e.msRequestFullscreen?e.msRequestFullscreen():e.webkitRequestFullscreen&&e.webkitRequestFullScreen()},e.localCache=f,e.obj2FormData=function(e){var t=new FormData;return Object.entries(e).forEach((function(e){var n=e[0],r=e[1];t.append(n,r)})),t},e.omit=function(e,t){for(var n=Object.create(null,Object.getOwnPropertyDescriptors(e)),r=0;r<t.length;r+=1){delete n[t[r]]}return n},e.parseUrlParams=function(e){var t;void 0===e&&(e=window.location.href);var n=null===(t=/.+\?(.+)$/.exec(e))||void 0===t?void 0:t[1];if(n){var r=n.split("&"),o={};return r.forEach((function(e){if(/=/.test(e)){var t=e.split("="),n=t[0],r=t[1];r=decodeURIComponent(r);var i=/^\d+$/.test(r)?parseFloat(r):r;o.hasOwnProperty(n)?o[n]=[].concat(o[n],i):o[n]=i}else o[e]=void 0})),o}return{}},e.pick=function(e,t){return t.length?Object.entries(e).reduce((function(e,n){var o,i=n[0],u=n[1];return t.includes(i)?r(r({},e),((o={})[i]=u,o)):e}),{}):r({},e)},e.randomColor=function(){return"#".concat(Math.random().toString(16).slice(2,8).padEnd(6,"0"))},e.scrollToElement=function(e){e.scrollIntoView({behavior:"smooth"})},e.scrollToTop=function e(){var t=document.documentElement.scrollTop||document.body.scrollTop;t>0&&(window.requestAnimationFrame(e),window.scrollTo(0,t-t/8))},e.sessionCache=h,e.streamDownload=function(e,t,n){void 0===t&&(t=""),void 0===n&&(n="");var r=new Blob([e],{type:n}),o=document.createElement("a");o.style.display="none",o.href=URL.createObjectURL(r),o.download=t,document.body.appendChild(o),o.click(),URL.revokeObjectURL(o.href),document.body.removeChild(o)},e.tmEmail=function(e){return e.replace(/^(.{0,3}).*@(.*)$/,"$1***@$2")},e.tmIdCard=function(e){return e.replace(/^(.{4})\d+(.{2})$/,"$1************$2")},e.tmName=function(e){var t=e.length;return 2===t?e.substring(0,1)+"*":3===t?e.substring(0,1)+"*"+e.substring(2,3):t>3?e.substring(0,1)+"**"+e.substring(3,t):e},e.tmPhone=function(e){return e.length>=11?e.replace(/^(.{3})\d+(.{4})$/,"$1****$2"):e.replace(/^(.{2})\d+(.{2})$/,"$1****$2")},e.uuid=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){var t=16*Math.random()|0;return("x"==e?t:3&t|8).toString(16)}))},Object.defineProperty(e,"__esModule",{value:!0})}));