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