UNPKG

native-lodash

Version:
2 lines (1 loc) 8.57 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).nativeLodash={})}(this,(function(t){"use strict";const e=t=>{if(!p(t))return t;if(Array.isArray(t))return t.map((t=>e(t)));if(m(t)){const n={};return Object.keys(t).forEach((r=>{n[r]=e(t[r])})),n}return t};const n=t=>t.flatMap((t=>Array.isArray(t)?n(t):t));const r=Object.prototype.hasOwnProperty,o=(t,e)=>{const n=e.split(".");return!!t&&(n.length>1?o(t[e.split(".")[0]],n.slice(1).join(".")):r.call(t,e))};function i(t){return h(t)||m(t)}function s(t,e=15){return+parseFloat(Number(t).toPrecision(e))}function c(t){const e=t.toString().split(/[Ee]/),n=(e[0].split(".")[1]||"").length-+(e[1]||0);return n>0?n:0}function a(t){if(!t.toString().includes("e"))return Number(t.toString().replace(".",""));const e=c(t);return e>0?s(Number(t)*10**e):Number(t)}function u(t){(Number(t)>Number.MAX_SAFE_INTEGER||Number(t)<Number.MIN_SAFE_INTEGER)&&console.warn(`${t} 超出了精度限制,结果可能不正确`)}function l(t,e){const[n,r,...o]=t;let i=e(n,r);return o.forEach((t=>{i=e(i,t)})),i}function d(...t){if(t.length>2)return l(t,d);const[e,n]=t,r=a(e),o=a(n),i=c(e)+c(n),s=r*o;return u(s),s/10**i}function f(...t){if(t.length>2)return l(t,f);const[e,n]=t,r=a(e),o=a(n);return u(r),u(o),d(r/o,s(10**(c(n)-c(e))))}const p=t=>null!=t;function g(t){return null!=t&&"string"==typeof t.valueOf()}function h(t){return"function"==typeof Array.isArray?Array.isArray(t):"[object Array]"===Object.prototype.toString.call(t)}function m(t){return"[object Object]"===Object.prototype.toString.call(t)}const y=t=>"function"==typeof t,b="undefined"!=typeof window;function w(t,e="yyyy-mm-dd"){let n;n=t?/^\d{10}$/.test(null==t?void 0:t.toString().trim())?new Date(1e3*+t):"string"==typeof t&&/^\d+$/.test(t.trim())?new Date(Number(t)):"string"==typeof t&&t.includes("-")&&!t.includes("T")?new Date(t.replace(/-/g,"/")):new Date(t):new Date;const r={y:n.getFullYear().toString(),m:(n.getMonth()+1).toString().padStart(2,"0"),d:n.getDate().toString().padStart(2,"0"),h:n.getHours().toString().padStart(2,"0"),M:n.getMinutes().toString().padStart(2,"0"),s:n.getSeconds().toString().padStart(2,"0")};return Object.keys(r).forEach((t=>{const[n]=new RegExp(`${t}+`).exec(e)||[];if(n){const o="y"===t&&2===n.length?2:0;e=e.replace(n,r[t].slice(o))}})),e}t.clipboard=t=>navigator.clipboard.writeText(t),t.cloneDeep=e,t.debounce=(t,e=0,n=!1)=>{let r,o=!1;return function(...i){r&&clearTimeout(r),n&&!o?(Reflect.apply(t,this,i),o=!0):r=setTimeout((()=>{Reflect.apply(t,this,i),o=!1}),e)}},t.delay=function(t,e=0,...n){if(!y(t))throw new TypeError("Expected a function");return setTimeout(t,e,...n)},t.desensitize=function(t,e=3,n=4,r="*"){if(!t)return t;const o=new RegExp(`^(.{${e}})(.*)(.{${n}})$`);return String(t).replace(o,((t,e,n,o)=>e+r.repeat(n.length)+o))},t.download=function(t,e){const n=document.createElement("a");n.style.display="none",n.setAttribute("href",t),n.setAttribute("download",e||"");const r=Math.random().toString(36).slice(2);n.id=r,document.body.append(n),n.click(),n.remove()},t.downloadImage=function(t,e){const n=new Image;n.setAttribute("crossOrigin","anonymous"),n.addEventListener("load",(()=>{const t=document.createElement("canvas");t.width=n.width,t.height=n.height;const r=t.getContext("2d");null==r||r.drawImage(n,0,0,n.width,n.height);const o=t.toDataURL("image/png"),i=document.createElement("a"),s=new MouseEvent("click");i.download=e||"photo",i.href=o,i.dispatchEvent(s)})),n.src=t},t.eq=function(t,e){return t===e||t!=t&&e!=e},t.flattenDeep=n,t.getUa=function(){const t=window.navigator.userAgent.toLowerCase();return/iphone|ipod|ipad/i.test(t)?"ios":/android/i.test(t)?"android":/micromessenger/i.test(t)?"wechat":/micromessenger/i.test(t)&&/wxwork/i.test(t)?"wxwork":/micromessenger/i.test(t)&&/miniprogram/i.test(t)?"wxmini":/dingtalk/i.test(t)?"dingding":/lark/i.test(t)?"feishu":/applewebkit.*mobile.*/gi.test(t)?"mobile":void 0},t.getUrlAllParams=function(t){const e=(t=t||decodeURIComponent(window.location.href)).slice(Math.max(0,t.indexOf("?")+1)).split("&"),n={};for(let t=0,r=e.length;t<r;t++){const r=e[t].indexOf("=");if(-1===r)continue;const o=e[t].slice(0,Math.max(0,r)),i=window.decodeURIComponent(e[t].slice(Math.max(0,r+1)));n[o]=i}return n},t.getUrlParams=function(t,e){const n=new RegExp(`(^|&)${t}=([^&]*)(&|$)`);let r=null;return r=e?e.split("?")[1].match(n):window.location.search.slice(1).match(n),null!=r?decodeURIComponent(r[2]):null},t.groupBy=function(t,e){return t.reduce(((t,n)=>{const r=n[e];return t[r]||(t[r]=[]),t[r].push(n),t}),{})},t.guid=function(t=32,e="u",n=62){const r="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(""),o=[];if(t)for(let e=0;e<t;e++)o[e]=r[0|Math.random()*n];else{let t;o[8]=o[13]=o[18]=o[23]="-",o[14]="4";for(let e=0;e<36;e++)o[e]||(t=0|16*Math.random(),o[e]=r[19===e?3&t|8:t])}return e?(o.shift(),`${e}${o.join("")}`):o.join("")},t.has=o,t.inBrowser=b,t.isArray=h,t.isCarNo=function(t){const e=/^[A-Z云京使冀吉宁川新晋桂沪津浙渝湘琼甘皖粤苏蒙藏豫贵赣辽鄂闽陕青领鲁黑][A-Z]((\d{5}[DF]$)|([DF][\dA-HJ-NP-Z]\d{4}$))/,n=/^[A-Z云京使冀吉宁川新晋桂沪津浙渝湘琼甘皖粤苏蒙藏豫贵赣辽鄂闽陕青领鲁黑][A-Z][\dA-HJ-NP-Z]{4}[\dA-HJ-NP-Z学挂港澳警]$/;return 7===t.length?n.test(t):8===t.length&&e.test(t)},t.isCode=function(t,e=6){return new RegExp(`^\\d{${e}}$`).test(t)},t.isDef=p,t.isEmail=function(t){return/^\w+((-\w+)|(\.\w+))*@[\dA-Za-z]+(([.-])[\dA-Za-z]+)*\.[\dA-Za-z]+$/.test(t)},t.isEmpty=function(t){return""===t||t.constructor===Object&&0===Object.keys(t).length||Array.isArray(t)&&0===t.length},t.isEqual=function t(e,n){if(!i(e)||!i(n))return e===n;if(e===n)return!0;const r=Object.keys(e),o=Object.keys(n);if(r.length!==o.length)return!1;for(const r in e){if(!t(e[r],n[r]))return!1}return!0},t.isFunction=y,t.isIdCard=function(t){return/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0-2|]\d)|3[01])\d{3}(\d|X)$/.test(t)},t.isMobile=function(t){return/^1[3-9]\d{9}$/.test(String(t))},t.isObject=m,t.isString=g,t.isTrue=t=>!0===t||"true"===t,t.isURL=t=>/^((ht|f)tps?):\/\/?/.test(t),t.later=function(t=0){return new Promise((e=>{setTimeout(e,t)}))},t.moneyFormat=function(t,e=0){t=`${t}`.replace(/[^\d+-Ee]/g,"");const n=isFinite(+t)?+t:0,r=isFinite(+e)?Math.abs(e):0;let o=[];o=(r?`${function(t,e){const n=10**e;let r=f(Math.round(Math.abs(d(t,n))),n);return t<0&&0!==r&&(r=d(r,-1)),r}(n,r)}`:`${Math.round(n)}`).split(".");const i=/(-?\d+)(\d{3})/;for(;i.test(o[0]);)o[0]=o[0].replace(i,"$1,$2");return(o[1]||"").length<r&&(o[1]=o[1]||"",o[1]+=Array.from({length:r-o[1].length+1}).join("0")),o.join(".")},t.omit=function(t,e){if(!m(t))return{};g(e)&&(e=[e]);const n=Object.keys(t),r={};for(const o of n){const n=t[o];e&&e.includes(o)||(r[o]=n)}return r},t.queryParams=function(t={},e=!0,n="brackets"){const r=e?"?":"",o=[];["indices","brackets","repeat","comma"].includes(n)||(n="brackets");for(const e in t){const r=t[e];if(p(r))if(Array.isArray(r))switch(n){case"indices":for(const[t,n]of r.entries())o.push(`${e}[${t}]=${n}`);break;case"brackets":r.forEach((t=>{o.push(`${e}[]=${t}`)}));break;case"repeat":r.forEach((t=>{o.push(`${e}=${t}`)}));break;case"comma":let t="";r.forEach((e=>{t+=(t?",":"")+e})),o.push(`${e}=${t}`)}else o.push(`${e}=${r}`)}return o.length>0?r+o.join("&"):""},t.throttle=(t,e=0)=>{let n=0;return function(...r){Date.now()-n>e&&(Reflect.apply(t,this,r),n=Date.now())}},t.timeFormat=w,t.timeFrom=function(t,e=""){t||(t=Date.now()),10===(t=parseInt(String(t))).toString().length&&(t*=1e3);let n=Date.now()-t;n=parseInt(String(n/1e3));let r="";switch(!0){case n<60:r="刚刚";break;case n>=60&&n<3600:r=`${parseInt(String(n/60))}分钟前`;break;case n>=3600&&n<86400:r=`${parseInt(String(n/3600))}小时前`;break;case n>=86400&&n<172800:r="昨天";break;case n>=172800&&n<2592e3:r=`${parseInt(String(n/86400))}天前`;break;default:r=e?w(t,e):n>=2592e3&&n<31536e3?`${parseInt(String(n/2592e3))}个月前`:`${parseInt(String(n/31536e3))}年前`}return r},t.toFixed=function(t,e=2){const n=10**e;return(Math.round(Number(t)*n)/n).toFixed(e)},t.trim=function(t,e="both"){return t=String(t),"both"===e?t.replace(/^\s+|\s+$/g,""):"left"===e?t.replace(/^\s*/,""):"right"===e?t.replace(/(\s*$)/g,""):"all"===e?t.replace(/\s+/g,""):t},t.uniqBy=function(t,e){if(!e)return[...new Set(t)];const n=g(e)?t=>t[e]:e;return t.reduce(((t,e)=>(t.some((t=>n(t)===n(e)))||t.push(e),t)),[])}}));