fenzhi-utils
Version:
分值前端项目的js函数库
2 lines (1 loc) • 14.9 kB
JavaScript
!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).Fenzhi={})}(this,(function(e){"use strict";function t(e){return!isNaN(parseFloat(e))&&isFinite(e)}function n(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;if(isNaN(parseFloat(e))||parseFloat(e)===1/0)return"";var n=e.toString();if(n.includes("e")){var r=n.match(/\d(?:\.(\d*))?e([+-]\d+)/);n=e.toFixed(Math.max(0,(r[1]||"").length-r[2]))}var o=!1;n.startsWith("-")&&(o=!0,n=n.slice(1));var i=n.indexOf(".");if(-1===i||n.length-(i+1)<=t)return e.toFixed(t);var u=n.match(/\d/g)||[];if(u=u.slice(0,i+t+1),parseInt(u[u.length-1],10)>4)for(var a=u.length-2;a>=0&&(u[a]=String(parseInt(u[a],10)+1),"10"===u[a]);a--)u[a]="0";return u.splice(i,0,"."),u.pop(),o&&u.unshift("-"),Number(u.join("")).toFixed(t)}function r(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2;if(!t(e))return"";var o=(e=parseFloat(e))>=0?"":"-",i=Math.abs(e);return i>=1e8?i=n(i/1e8,r):i>=1e4&&(i=n(i/1e4,r)),o+i}function o(e){if(!t(e))return"";var n="";return(e=Math.abs(e))>=1e8?n="亿":e>=1e4&&(n="万"),n}function i(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function u(e,t){return e(t={exports:{}},t.exports),t.exports}var a=u((function(e){e.exports=function(e){if(Array.isArray(e))return e},e.exports.__esModule=!0,e.exports.default=e.exports}));i(a);var l=u((function(e){e.exports=function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,o,i,u,a=[],s=!0,l=!1;try{if(i=(n=n.call(e)).next,0===t){if(Object(n)!==n)return;s=!1}else for(;!(s=(r=i.call(n)).done)&&(a.push(r.value),a.length!==t);s=!0);}catch(e){l=!0,o=e}finally{try{if(!s&&null!=n.return&&(u=n.return(),Object(u)!==u))return}finally{if(l)throw o}}return a}},e.exports.__esModule=!0,e.exports.default=e.exports}));i(l);var c=u((function(e){e.exports=function(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r},e.exports.__esModule=!0,e.exports.default=e.exports}));i(c);var f=u((function(e){e.exports=function(e,t){if(e){if("string"==typeof e)return c(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?c(e,t):void 0}},e.exports.__esModule=!0,e.exports.default=e.exports}));i(f);var p=u((function(e){e.exports=function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")},e.exports.__esModule=!0,e.exports.default=e.exports}));i(p);var d=i(u((function(e){e.exports=function(e,t){return a(e)||l(e,t)||f(e,t)||p()},e.exports.__esModule=!0,e.exports.default=e.exports})));function h(e){return Number.isNaN(e)&&isNaN(e)?"nan":Object.prototype.toString.call(e).slice(8,-1).toLowerCase()}var g=function(e){return 4===e.length?e.substr(1).split("").map((function(e){return 17*parseInt(e,16)})):[e.substr(1,2),e.substr(3,2),e.substr(5,2)].map((function(e){return parseInt(e,16)}))},m=function(e){return 1===e.length?"0"+e:e};var v=u((function(e){function t(n){return e.exports=t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e.exports.__esModule=!0,e.exports.default=e.exports,t(n)}e.exports=t,e.exports.__esModule=!0,e.exports.default=e.exports})),y=i(v),x={},b=function(e){var t={waterText:"",waterTextColor:"#EBEBEB",waterTextFontSize:20,waterElID:"CustomWater-1.23452384164.123412415",waterAreaWidth:document.documentElement.clientWidth,waterAreaHeight:document.documentElement.clientHeight,waterAreaPosition:"fixed",waterAreazIndex:"9999999",waterMountElement:document.body,waterAreaOpacity:.2,waterAreaRotate:-20*Math.PI/150,singleWidth:200,singleHeight:200,positionTop:"0px",positionleft:"0px"};if("string"==typeof e)t.waterText=e;else{if("object"!==y(e))return!1;t=Object.assign({},t,e)}var n=t.waterElID;n.startsWith("CustomWater")||(n="CustomWater-"+n),null!==document.getElementById(n)&&document.body.removeChild(document.getElementById(n));var r=document.createElement("canvas"),o=20*t.waterText.length;t.singleWidth=Math.max(t.singleWidth,o);var i=18*t.waterText.length;t.singleHeight=Math.max(t.singleHeight,i),r.width=t.singleWidth,r.height=t.singleHeight;var u=r.getContext("2d");u.rotate(t.waterAreaRotate),u.fillStyle=t.waterTextColor,u.font=t.waterTextFontSize+"px Vedana",u.textAlign="center",u.textBaseline="alphabetic",u.fillText(t.waterText,r.width/3,r.height/2);var a=document.createElement("div");return a.id=n,a.style.opacity=t.waterAreaOpacity,a.style.pointerEvents="none",a.style.top=t.positionTop,a.style.left=t.positionleft,a.style.position=t.waterAreaPosition,a.style.zIndex=t.waterAreazIndex,a.style.width=t.waterAreaWidth+"px",a.style.height=t.waterAreaHeight+"px",a.style.background="url("+r.toDataURL("image/png")+") left top repeat",t.waterMountElement.appendChild(a),n};function w(e){x.set(e)}x.set=function(e){var t=b(e),n=setInterval((function(){null===document.getElementById(t)?t=b(e):clearTimeout(n)}),500);window.onresize=function(){b(e)}},w.prototype.destroyed=function(){for(var e=document.querySelectorAll('[id^="CustomWater-"]'),t=0;t<e.length;t++)e[t].remove()},e.CustomArrayFlatter=function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"children",r=arguments.length>2&&void 0!==arguments[2]&&arguments[2],o=[];return JSON.parse(JSON.stringify(t)).forEach((function(t){var i=t[n];r||delete t[n],o.push(t),Array.isArray(i)&&(o=o.concat(e(i,n,r)))})),o},e.CustomArraySort=function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"desc";return Array.isArray(e)&&"string"==typeof t&&t.trim()&&["asc","desc"].includes(n)?e.sort((function(r,o){if(isNaN(parseFloat(r[t])))return 1;if(isNaN(parseFloat(o[t])))return-1;var i=parseFloat(r[t]),u=parseFloat(o[t]);return i===u?e.indexOf(r)-e.indexOf(o):function(e,t){return"asc"===n?e-t:t-e}(i,u)})):e},e.CustomArraySumField=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],t=arguments.length>1?arguments[1]:void 0;return e.length?[0,1].includes(arguments.length)?0:e.reduce((function(e,n){var r=0;return[null,"null","","undefined",void 0,1/0].includes(n[t])?r=0:"number"==typeof n[t]?r=n[t]:"string"==typeof n[t]?r=parseInt(n[t],10):"boolean"==typeof n[t]&&(r=n[t]?1:0),e+r}),0):0},e.CustomArraySumSignle=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];return e.length?e.reduce((function(e,t){var n=0;return[null,"null","","undefined",void 0,1/0].includes(t)?n=0:"number"==typeof t?n=t:"string"==typeof t?n=parseInt(t,10):"boolean"==typeof t&&(n=t?1:0),e+n}),0):0},e.CustomArrayToTree=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id",n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"pid",r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"children",o=new Map,i=[];return e.forEach((function(e){o.set(e[t],e)})),e.forEach((function(e){var t=o.get(e[n]);t?(t[r]||(t[r]=[]),t[r].push(e)):i.push(e)})),i},e.CustomByteConversion=function(e){var t="",n=(t=e<102.4?e.toFixed(2)+"B":e<104857.6?(e/1024).toFixed(2)+"KB":e<107374182.4?(e/1048576).toFixed(2)+"MB":(e/1073741824).toFixed(2)+"GB")+"",r=n.indexOf(".");return"00"==n.substr(r+1,2)?n.substring(0,r)+n.substr(r+3,2):t},e.CustomConverPercent=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2;return e=parseFloat(e),isNaN(e)||e===1/0?"":(e*=100).toFixed(t)},e.CustomDateFtt=function(e,t){if(null!=e&&""!=e&&null!=e){"string"==typeof e&&(e=e.includes("T")?new Date(e):e.replace(/-/g,"/"));var n={"M+":(e=new Date(e)).getMonth()+1,"d+":e.getDate(),"h+":e.getHours(),"m+":e.getMinutes(),"s+":e.getSeconds(),"q+":Math.floor((e.getMonth()+3)/3),S:e.getMilliseconds()};for(var r in n.S<100&&(n.S=n.S<10?"00"+n.S:"0"+n.S),/(y+)/.test(t)&&(t=t.replace(RegExp.$1,(e.getFullYear()+"").substr(4-RegExp.$1.length))),n)new RegExp("("+r+")").test(t)&&(t=t.replace(RegExp.$1,1==RegExp.$1.length?n[r]:("00"+n[r]).substr((""+n[r]).length)))}else t="";return t},e.CustomDebounce=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:100,n=arguments.length>2&&void 0!==arguments[2]&&arguments[2],r=null;return function(){var o=this,i=arguments;if(clearTimeout(r),n&&null===r)return e.apply(o,i),void(r=0);r=setTimeout((function(){e.apply(o,i),r=null}),t)}},e.CustomDeepClone=function(e){var t,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:new WeakMap;if(Object(e)!==e||e instanceof Function||e instanceof RegExp||e instanceof Error||e instanceof Date)return e;if(n.has(e))return n.get(e);try{t=new e.constructor}catch(n){t=Object.create(Object.getPrototypeOf(e))}return n.set(e,t),e instanceof Map?Array.from(e,(function(e){var r=d(e,2),o=r[0],i=r[1];return t.set(deepClone(o,n),deepClone(i,n))})):e instanceof Set?Array.from(e,(function(e){return t.add(deepClone(e,n))})):Array.isArray(e)?t=e.map((function(e){return deepClone(e,n)})):(t=Object.getOwnPropertyNames(e).reduce((function(t,r){var o=Object.getOwnPropertyDescriptor(e,r);return void 0!==o.value&&"function"!=typeof o.value?o.value=deepClone(o.value,n):(o.get||o.set)&&(o.get=deepClone(o.get,n),o.set=deepClone(o.set,n)),Object.defineProperty(t,r,o),t}),t),Object.setPrototypeOf(t,deepClone(Object.getPrototypeOf(e),n))),e instanceof Map?Object.setPrototypeOf(t,Map.prototype):e instanceof Set&&Object.setPrototypeOf(t,Set.prototype),t},e.CustomGetTypeOf=h,e.CustomGradientColors=function(e,t,n,r){if(0===n)return[];if(1===n)return[e];var o,i,u,a,s=[],l=[];r=r||1;var c=function(e){return Math.pow(e/255,r)};for(e=g(e).map(c),t=g(t).map(c),o=0;o<n;o++){for(a=1-(u=o/(n-1)),i=0;i<3;i++)l[i]=m(Math.round(255*Math.pow(e[i]*a+t[i]*u,1/r)).toString(16));s.push("#"+l.join(""))}return s},e.CustomHideStrGeneral=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=arguments.length>1?arguments[1]:void 0,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"*",r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"*";try{if("string"!==h(e))throw new Error("请传入字符串");if(!e.length)return e;if("array"===h(t)){if(function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];if(!e.length)return!1;var t=!0;return e.forEach((function(e){var n="number"===h(e),r=Number.isInteger(e);n&&r&&e>-1||(t=!1)})),t}(t))return e.split("").reduce((function(e,r,o){return e+(t.includes(o)?n:r)}),"");if(function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];if(!e.length)return!1;var t=!0;return e.forEach((function(e){var n="number"===h(e[0]),r="number"===h(e[1]),o=Number.isInteger(e[0])&&e[0]>-1,i=Number.isInteger(e[1])&&e[1]>-1,u=e[0]<=e[1];n&&r&&o&&i&&u||(t=!1)})),t}(t))return t.forEach((function(t){e=e.split("").reduce((function(e,r,o){return e+(t[0]<=o&&o<=t[1]?n:r)}),"")})),e;throw new Error("请检查数组格式")}if("number"===h(t)){var o=Number.isInteger(t),i=Number.isInteger(n);if("number"===h(n)&&o&&i){if(t>n)throw new Error("字符串截取开始位置不能大于结束位置");return t=t>0?t:0,n=n>e.length?e.length:n,e.split("").reduce((function(e,o,i){return e+(t<=i&&i<=n?r:o)}),"")}throw new Error("第二个参数、第三个参数需为非负整数")}return e}catch(e){console.log(e)}},e.CustomIsNumeric=t,e.CustomMonthRate=function(e){var t;if(arguments.length){if([null,"",void 0].includes(e))return["",""];t=new Date(e)}else t=new Date;var n=t.getFullYear(),r=t.getMonth()+1,o=new Date(n,r,0).getDate();return["".concat(n,"-").concat(r,"-01"),"".concat(n,"-").concat(r,"-").concat(o)]},e.CustomNumberNoFixed=function(e,t){var n=parseFloat(e);return isNaN(n)?"-":~~(Math.pow(10,t)*n)/Math.pow(10,t)},e.CustomNumberToFixed=n,e.CustomPasswordVerifiersFn=function(e){var t=e.split(""),n=[],r="ABCDEFGHIJKLMNOPQRSTUVWXYZ",o="abcdefghijklmnopqrstuvwxyz",i="0123456789",u="!@#$&*.",a=[],s=[],l=[],c=[],f=r.concat(o).concat(i).concat(u);t.forEach((function(e){f.includes(e)?r.includes(e)?a.push(e):o.includes(e)?s.push(e):i.includes(e)?l.push(e):u.includes(e)&&c.push(e):n.push(e)}));var p=[];return t.length<8&&p.push("密码长度小于8位"),t.length>12&&p.push("密码长度大于12位"),!a.length&&p.push("未包含大写字母"),!s.length&&p.push("未包含小写字母"),!l.length&&p.push("未包含数字"),!c.length&&p.push("未包含特殊符号"),n.length&&p.push("包含未指定字符:".concat(p.join(""))),p.length?(p.unshift("密码校验存在以下问题:"),p.push(""),p.push("温馨提示:"),p.push("密码规则:8-12位,包含大小写字母、数字、特殊符号"),p.push("大写字母取值范围:".concat(r)),p.push("小写字母取值范围:".concat(o)),p.push("数字取值范围:".concat(i)),p.push("特殊符号取值范围:".concat(u)),{isTrue:!1,message:p.join("\n")}):{isTrue:!0,message:"验证通过"}},e.CustomPersonNameFn=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"*";try{if("string"!==h(e))throw new Error("传入的姓名为非字符串");return[0,1].includes(e.length)?e:[2].includes(e.length)?e.charAt(0)+t:e.split("").reduce((function(n,r,o){return n+([0,e.length-1].includes(o)?r:t)}),"")}catch(e){console.log(e)}},e.CustomTendToNumber=r,e.CustomTendToTurn=function(e){return r(e,arguments.length>1&&void 0!==arguments[1]?arguments[1]:2)+o(e)},e.CustomTendToUnit=o,e.CustomTendToWan=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2;if(!t(e))return"";var o=(e=parseFloat(e))>=0?"":"-",i=Math.abs(e);return o+(i=n(i/1e4,r))},e.CustomThousandSplit=function(e,t){if([null,void 0].includes(e))return"";var n="-"===(e=String(e))[0]?"-":"",r=Math.abs(parseFloat(e));return n+Math.floor(r).toString().replace(/\B(?=(\d{3})+(?!\d))/g,",")+(void 0!==t?"."+r.toFixed(t).split(".")[1]:"")},e.CustomThrottle=function(e,t){var n=0;return function(){var r=(new Date).getTime();r-n<t||(n=r,e.apply(void 0,arguments))}},e.CustomUrlParam=function(e){var t=new RegExp("(^|&)"+e+"=([^&]*)(&|$)"),n=window.location.search.substr(1).match(t);return null!=n?unescape(n[2]):null},e.CustomValidateEmail=function(e){return/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e)},e.CustomValidatePhone=function(e){return/^1[3456789]\d{9}$/.test(e)},e.CustomWatermark=w,e.CustomWeekTime=function(e,t){var n=new Date(t),r=n.getFullYear(),o=n.getMonth()+1,i=n.getDate(),u=n.getDay();return e+=0!==u?u-1:u,u&&(o>1||(r-=1,o=12)),n.setDate(n.getDate()-e),r=n.getFullYear(),o=n.getMonth()+1,i=n.getDate(),s=r+"-"+(o<10?"0"+o:o)+"-"+(i<10?"0"+i:i),s}}));