@yh-kit/utils 1.7.1 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- (function(l,b){typeof exports=="object"&&typeof module<"u"?b(exports):typeof define=="function"&&define.amd?define(["exports"],b):(l=typeof globalThis<"u"?globalThis:l||self,b(l.yhkitUtils={}))})(this,function(l){"use strict";const m=Object.assign({unique(e){return e?Array.from(new Set(e)):[]},isExist(e,n){return!e||!n?!1:e.indexOf(n)!==-1},countOfAppear(e,n){let t=0;for(const r of e)r===n&&t++;return t},indexsOfAppear(e,n){let t=-1;const r=[];do t=e.indexOf(n,t+1),t!==-1&&r.push(t);while(t!==-1);return r},sort(e,n="asc"){return e?e.sort((t,r)=>n==="asc"?t>r?1:-1:t<r?1:-1):[]},shuffle(e){if(!e)return[];for(let n=e.length-1;n>0;n--){const t=Math.floor(Math.random()*(n+1));[e[n],e[t]]=[e[t],e[n]]}return e},getMaxValue(e){return e?Math.max(...e):0},getMinValue(e){return e?Math.min(...e):0},getMinValueAndIndex(e){if(e.length===0)return{value:0,index:0};const n={value:e[0],index:0};for(let t=1,r=e.length;t<r;t++){const o=e[t];n.value>o&&(n.value=o,n.index=t)}return n},getMaxValueAndIndex(e){return e.length===0?{value:0,index:0}:e.reduce((n,t,r)=>t<n.value?{value:t,index:r}:n,{value:e[0],index:0})}},{...Object.freeze(Object.defineProperty({__proto__:null,toEnumObj:e=>{if(!e||!e.length)return{};const n={};return e.forEach(t=>{const r=Object.assign({label:t.label,text:t.label},t);n[t==null?void 0:t.value]=r}),n}},Symbol.toStringTag,{value:"Module"}))}),M={toBlob(e,n="",t=512){const o=(e.split(",")[1]||e).replace(/-/g,"+").replace(/_/g,"/"),s="=".repeat((4-o.length%4)%4),u=o+s;try{const c=atob(u),a=[];for(let h=0;h<c.length;h+=t){const i=c.slice(h,h+t),d=new Array(i.length);for(let f=0;f<i.length;f++)d[f]=i.charCodeAt(f);const g=new Uint8Array(d);a.push(g)}return new Blob(a,{type:n})}catch(c){return console.error("Failed to convert base64 to blob:",c),null}},toFile(e,n="file.txt",t="text/plain"){const o=e.replace(/^data:.+;base64,/,"").replace(/-/g,"+").replace(/_/g,"/"),s="=".repeat((4-o.length%4)%4),u=o+s,c=atob(u),a=[];for(let i=0;i<c.length;i+=512){const d=c.slice(i,i+512),g=new Array(d.length);for(let f=0;f<d.length;f++)g[f]=d.charCodeAt(f);a.push(new Uint8Array(g))}const h=new Blob(a,{type:t});return new File([h],n,{type:t})}},w={isEmptyString(e){return typeof e=="string"&&e.trim()===""},isNumber(e){return typeof e=="number"&&!isNaN(e)},isObject(e){return e!==null&&typeof e=="object"&&!Array.isArray(e)},isEmptyObject(e){return e?Object.keys(e).length===0&&e.constructor===Object:!0},isBoolean(e){return typeof e=="boolean"},isArray(e){return Array.isArray(e)},isFunction(e){return typeof e=="function"},isPromise(e){return!!e&&(typeof e=="object"||typeof e=="function")&&typeof e.then=="function"},isElement(e){return e instanceof Element},isPhone(e){return/^1[3-9]\d{9}$/.test(e)},isEmail(e){return/^[\w.-]+@[\w.-]+\.\w+$/.test(e)},isJSON(e){try{return JSON.parse(e),!0}catch{return!1}},isImageLoaded(e){return e.complete&&e.naturalWidth!==0},isInViewport(e){const n=e.getBoundingClientRect();return n.top>=0&&n.left>=0&&n.bottom<=window.innerHeight&&n.right<=window.innerWidth},isLeapYear(e){return e%4===0&&e%100!==0||e%400===0},isMobile(){return/Mobi|Android|iPhone/i.test(navigator.userAgent)}},U={getCookie(e){if(!e)return;const n=document.cookie.match(new RegExp("(^| )"+e+"=([^;]+)"));return n?decodeURIComponent(n[2]):null},setCookie(e,n,t=7){const r=new Date;r.setTime(r.getTime()+t*24*60*60*1e3),document.cookie=`${e}=${encodeURIComponent(n)};expires=${r.toUTCString()};path=/`},deleteCookie(e){this.setCookie(e,"",-1)}},S={getTimeString(e,n=!1){return n?new Date().toLocaleString(e,{hour12:!0}):new Date().toLocaleString("chinese",{hour12:!1})},diffDays(e,n){const t=new Date(e).getTime(),r=new Date(n).getTime();return t>r?Math.abs(Math.floor((t-r)/(24*3600*1e3))):Math.abs(Math.floor((r-t)/(24*3600*1e3)))},uniqueId(){return Date.now().toString(36)+Math.random().toString(36).substr(2,5)},getWhichDays(e,n,t){let r=t;for(let o=1;o<n;o++)switch(o){case 1:case 3:case 5:case 7:case 8:case 10:case 12:r+=31;break;case 2:w.isLeapYear(e)?r+=29:r+=28;break;default:r+=30;break}return r}},O={getOffsetTop(e){if(!e)throw new Error("Element is not provided");if(e instanceof SVGElement){const t=e.getBoundingClientRect(),r=window.pageYOffset||document.documentElement.scrollTop;return t.top+r}let n=0;for(;e;)n+=e.offsetTop,e=e.offsetParent;return n},getScrollValue(){const e={scrollLeft:0,scrollTop:0};return e.scrollLeft=document.body.scrollLeft||document.documentElement.scrollLeft,e.scrollTop=document.body.scrollTop||document.documentElement.scrollTop,e},getPageValue(e){e=e||window.event;const n=e.pageX||e.clientX+this.getScrollValue().scrollLeft,t=e.pageY||e.clientY+this.getScrollValue().scrollTop;return{pageX:n,pageY:t}},addEventListener(e,n,t){e.addEventListener?e.addEventListener(n,t):e.attachEvent?e.attachEvent("on"+n,t):e["on"+n]=t}},v={saveAsBlob(e,n){const t=document.createElement("a"),r=window.URL.createObjectURL(e);t.href=r,t.download=n,document.body.appendChild(t),t.click(),URL.revokeObjectURL(r),document.body.removeChild(t)}},A={sortFromA2Z(e){return e!=null&&e.length?e.sort((t,r)=>{const o=t.toUpperCase(),s=r.toUpperCase();return o<s?-1:o>s?1:0}):void 0}},E={degrees2Radians(e){return e*Math.PI/180},getDistance(e,n,t,r){const o=this.degrees2Radians(e),s=this.degrees2Radians(t),u=o-s,c=this.degrees2Radians(n)-this.degrees2Radians(r);let a=2*Math.asin(Math.sqrt(Math.pow(Math.sin(u/2),2)+Math.cos(o)*Math.cos(s)*Math.pow(Math.sin(c/2),2)));return a=a*6378.137,a=Math.round(a*1e4)/1e4,a=+a.toFixed(2),console.log("经纬度计算的距离为:"+a),a},calculateDistanceByHaversine(e,n,t="km"){const r=this,o=e.longitude??e.lng,s=e.latitude??e.lat,u=n.longitude??n.lng,c=n.latitude??n.lat;if(o===void 0||s===void 0||u===void 0||c===void 0)throw new Error("无效的坐标格式,缺少经纬度信息");const a=6371,h=r.degrees2Radians(s),i=r.degrees2Radians(c),d=r.degrees2Radians(c-s),g=r.degrees2Radians(u-o),f=Math.sin(d/2)*Math.sin(d/2)+Math.cos(h)*Math.cos(i)*Math.sin(g/2)*Math.sin(g/2),p=2*Math.atan2(Math.sqrt(f),Math.sqrt(1-f)),y=a*p;switch(t){case"m":return y*1e3;case"mi":return y*.621371;case"nmi":return y*.539957;default:return y}},calculateDistancesByHaversine(e,n="km",t=!1){if(e.length<2)return{segments:[],total:0};const r=[];let o=0;for(let s=0;s<e.length-1;s++){const u=this.calculateDistanceByHaversine(e[s],e[s+1],n);r.push(u),o+=u}if(t&&e.length>2){const s=this.calculateDistanceByHaversine(e[e.length-1],e[0],n);r.push(s),o+=s}return{segments:r,total:o}}};class j{static toStandardFormat(n){try{const t=typeof n=="string"?parseFloat(n):n;if(isNaN(t))throw new Error("输入不是有效的数字");if(!isFinite(t))throw new Error("输入是无穷大");return t.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g,",")}catch(t){return console.error("格式化失败:",t),"格式错误"}}static toChineseFormat(n){try{const t=typeof n=="string"?parseFloat(n):n;if(isNaN(t))throw new Error("输入不是有效的数字");if(t>9999999999999e-2||t<-9999999999999e-2)throw new Error("输入数字超出范围");const r=t<0,o=Math.abs(t),s=Math.floor(o),u=Math.round((o-s)*100),c=["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"],a=["","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟"],h=["角","分"];let i="",d=s;if(d===0)i=c[0];else{let p=0;for(;d>0;){const y=d%10;y!==0?i=c[y]+a[p]+i:i.charAt(0)!==c[0]&&(i=c[y]+i),d=Math.floor(d/10),p++}i=i.replace(/零+/g,"零"),i=i.replace(/零+$/,"")}let g="";if(u>0){const p=Math.floor(u/10),y=u%10;p>0&&(g+=c[p]+h[0]),y>0&&(g+=c[y]+h[1])}else g="整";let f=(r?"负":"")+i+"圆"+g;return f==="零圆整"&&(f="零圆"),f}catch(t){return console.error("转换失败:",t),"格式错误"}}}const L={...Object.freeze(Object.defineProperty({__proto__:null,toLetter:e=>e>25||e<0?"":"ABCDEFGHIJKLMNOPQRSTUVWXYZ"[e]},Symbol.toStringTag,{value:"Module"})),...Object.freeze(Object.defineProperty({__proto__:null,toMoney:e=>e.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g,",")},Symbol.toStringTag,{value:"Module"})),isNumber(e){return typeof e=="number"&&!isNaN(e)}},x={isEmptyObject(e){return e?Object.keys(e).length===0&&e.constructor===Object:!0},copy(e,n){for(const t in e)n[t]=e[t]},deepCopy(e,n){for(const t in e){const r=e[t];r instanceof Object?(n[t]={},this.deepCopy(r,n[t])):r instanceof Array?(n[t]=[],this.deepCopy(r,n[t])):n[t]=e[t]}}},R={desensitize(e){return e?e.replace(/^(\d{3})\d{4}(\d{4})$/,"$1****$2")||e.slice(0,3)+"****"+e.slice(7):""}},k={color:function(){return`#${Math.random().toString(16).slice(2,8)}`},int(e,n){return Math.floor(Math.random()*(n-e+1))+e},uniqueId(){return Date.now().toString(36)+Math.random().toString(36).substr(2,5)}},C={mobile:/^1[3-9]\d{9}$/,email:/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,chinese:/^[\u4e00-\u9fa5]+$/,english:/^[a-zA-Z]+$/,chineseStart:/^[\u4e00-\u9fa5]+\w*$/,integer:/^\d+$/,integerAndzero:/^(0|[1-9]+[0-9]*)$/,strongPassword:/^(?!.*[\s\p{C}])(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[\d\W])|(?=.*\d)(?=.*[^\w\s])).{8,}$/u,decimal:/^0\.(0[1-9]|[1-9][0-9]*)$/},T={getLocal(e){return localStorage.getItem(e)},setLocal(e,n){localStorage.setItem(e,n)},removeLocal(e){localStorage.removeItem(e)},getSession(e){return sessionStorage.getItem(e)},setSession(e,n){sessionStorage.setItem(e,n)},removeSession(e){sessionStorage.removeItem(e)}},B={isExist(e,n){if(!e||!n)return!1;const t=e.split(",");return console.log("判断某元素是否在字符串中",t.indexOf(n)===-1),t.indexOf(n)!==-1},includes(e,n){return!e||!n?!1:e.includes(n)},isJSON(e){try{return JSON.parse(e),!0}catch{return!1}},indexsOfAppear(e,n){let t=-1;const r=[];do t=e.indexOf(n,t+1),t!==-1&&r.push(t);while(t!==-1);return r},getMaxTimesAndVal(e){const n=[0,""],t={};for(let s=0;s<e.length;s++){const u=e.charAt(s);t[u]?t[u]++:t[u]=1}let r=1;for(const s in t)r<t[s]&&(r=t[s]);const o=[];for(const s in t)r==t[s]&&o.push(s);return n[0]=r,n[1]=o.join(),n}},I=e=>{const t=new RegExp("[?&]"+e+"=([^&#]*)","i").exec(window.location.href);return t?decodeURIComponent(t[1]):null};function _(e){return new URLSearchParams(window.location.search).get(e)}const $={...Object.freeze(Object.defineProperty({__proto__:null,getQueryInfoByName:I,getQueryParam:_},Symbol.toStringTag,{value:"Module"})),getHost(){return window.location.host},getPath(){return window.location.pathname}},P={toList(e,n=2){console.log(e,n);const t={};for(let o=0;o<n;o++)t[o]=[];e.forEach((o,s)=>t[s%n].push(o));const r=[];for(const o in t)r.push(...t[o]);return r},jsLayout(e,n,t){const r=e.offsetWidth,o=n[0].offsetWidth,s=parseInt((r/o).toString()),u=(r-o*s)/(s-1),c=[];for(let a=0,h=n.length;a<h;a++){const i=n[a];if(a<s)i.style.left=(o+u)*a+"px",c[a]=i.offsetHeight;else{const{index:d,value:g}=m.getMinValueAndIndex(c);i.style.left=(o+u)*d+"px",i.style.top=g+t+"px",c[d]=i.offsetHeight+t+g}}}};l.MoneyFormatter=j,l.arrayUtils=m,l.base64Utils=M,l.booleanUtils=w,l.cookieUtils=U,l.dateUtils=S,l.documentUtils=O,l.downloadUtils=v,l.letterUtils=A,l.mapUtils=E,l.numberUtils=L,l.objectUtils=x,l.phoneUtils=R,l.randomUtils=k,l.regexpUtils=C,l.storageUtils=T,l.stringUtils=B,l.urlUtils=$,l.waterfallUtils=P,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
1
+ (function(h,m){typeof exports=="object"&&typeof module<"u"?m(exports):typeof define=="function"&&define.amd?define(["exports"],m):(h=typeof globalThis<"u"?globalThis:h||self,m(h.yhkitUtils={}))})(this,function(h){"use strict";const b=Object.assign({unique(t){return t?Array.from(new Set(t)):[]},isExist(t,n){return!t||!n?!1:t.indexOf(n)!==-1},countOfAppear(t,n){let e=0;for(const o of t)o===n&&e++;return e},indexsOfAppear(t,n){let e=-1;const o=[];do e=t.indexOf(n,e+1),e!==-1&&o.push(e);while(e!==-1);return o},sort(t,n="asc"){return t?t.sort((e,o)=>n==="asc"?e>o?1:-1:e<o?1:-1):[]},shuffle(t){if(!t)return[];for(let n=t.length-1;n>0;n--){const e=Math.floor(Math.random()*(n+1));[t[n],t[e]]=[t[e],t[n]]}return t},getMaxValue(t){return t?Math.max(...t):0},getMinValue(t){return t?Math.min(...t):0},getMinValueAndIndex(t){if(t.length===0)return{value:0,index:0};const n={value:t[0],index:0};for(let e=1,o=t.length;e<o;e++){const r=t[e];n.value>r&&(n.value=r,n.index=e)}return n},getMaxValueAndIndex(t){return t.length===0?{value:0,index:0}:t.reduce((n,e,o)=>e<n.value?{value:e,index:o}:n,{value:t[0],index:0})}},{...Object.freeze(Object.defineProperty({__proto__:null,toEnumObj:t=>{if(!t||!t.length)return{};const n={};return t.forEach(e=>{const o=Object.assign({label:e.label,text:e.label},e);n[e?.value]=o}),n}},Symbol.toStringTag,{value:"Module"}))});class A{static BASE64_CHARS="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";static toBlob(n,e="",o=512){const s=(n.split(",")[1]||n).replace(/-/g,"+").replace(/_/g,"/"),c="=".repeat((4-s.length%4)%4),l=s+c;try{const i=atob(l),d=[];for(let a=0;a<i.length;a+=o){const u=i.slice(a,a+o),f=new Array(u.length);for(let g=0;g<u.length;g++)f[g]=u.charCodeAt(g);const y=new Uint8Array(f);d.push(y)}return new Blob(d,{type:e})}catch(i){return console.error("Failed to convert base64 to blob:",i),null}}static toFile(n,e="file.txt",o="text/plain"){const s=n.replace(/^data:.+;base64,/,"").replace(/-/g,"+").replace(/_/g,"/"),c="=".repeat((4-s.length%4)%4),l=s+c,i=atob(l),d=[];for(let u=0;u<i.length;u+=512){const f=i.slice(u,u+512),y=new Array(f.length);for(let g=0;g<f.length;g++)y[g]=f.charCodeAt(g);d.push(new Uint8Array(y))}const a=new Blob(d,{type:o});return new File([a],e,{type:o})}static encode(n){if(!n)return"";const e=this._stringToUtf8Bytes(n);return this._bytesToBase64(e)}static decode(n){if(!n)return"";n=n.replace(/\s/g,"");const e=this._base64ToBytes(n);return this._utf8BytesToString(e)}static _stringToUtf8Bytes(n){const e=[];for(let o=0;o<n.length;o++){let r=n.charCodeAt(o);if(r<128)e.push(r);else if(r<2048)e.push(192|r>>6),e.push(128|r&63);else if(r<55296||r>=57344)e.push(224|r>>12),e.push(128|r>>6&63),e.push(128|r&63);else{o++;const s=r,c=n.charCodeAt(o);if(isNaN(c))throw new Error("代理对不完整");r=65536+((s&1023)<<10)+(c&1023),e.push(240|r>>18),e.push(128|r>>12&63),e.push(128|r>>6&63),e.push(128|r&63)}}return e}static _utf8BytesToString(n){let e="",o=0;for(;o<n.length;){const r=n[o++];if(r<128)e+=String.fromCharCode(r);else if(r>=192&&r<224){const s=n[o++]&63;e+=String.fromCharCode((r&31)<<6|s)}else if(r>=224&&r<240){const s=n[o++]&63,c=n[o++]&63;e+=String.fromCharCode((r&15)<<12|s<<6|c)}else if(r>=240&&r<248){const s=n[o++]&63,c=n[o++]&63,l=n[o++]&63,i=(r&7)<<18|s<<12|c<<6|l,d=Math.floor((i-65536)/1024)+55296,a=(i-65536)%1024+56320;e+=String.fromCharCode(d,a)}else e+="�"}return e}static _bytesToBase64(n){let e="",o=0;for(;o<n.length;){const r=n[o++],s=o<n.length,c=s?n[o++]:0,l=o<n.length,i=l?n[o++]:0,d=r>>2,a=(r&3)<<4|c>>4,u=(c&15)<<2|i>>6,f=i&63;s?l?e+=this.BASE64_CHARS.charAt(d)+this.BASE64_CHARS.charAt(a)+this.BASE64_CHARS.charAt(u)+this.BASE64_CHARS.charAt(f):e+=this.BASE64_CHARS.charAt(d)+this.BASE64_CHARS.charAt(a)+this.BASE64_CHARS.charAt(u)+"=":e+=this.BASE64_CHARS.charAt(d)+this.BASE64_CHARS.charAt(a)+"=="}return e}static _base64ToBytes(n){const e=n.length;let o=0;e>=2&&(n[e-1]==="="&&o++,n[e-2]==="="&&o++);const r=Math.floor(e*3/4)-o,s=new Array(r),c=new Array(256).fill(-1);for(let d=0;d<this.BASE64_CHARS.length;d++)c[this.BASE64_CHARS.charCodeAt(d)]=d;let l=0,i=0;for(;i<e;){const d=c[n.charCodeAt(i++)],a=c[n.charCodeAt(i++)],u=i<e?c[n.charCodeAt(i++)]:-1,f=i<e?c[n.charCodeAt(i++)]:-1;if(d===-1||a===-1)throw new Error("无效的 Base64 字符");const y=d<<2|a>>4;if(s[l++]=y,u!==-1){const g=(a&15)<<4|u>>2;if(s[l++]=g,f!==-1){const p=(u&3)<<6|f;s[l++]=p}}}return s}}const w={isEmptyString(t){return typeof t=="string"&&t.trim()===""},isNumber(t){return typeof t=="number"&&!isNaN(t)},isObject(t){return t!==null&&typeof t=="object"&&!Array.isArray(t)},isEmptyObject(t){return t?Object.keys(t).length===0&&t.constructor===Object:!0},isBoolean(t){return typeof t=="boolean"},isArray(t){return Array.isArray(t)},isFunction(t){return typeof t=="function"},isPromise(t){return!!t&&(typeof t=="object"||typeof t=="function")&&typeof t.then=="function"},isElement(t){return t instanceof Element},isPhone(t){return/^1[3-9]\d{9}$/.test(t)},isEmail(t){return/^[\w.-]+@[\w.-]+\.\w+$/.test(t)},isJSON(t){try{return JSON.parse(t),!0}catch{return!1}},isImageLoaded(t){return t.complete&&t.naturalWidth!==0},isInViewport(t){const n=t.getBoundingClientRect();return n.top>=0&&n.left>=0&&n.bottom<=window.innerHeight&&n.right<=window.innerWidth},isLeapYear(t){return t%4===0&&t%100!==0||t%400===0},isMobile(){return/Mobi|Android|iPhone/i.test(navigator.userAgent)}},S={getCookie(t){if(!t)return;const n=document.cookie.match(new RegExp("(^| )"+t+"=([^;]+)"));return n?decodeURIComponent(n[2]):null},setCookie(t,n,e=7){const o=new Date;o.setTime(o.getTime()+e*24*60*60*1e3),document.cookie=`${t}=${encodeURIComponent(n)};expires=${o.toUTCString()};path=/`},deleteCookie(t){this.setCookie(t,"",-1)}},M={getTimeString(t,n=!1){return n?new Date().toLocaleString(t,{hour12:!0}):new Date().toLocaleString("chinese",{hour12:!1})},diffDays(t,n){const e=new Date(t).getTime(),o=new Date(n).getTime();return e>o?Math.abs(Math.floor((e-o)/(24*3600*1e3))):Math.abs(Math.floor((o-e)/(24*3600*1e3)))},uniqueId(){return Date.now().toString(36)+Math.random().toString(36).substr(2,5)},getWhichDays(t,n,e){let o=e;for(let r=1;r<n;r++)switch(r){case 1:case 3:case 5:case 7:case 8:case 10:case 12:o+=31;break;case 2:w.isLeapYear(t)?o+=29:o+=28;break;default:o+=30;break}return o}},U={getOffsetTop(t){if(!t)throw new Error("Element is not provided");if(t instanceof SVGElement){const e=t.getBoundingClientRect(),o=window.pageYOffset||document.documentElement.scrollTop;return e.top+o}let n=0;for(;t;)n+=t.offsetTop,t=t.offsetParent;return n},getScrollValue(){const t={scrollLeft:0,scrollTop:0};return t.scrollLeft=document.body.scrollLeft||document.documentElement.scrollLeft,t.scrollTop=document.body.scrollTop||document.documentElement.scrollTop,t},getPageValue(t){t=t||window.event;const n=t.pageX||t.clientX+this.getScrollValue().scrollLeft,e=t.pageY||t.clientY+this.getScrollValue().scrollTop;return{pageX:n,pageY:e}},addEventListener(t,n,e){t.addEventListener?t.addEventListener(n,e):t.attachEvent?t.attachEvent("on"+n,e):t["on"+n]=e},getViewportWidth(){return window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth},getViewportHeight(){return window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight}},C={saveAsBlob(t,n){const e=document.createElement("a"),o=window.URL.createObjectURL(t);e.href=o,e.download=n,document.body.appendChild(e),e.click(),URL.revokeObjectURL(o),document.body.removeChild(e)}},B={sortFromA2Z(t){return t?.length?t.sort((e,o)=>{const r=e.toUpperCase(),s=o.toUpperCase();return r<s?-1:r>s?1:0}):void 0}},E={degrees2Radians(t){return t*Math.PI/180},getDistance(t,n,e,o){const r=this.degrees2Radians(t),s=this.degrees2Radians(e),c=r-s,l=this.degrees2Radians(n)-this.degrees2Radians(o);let i=2*Math.asin(Math.sqrt(Math.pow(Math.sin(c/2),2)+Math.cos(r)*Math.cos(s)*Math.pow(Math.sin(l/2),2)));return i=i*6378.137,i=Math.round(i*1e4)/1e4,i=+i.toFixed(2),console.log("经纬度计算的距离为:"+i),i},calculateDistanceByHaversine(t,n,e="km"){const o=this,r=t.longitude??t.lng,s=t.latitude??t.lat,c=n.longitude??n.lng,l=n.latitude??n.lat;if(r===void 0||s===void 0||c===void 0||l===void 0)throw new Error("无效的坐标格式,缺少经纬度信息");const i=6371,d=o.degrees2Radians(s),a=o.degrees2Radians(l),u=o.degrees2Radians(l-s),f=o.degrees2Radians(c-r),y=Math.sin(u/2)*Math.sin(u/2)+Math.cos(d)*Math.cos(a)*Math.sin(f/2)*Math.sin(f/2),g=2*Math.atan2(Math.sqrt(y),Math.sqrt(1-y)),p=i*g;switch(e){case"m":return p*1e3;case"mi":return p*.621371;case"nmi":return p*.539957;default:return p}},calculateDistancesByHaversine(t,n="km",e=!1){if(t.length<2)return{segments:[],total:0};const o=[];let r=0;for(let s=0;s<t.length-1;s++){const c=this.calculateDistanceByHaversine(t[s],t[s+1],n);o.push(c),r+=c}if(e&&t.length>2){const s=this.calculateDistanceByHaversine(t[t.length-1],t[0],n);o.push(s),r+=s}return{segments:o,total:r}}};class _{static toStandardFormat(n){try{const e=typeof n=="string"?parseFloat(n):n;if(isNaN(e))throw new Error("输入不是有效的数字");if(!isFinite(e))throw new Error("输入是无穷大");return e.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g,",")}catch(e){return console.error("格式化失败:",e),"格式错误"}}static toChineseFormat(n){try{const e=typeof n=="string"?parseFloat(n):n;if(isNaN(e))throw new Error("输入不是有效的数字");if(e>9999999999999e-2||e<-9999999999999e-2)throw new Error("输入数字超出范围");const o=e<0,r=Math.abs(e),s=Math.floor(r),c=Math.round((r-s)*100),l=["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"],i=["","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟"],d=["角","分"];let a="",u=s;if(u===0)a=l[0];else{let g=0;for(;u>0;){const p=u%10;p!==0?a=l[p]+i[g]+a:a.charAt(0)!==l[0]&&(a=l[p]+a),u=Math.floor(u/10),g++}a=a.replace(/零+/g,"零"),a=a.replace(/零+$/,"")}let f="";if(c>0){const g=Math.floor(c/10),p=c%10;g>0&&(f+=l[g]+d[0]),p>0&&(f+=l[p]+d[1])}else f="整";let y=(o?"负":"")+a+"圆"+f;return y==="零圆整"&&(y="零圆"),y}catch(e){return console.error("转换失败:",e),"格式错误"}}}const O={...Object.freeze(Object.defineProperty({__proto__:null,toLetter:t=>t>25||t<0?"":"ABCDEFGHIJKLMNOPQRSTUVWXYZ"[t]},Symbol.toStringTag,{value:"Module"})),...Object.freeze(Object.defineProperty({__proto__:null,toMoney:t=>t.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g,",")},Symbol.toStringTag,{value:"Module"})),isNumber(t){return typeof t=="number"&&!isNaN(t)}},v={isEmptyObject(t){return t?Object.keys(t).length===0&&t.constructor===Object:!0},copy(t,n){for(const e in t)n[e]=t[e]},deepCopy(t,n){for(const e in t){const o=t[e];o instanceof Object?(n[e]={},this.deepCopy(o,n[e])):o instanceof Array?(n[e]=[],this.deepCopy(o,n[e])):n[e]=t[e]}}},R={desensitize(t){return t?t.replace(/^(\d{3})\d{4}(\d{4})$/,"$1****$2")||t.slice(0,3)+"****"+t.slice(7):""}},L={color:function(){return`#${Math.random().toString(16).slice(2,8)}`},int(t,n){return Math.floor(Math.random()*(n-t+1))+t},uniqueId(){return Date.now().toString(36)+Math.random().toString(36).substr(2,5)}},T={mobile:/^1[3-9]\d{9}$/,email:/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,chinese:/^[\u4e00-\u9fa5]+$/,english:/^[a-zA-Z]+$/,chineseStart:/^[\u4e00-\u9fa5]+\w*$/,integer:/^\d+$/,integerAndzero:/^(0|[1-9]+[0-9]*)$/,strongPassword:/^(?!.*[\s\p{C}])(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[\d\W])|(?=.*\d)(?=.*[^\w\s])).{8,}$/u,decimal:/^0\.(0[1-9]|[1-9][0-9]*)$/},j={getLocal(t){return localStorage.getItem(t)},setLocal(t,n){localStorage.setItem(t,n)},removeLocal(t){localStorage.removeItem(t)},getSession(t){return sessionStorage.getItem(t)},setSession(t,n){sessionStorage.setItem(t,n)},removeSession(t){sessionStorage.removeItem(t)}},x={isExist(t,n){if(!t||!n)return!1;const e=t.split(",");return console.log("判断某元素是否在字符串中",e.indexOf(n)===-1),e.indexOf(n)!==-1},includes(t,n){return!t||!n?!1:t.includes(n)},isJSON(t){try{return JSON.parse(t),!0}catch{return!1}},indexsOfAppear(t,n){let e=-1;const o=[];do e=t.indexOf(n,e+1),e!==-1&&o.push(e);while(e!==-1);return o},getMaxTimesAndVal(t){const n=[0,""],e={};for(let s=0;s<t.length;s++){const c=t.charAt(s);e[c]?e[c]++:e[c]=1}let o=1;for(const s in e)o<e[s]&&(o=e[s]);const r=[];for(const s in e)o==e[s]&&r.push(s);return n[0]=o,n[1]=r.join(),n}},k=t=>{const e=new RegExp("[?&]"+t+"=([^&#]*)","i").exec(window.location.href);return e?decodeURIComponent(e[1]):null};function H(t){return new URLSearchParams(window.location.search).get(t)}const I={...Object.freeze(Object.defineProperty({__proto__:null,getQueryInfoByName:k,getQueryParam:H},Symbol.toStringTag,{value:"Module"})),getHost(){return window.location.host},getPath(){return window.location.pathname}},P={toList(t,n=2){console.log(t,n);const e={};for(let r=0;r<n;r++)e[r]=[];t.forEach((r,s)=>e[s%n].push(r));const o=[];for(const r in e)o.push(...e[r]);return o},jsLayout(t,n,e){const o=t.offsetWidth,r=n[0].offsetWidth,s=parseInt((o/r).toString()),c=(o-r*s)/(s-1),l=[];for(let i=0,d=n.length;i<d;i++){const a=n[i];if(i<s)a.style.left=(r+c)*i+"px",l[i]=a.offsetHeight;else{const{index:u,value:f}=b.getMinValueAndIndex(l);a.style.left=(r+c)*u+"px",a.style.top=f+e+"px",l[u]=a.offsetHeight+e+f}}}};h.Base64Utils=A,h.MoneyFormatter=_,h.arrayUtils=b,h.booleanUtils=w,h.cookieUtils=S,h.dateUtils=M,h.documentUtils=U,h.downloadUtils=C,h.letterUtils=B,h.mapUtils=E,h.numberUtils=O,h.objectUtils=v,h.phoneUtils=R,h.randomUtils=L,h.regexpUtils=T,h.storageUtils=j,h.stringUtils=x,h.urlUtils=I,h.waterfallUtils=P,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@yh-kit/utils",
3
3
  "private": false,
4
- "version": "1.7.1",
4
+ "version": "1.9.0",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist",
@@ -1,7 +1,9 @@
1
1
  /**
2
2
  * Base64 相关操作工具函数
3
3
  */
4
- export declare const base64Utils: {
4
+ export declare class Base64Utils {
5
+ /** Base64 编码表 */
6
+ private static readonly BASE64_CHARS;
5
7
  /**
6
8
  * 将Base64编码的字符串转换为Blob对象。
7
9
  * Blob对象用于表示二进制大型对象,可以在浏览器环境中处理大文件或二进制数据。
@@ -11,7 +13,7 @@ export declare const base64Utils: {
11
13
  * @param sliceSize 可选参数,表示分片大小,默认为512字节。用于控制每次处理的Base64字符串长度,以优化大文件的处理。
12
14
  * @returns 返回转换后的Blob对象,如果转换失败,则返回null。
13
15
  */
14
- toBlob(base64: string, contentType?: string, sliceSize?: number): Blob | null;
16
+ static toBlob(base64: string, contentType?: string, sliceSize?: number): Blob | null;
15
17
  /**
16
18
  * 将Base64编码的字符串转换为File对象。
17
19
  * 这个函数接受一个Base64编码的字符串,一个可选的文件名和一个可选的MIME类型,
@@ -22,5 +24,33 @@ export declare const base64Utils: {
22
24
  * @param mimeType MIME类型,默认为"text/plain"。
23
25
  * @returns 返回一个File对象,包含解码后的数据。
24
26
  */
25
- toFile(base64: string, fileName?: string, mimeType?: string): File;
26
- };
27
+ static toFile(base64: string, fileName?: string, mimeType?: string): File;
28
+ /**
29
+ * base64加密 -- 普通字符串转 Base64 编码
30
+ * @param text 普通字符串
31
+ * @returns Base64 编码字符串
32
+ */
33
+ static encode(text: string): string;
34
+ /**
35
+ * base64解密 -- Base64 编码转普通字符串
36
+ * @param base64Str Base64 编码字符串
37
+ * @returns 普通字符串
38
+ */
39
+ static decode(base64Str: string): string;
40
+ /**
41
+ * 字符串转 UTF-8 字节数组
42
+ */
43
+ private static _stringToUtf8Bytes;
44
+ /**
45
+ * UTF-8 字节数组转字符串
46
+ */
47
+ private static _utf8BytesToString;
48
+ /**
49
+ * 字节数组转 Base64 编码
50
+ */
51
+ private static _bytesToBase64;
52
+ /**
53
+ * Base64 编码转字节数组
54
+ */
55
+ private static _base64ToBytes;
56
+ }
@@ -37,4 +37,14 @@ export declare const documentUtils: {
37
37
  * @param fn 事件处理函数
38
38
  */
39
39
  addEventListener(element: any, eventName: string, fn: any): void;
40
+ /**
41
+ * 获取视口宽度
42
+ * @returns 视口宽度
43
+ */
44
+ getViewportWidth(): number;
45
+ /**
46
+ * 获取视口高度
47
+ * @returns 视口高度
48
+ */
49
+ getViewportHeight(): number;
40
50
  };
@@ -8,6 +8,10 @@ export declare const numberUtils: {
8
8
  * @returns 是否为数字。true 是;false 否
9
9
  */
10
10
  isNumber<T>(val: T): boolean;
11
- toMoney: (x: number) => string;
11
+ toMoney: (/**
12
+ * 判断是否为数字
13
+ * @param val 待判断的值
14
+ * @returns 是否为数字。true 是;false 否
15
+ */ x: number) => string;
12
16
  toLetter: (num: number) => string;
13
17
  };