cross-env-plugins
Version:
A cross-environment JavaScript utility library with common tools and validation functions for Web and Node.js
2 lines (1 loc) • 11.3 kB
JavaScript
(function(d,g){typeof exports=="object"&&typeof module<"u"?g(exports):typeof define=="function"&&define.amd?define(["exports"],g):(d=typeof globalThis<"u"?globalThis:d||self,g(d.crossEnvPlugins={}))})(this,function(d){"use strict";function g(t){const e=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],n=new Date(t).getDay();return e[n]}function T(t,e=new Date){const n=new Date(e).getTime(),r=new Date(t).getTime();if(isNaN(n)||isNaN(r))throw new Error("Invalid start or end time format");return Math.max(r-n,0)}function A(t,e=2){return parseFloat(String(t)).toFixed(e).replace(/\d(?=(\d{3})+\.)/g,"$&,")}function N(t){const e=["角","分"],n=["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"],r=[["元","万","亿"],["","拾","佰","仟"]];let o=t<0?"负":"";t=Math.abs(t);let i="";for(let s=0;s<e.length;s++)i+=(n[Math.floor(t*10*Math.pow(10,s))%10]+e[s]).replace(/零./,"");i=i||"整",t=Math.floor(t);for(let s=0;s<r[0].length&&t>0;s++){let c="";for(let a=0;a<r[1].length&&t>0;a++)c=n[t%10]+r[1][a]+c,t=Math.floor(t/10);i=c.replace(/(零.)*零$/,"").replace(/^$/,"零")+r[0][s]+i}return o+i.replace(/(零.)*零元/,"元").replace(/(零.)+/g,"零").replace(/^整$/,"零元整")}function S(t=6){const e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";let n="";for(let r=0;r<t;r++)n+=e.charAt(Math.floor(Math.random()*e.length));return n}function C(t,e){let n;return(...r)=>{n&&clearTimeout(n),n=setTimeout(()=>{t(...r)},e)}}function I(t,e){let n=0;return(...r)=>{const o=new Date().getTime();o-n>=e&&(t(...r),n=o)}}function m(t){if(typeof t!="object"||t===null)return t;const e=Array.isArray(t)?[]:{};for(const n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=m(t[n]));return e}function $(t,e,n=!0){for(let r in e)Object.prototype.hasOwnProperty.call(e,r)&&(n||!(r in t))&&(t[r]=e[r]);return t}function R(t){return[...new Set(t)]}function B(t){let e=0;const n=["[A-Z]","[a-z]","[0-9]","[!@#$%^&*()_+\\-={};':\",.<>?`~\\[\\]\\\\/]|\\\\"];for(let r=0;r<n.length;r++)new RegExp(n[r]).test(t)&&e++;return e}const E=(t,e)=>Math.floor(Math.random()*(e-t+1))+t,O=(t=new Date,e="yyyy-MM-dd hh:mm:ss")=>{const n=new Date(t),r={"M+":n.getMonth()+1,"d+":n.getDate(),"h+":n.getHours(),"m+":n.getMinutes(),"s+":n.getSeconds(),"q+":Math.floor((n.getMonth()+3)/3),S:n.getMilliseconds()},o=e.match(/(y+)/);o&&(e=e.replace(o[0],(n.getFullYear()+"").substring(4-o[0].length)));for(const i in r){const s=e.match(new RegExp("("+i+")"));s&&(e=e.replace(s[0],s[0].length===1?String(r[i]):("00"+r[i]).substring((""+r[i]).length)))}return e},U=t=>{if(typeof wx<"u"){const e=getCurrentPages(),n=e[e.length-1];return t?n.options[t]||"":n.options}else{if(typeof window>"u")return t?"":{};if(!t){const r={},o=window.location.search.substring(1);return o&&o.split("&").forEach(i=>{const[s,c]=i.split("=");s&&(r[decodeURIComponent(s)]=decodeURIComponent(c||""))}),r}const e=new RegExp("(^|&)"+t+"=([^&]*)(&|$)"),n=window.location.search.substring(1).match(e);return n!=null?decodeURIComponent(n[2]):""}},F=(t=16)=>{const e=Array(26).fill(0).map((i,s)=>String.fromCharCode(97+s)),n=new Date().getTime();let r=String(n).substring(5).split("");t>26&&(r=String(n).split(""));const o=r.length;for(let i=o;i<t;i++)r.push(e[Math.floor(Math.random()*e.length)]);return r.sort(()=>Math.random()-.5).join("")};function k(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){const e=Math.random()*16|0;return(t==="x"?e:e&3|8).toString(16)})}function j(t){return t&&t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()}function L(t){return t.replace(/_([a-z])/g,(e,n)=>n.toUpperCase())}function v(t){return t.replace(/([A-Z])/g,"_$1").toLowerCase()}function y(t,e=1/0){return e>0?t.reduce((n,r)=>n.concat(Array.isArray(r)?y(r,e-1):[r]),[]):t.slice()}function z(t,e){return t.reduce((n,r)=>{const o=typeof e=="function"?e(r):r[e];return n[o]||(n[o]=[]),n[o].push(r),n},{})}function Y(t,e=1,n=10){const r=(e-1)*n,o=r+n;return t.slice(r,o)}function W(t){const e=new Date,n=new Date(t),r=e.getTime()-n.getTime(),o=Math.floor(r/1e3),i=Math.floor(o/60),s=Math.floor(i/60),c=Math.floor(s/24),a=Math.floor(c/30),f=Math.floor(c/365);return f>0?`${f}年前`:a>0?`${a}个月前`:c>0?`${c}天前`:s>0?`${s}小时前`:i>0?`${i}分钟前`:"刚刚"}function X(t,e=new Date){const n=new Date(t),r=new Date(e),o=Math.abs(r.getTime()-n.getTime());return Math.floor(o/(1e3*60*60*24))}function Z(t,e=new Date){const n=new Date(t),r=new Date(e);return n.getFullYear()===r.getFullYear()&&n.getMonth()===r.getMonth()&&n.getDate()===r.getDate()}function H(t,e=2){if(t===0)return"0 Bytes";const n=1024,r=e<0?0:e,o=["Bytes","KB","MB","GB","TB","PB"],i=Math.floor(Math.log(t)/Math.log(n));return parseFloat((t/Math.pow(n,i)).toFixed(r))+" "+o[i]}function _(t){return String(t).replace(/\B(?=(\d{3})+(?!\d))/g,",")}function q(t,e=2){return(t*100).toFixed(e)+"%"}function J(t){if(typeof btoa<"u")return btoa(unescape(encodeURIComponent(t)));if(typeof Buffer<"u")return Buffer.from(t,"utf8").toString("base64");throw new Error("Base64 encoding not supported in this environment")}function K(t){if(typeof atob<"u")return decodeURIComponent(escape(atob(t)));if(typeof Buffer<"u")return Buffer.from(t,"base64").toString("utf8");throw new Error("Base64 decoding not supported in this environment")}function Q(t){return encodeURIComponent(t)}function G(t){return decodeURIComponent(t)}function V(t,e,n="..."){return!t||t.length<=e?t:t.substring(0,e)+n}function M(t,e){if(t===e)return!0;if(t==null||e==null||typeof t!="object"||typeof e!="object")return!1;const n=Object.keys(t),r=Object.keys(e);if(n.length!==r.length)return!1;for(let o of n)if(!r.includes(o)||!M(t[o],e[o]))return!1;return!0}function tt(t){return Object.prototype.toString.call(t).slice(8,-1)}function u(t){const e=t.toString();if(e.indexOf(".")===-1&&e.indexOf("e")===-1)return 0;if(e.indexOf("e")!==-1){const n=e.split("e"),r=n[0],o=parseInt(n[1],10),i=r.indexOf(".")===-1?0:r.split(".")[1].length;return Math.max(0,i-o)}return e.split(".")[1].length}function et(t,e,...n){const r=u(t),o=u(e),i=Math.max(r,o),s=Math.pow(10,i);let c=Math.round(t*s+e*s)/s;for(const a of n){const f=u(c),h=u(a),p=Math.max(f,h),l=Math.pow(10,p);c=Math.round(c*l+a*l)/l}return c}function nt(t,e,...n){const r=u(t),o=u(e),i=Math.max(r,o),s=Math.pow(10,i);let c=Math.round(t*s-e*s)/s;for(const a of n){const f=u(c),h=u(a),p=Math.max(f,h),l=Math.pow(10,p);c=Math.round(c*l-a*l)/l}return c}function rt(t,e,...n){const r=u(t),o=u(e),i=Math.pow(10,r),s=Math.pow(10,o);let c=Math.round(t*i)*Math.round(e*s)/(i*s);for(const a of n){const f=u(c),h=u(a),p=Math.pow(10,f),l=Math.pow(10,h);c=Math.round(c*p)*Math.round(a*l)/(p*l)}return c}function ot(t,e,...n){if(e===0)throw new Error("Division by zero is not allowed");const r=u(t),o=u(e),i=Math.pow(10,r),s=Math.pow(10,o);let c=Math.round(t*i)/Math.round(e*s);for(const a of n){if(a===0)throw new Error("Division by zero is not allowed");const f=u(c),h=u(a),p=Math.pow(10,f),l=Math.pow(10,h);c=Math.round(c*p)/Math.round(a*l)}return c}function it(t,e){if(!e||typeof e!="string")return!1;const n={object:"Object",array:"Array",string:"String",number:"Number",boolean:"Boolean",function:"Function",date:"Date",regexp:"RegExp",regex:"RegExp",null:"Null",undefined:"Undefined",symbol:"Symbol",promise:"Promise",error:"Error",asyncfunction:"AsyncFunction",async:"AsyncFunction"},r=e.toLowerCase(),o=n[r]||e,i=Object.prototype.toString.call(t).slice(8,-1);return r==="undefined"&&t===void 0?!0:r==="promise"?i==="Promise"||t!==null&&typeof t=="object"&&typeof t.then=="function"&&typeof t.catch=="function":r==="function"?i==="Function"||i==="AsyncFunction":r==="date"?i==="Date"&&!isNaN(t.getTime()):r==="number"?i==="Number"&&!isNaN(t):i===o}const w={random:E,formatTime:O,getDayOfWeek:g,getTimeUntilEnd:T,formatCurrency:A,convertToChinese:N,generateRandomCode:S,debounce:C,throttle:I,getUrlParam:U,deepClone:m,mergeObjects:$,uniqueArray:R,checkPwd:B,randomStr:F,generateUUID:k,capitalize:j,camelCase:L,snakeCase:v,flattenArray:y,groupBy:z,paginate:Y,getRelativeTime:W,getDaysDiff:X,isSameDay:Z,formatFileSize:H,formatNumber:_,toPercent:q,base64Encode:J,base64Decode:K,urlEncode:Q,urlDecode:G,truncate:V,deepEqual:M,getType:tt,isType:it,add:et,subtract:nt,multiply:rt,divide:ot};function st(t){return/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(t)}function ct(t){return/^1\d{10}$/.test(t)}function x(t){if(t.length!==15)return t;const e=t.substring(0,6)+"19"+t.substring(6),n=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2],r=e.split("").reduce((i,s,c)=>i+parseInt(s)*n[c],0),o="10X98765432".charAt(r%11);return e+o}function at(t){if(!/^\d{15}$|^\d{17}(\d|X|x)$/.test(t))return!1;let e=t;t.length===15&&(e=x(t));const n=e.substring(6,14);return b(n)?P(e):!1}function P(t){const e=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2],n=t.slice(0,17).split("").reduce((o,i,s)=>o+parseInt(i)*e[s],0),r="10X98765432".charAt(n%11);return t.charAt(17).toUpperCase()===r}function b(t){const e=parseInt(t.substring(0,4),10),n=parseInt(t.substring(4,6),10),r=parseInt(t.substring(6,8),10),o=new Date(e,n-1,r);return o.getFullYear()===e&&o.getMonth()===n-1&&o.getDate()===r}function ut(t){try{return new URL(t),!0}catch{return!1}}function lt(t){return/^(\d{1,3}\.){3}\d{1,3}$/.test(t)?t.split(".").every(n=>{const r=parseInt(n,10);return r>=0&&r<=255}):!1}function ft(t){if(!/^\d{13,19}$/.test(t))return!1;let e=0,n=!1;for(let r=t.length-1;r>=0;r--){let o=parseInt(t[r],10);n&&(o*=2,o>9&&(o-=9)),e+=o,n=!n}return e%10===0}function dt(t){return/^[\u4e00-\u9fa5]{2,4}$/.test(t)}function pt(t){return/^[1-9]\d{5}$/.test(t)}function ht(t){return/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-Z0-9]{4,5}[A-Z0-9挂学警港澳]$/.test(t)}function gt(t){if(!/^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/.test(t))return!1;const e=[1,3,9,27,19,26,16,17,20,29,25,13,8,24,10,30,28],n="0123456789ABCDEFGHJKLMNPQRTUWXY";let r=0;for(let i=0;i<17;i++){const s=t[i],c=n.indexOf(s);if(c===-1)return!1;r+=c*e[i]}const o=(31-r%31)%31;return t[17]===n[o]}function mt(t){return!isNaN(Number(t))&&!isNaN(parseFloat(String(t)))}function yt(t){return Number.isInteger(Number(t))}function Mt(t){return/^-?\d+\.\d+$/.test(String(t))}function wt(t){return!!(t==null||typeof t=="string"&&t.trim()===""||Array.isArray(t)&&t.length===0||typeof t=="object"&&Object.keys(t).length===0)}function xt(t,e,n){const r=t.length;return r>=e&&r<=n}function Pt(t){return/^[1-9]\d*$/.test(String(t))}function bt(t){return/^\d+$/.test(String(t))}function Dt(t){return/^[\u4e00-\u9fa5]+$/.test(t)}function Tt(t){return/^[a-zA-Z]+$/.test(t)}const D={isEmail:st,isMobile:ct,validateIDCard:at,checkCode:P,isValidDate:b,convertTo18Digits:x,isURL:ut,isIP:lt,isBankCard:ft,isChineseName:dt,isPostalCode:pt,isLicensePlate:ht,isUnifiedSocialCreditCode:gt,isNumber:mt,isInteger:yt,isDecimal:Mt,isEmpty:wt,isLength:xt,isPositiveInteger:Pt,isNonNegativeInteger:bt,isChinese:Dt,isEnglish:Tt};(function(t,e){typeof define=="function"&&define.amd?define([],e):typeof d=="object"&&typeof module<"u"?module.exports=e():t.crossEnvPlugins=e()})(typeof self<"u"?self:typeof globalThis<"u"?globalThis:typeof global<"u"?global:{},function(){return{utils:w,valids:D}}),d.utils=w,d.valids=D,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});