@3r/tool
Version:
🏃包含一些常用方法例如对象深克隆、递归调用、一一对比/数组交集、并集、差集/二维向量点乘、叉乘/股票KDJ、MACD、RSI、BOLL/验证为空、车牌号、邮箱、身份证、统一社会信用代码、手机号、版本对比/转换日期、星座、身份证解析、字节/随机颜色、手机号、身份证号码、统一社会信用代码...持续更新整合
1 lines • 8.56 kB
JavaScript
var n;class e{static encode(t){let e="",r,o,a,n,i,s,p,l=0;for(t=this._utf8_encode(t);l<t.length;)r=t.charCodeAt(l++),o=t.charCodeAt(l++),a=t.charCodeAt(l++),n=r>>2,i=(3&r)<<4|o>>4,s=(15&o)<<2|a>>6,p=63&a,isNaN(o)?s=p=64:isNaN(a)&&(p=64),e=e+this.keyStr.charAt(n)+this.keyStr.charAt(i)+this.keyStr.charAt(s)+this.keyStr.charAt(p);return e}static decode(t){let e="";var r,o,a,n,i,s;let p=0;for(t=t.replace(/[^A-Za-z0-9\+\/\=]/g,"");p<t.length;)a=this.keyStr.indexOf(t.charAt(p++)),r=(15&(n=this.keyStr.indexOf(t.charAt(p++))))<<4|(i=this.keyStr.indexOf(t.charAt(p++)))>>2,o=(3&i)<<6|(s=this.keyStr.indexOf(t.charAt(p++))),e+=String.fromCharCode(a<<2|n>>4),64!==i&&(e+=String.fromCharCode(r)),64!==s&&(e+=String.fromCharCode(o));return e=this._utf8_decode(e)}static _utf8_encode(e){e=e.replace(/\r\n/g,"\n");let r="";for(let t=0;t<e.length;t++){var o=e.charCodeAt(t);o<128?r+=String.fromCharCode(o):r=127<o&&o<2048?(r+=String.fromCharCode(o>>6|192))+String.fromCharCode(63&o|128):(r=(r+=String.fromCharCode(o>>12|224))+String.fromCharCode(o>>6&63|128))+String.fromCharCode(63&o|128)}return r}static _utf8_decode(t){let e="",r=0;var o,a;let n=0;for(;r<t.length;)(o=t.charCodeAt(r))<128?(e+=String.fromCharCode(o),r++):191<o&&o<224?(n=t.charCodeAt(r+1),e+=String.fromCharCode((31&o)<<6|63&n),r+=2):(n=t.charCodeAt(r+1),a=t.charCodeAt(r+2),e+=String.fromCharCode((15&o)<<12|(63&n)<<6|63&a),r+=3);return e}}e.keyStr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";class r{static hexadecimalToRadix(t){return+("0x"+t)}static usciToOibc(t){return 18!==t.length?"":t.substring(8,16)+"-"+t.substring(16,17)}static timeFormat(t,e="yyyy-MM-dd hh:mm:ss"){var r,o={"M+":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()};let a=/(y+)/.exec(e);for(r in a&&(e=e.replace(a[0],(t.getFullYear()+"").substring(4-a[0].length))),o)(a=new RegExp("("+r+")").exec(e))&&(e=e.replace(a[0],1===a[0].length?o[r]:("00"+o[r]).substring((""+o[r]).length)));return e}static thousands(t){t=(t+"").split(".");let e=t[0].replace(/\d{1,3}(?=(\d{3})+$)/g,t=>t+",");return 1<t.length&&(e+="."+t[1]),e}static textToBase64(t){return e.encode(t)}static base64ToText(t){return e.decode(t)}static jsonToBase64(t){return this.textToBase64(JSON.stringify(t))}static base64ToJson(t){return JSON.parse(this.base64ToText(t))}static hexToRgb(t){let e="";var r=t.length;if((/^#[0-9a-fA-F]{6}$/.test(t)||/^#[0-9a-fA-F]{3}$/.test(t))&&(e=t.substring(1,r)),e=/^[0-9a-fA-F]{6}$/.test(t)||/^[0-9a-fA-F]{3}$/.test(t)?t.substring(0,r):e)return`rgb(${this.hexadecimalToRadix(4<r?e.substring(0,2):e[0]+e[0])},${this.hexadecimalToRadix(4<r?e.substring(2,4):e[1]+e[1])},${this.hexadecimalToRadix(4<r?e.substring(4,6):e[2]+e[2])})`;throw new TypeError("input data type error.")}static rgbToHex(t){let e=Array.isArray(t)?t:[];if("string"==typeof t&&(t=(t=t.replace(/ /g,"")).replace(/[rgb\(\)]/g,""),e=t.split(",")),3!==e.length||e.some(t=>+t<0||255<+t))throw new TypeError("input data type error.");return"#"+e.map(t=>(+t).toString(16)).map(t=>t[1]?t:"0"+t).join("")}static xmlToText(t){return t.replace(/<[^>]+>/g,"")}static numToAmountInWords(r){var e=["角","分"],o=["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"],a=[["圆","万","亿"],["","拾","佰","仟"]],t=Number(r);if(isNaN(t))return"";t=r<0?"欠":"";r=Math.abs(r);let n="";for(let t=0;t<e.length;t++)n+=(o[Math.floor(100*r/10*Math.pow(10,t))%10]+e[t]).replace(/零./,"");n=n||"整",r=Math.floor(r);for(let t=0;t<a[0].length&&0<r;t++){let e="";for(let t=0;t<a[1].length&&0<r;t++)e=o[r%10]+a[1][t]+e,r=Math.floor(r/10);n=e.replace(/(零.)*零$/,"").replace(/^$/,"零")+a[0][t]+n}return t+n.replace(/(零.)*零元/,"元").replace(/(零.)+/g,"零").replace(/^整$/,"零元整")}static numToChinese(r){var o=["","一","二","三","四","五","六","七","八","九"],a=[["","万","亿"],["","十","百","千"]],t=Number(r);if(isNaN(t))return"";r=Math.abs(r);let n="";r=Math.floor(r);for(let t=0;t<a[0].length&&0<r;t++){let e="";for(let t=0;t<a[1].length&&0<r;t++){var i=o[r%10]+a[1][t];e=r%10?i+e:e,r=Math.floor(r/10)}e=e.replace("一十","十"),n=e+a[0][t]+n}return n}static urlQueryToObject(t){var e={},t=t.split("?")[1];if(!t)return{};var r=t.split("&");for(let t=0;t<r.length;t++){var o=r[t].split("=");e[o[0]]=o[1]}return e}static urlObjectToQuery(t){let e="";for(var r in t)e=(e+=e?"&":"?")+r+"="+t[r];return e}static snakeCaseToUpperCamelcase(t){return t.split("_").reduce((t,e)=>t+e[0].toLocaleUpperCase()+e.substring(1),"")}static snakeCaseToLowerCamelcase(t){return(t=this.snakeCaseToUpperCamelcase(t))[0].toLocaleLowerCase()+t.substring(1)}static camelcaseToSnakeCase(e){let r=e[0].toLocaleLowerCase();for(let t=1;t<e.length;t++){var o=e[t];o.match(/[A-Z]/)?r+="_"+o.toLocaleLowerCase():r+=o}return r}static getConstellationByDate(t){let e=0,r=0,o=(t instanceof Date&&(e=t.getMonth()+1,r=t.getDate()),"string"==typeof t&&(e=+t.substring(0,2),r=+t.substring(3,5)),100*e+r);"number"==typeof t&&(o=t);var a=["摩羯座","水瓶座","双鱼座","白羊座","金牛座","双子座","巨蟹座","狮子座","处女座","天秤座","天蝎座","射手座"],n=[119,218,320,419,520,621,722,822,922,1023,1122,1221];for(let t=0;t<n.length;t++){var i=n[t];if(o<=i)return a[t]}return a[0]}static citizenIdentificationNumberParse(t){var e=t.substring(0,6),r=new Date(+t.substring(6,10),+t.substring(10,12)-1,+t.substring(12,14)),o=+t[16]%2==0?"女":"男",t=n.getConstellationByDate(t.substring(10,14));return{reginCode:e,birthday:r.timeFormat("yyyy/MM/dd"),gender:o,constellation:t,age:(new Date).getFullYear()-r.getFullYear()}}static byteFormat(e,r=2,o=n.byteUnits){var a;"string"==typeof e&&(e=+e);for(let t=0;t<o.length-1;t++)if(e<n.byteDifferenceValue[t])return(e/(null!=(a=n.byteDifferenceValue[t-1])?a:1)).toFixed(t&&r)+o[t];return(e/n.byteDifferenceValue[n.byteDifferenceValue.length-1]).toFixed(r)+o[o.length-1]}static fourValueSplit(t){if(Array.isArray(t)){if(4===t.length)return t;if(3===t.length)return t.concat([t[1]]);if(2===t.length)return t.concat([t[0],t[1]]);if(1===t.length)return[t[0],t[0],t[0],t[0]];throw Error("array length error .")}return[t,t,t,t]}static sensitivePlusSymbol(t,e,r="*"){var o,a=t.split("");if((o=(e=Array.isArray(e)?e.toString():e).split(",").map(t=>+t)).length%2==1||0===o.length)return t;for(let e=0;e<t.length;e++)for(let t=0;t<o.length;t+=2){var n=o[t],i=o[t+1];n<=e&&e<i&&(a[e]=r)}return a.join("")}static toHalfWidthChar(t){var e=t.split("");for(let t=0;t<e.length;t++){var r=e[t],r=this.FULL_WIDTH_CHAR.indexOf(r);-1<r&&(e[t]=this.HALF_WIDTH_CHAR[r])}return e.join("")}static toFullWidthChar(t){var e=t.split("");for(let t=0;t<e.length;t++){var r=e[t],r=this.HALF_WIDTH_CHAR.indexOf(r);-1<r&&(e[t]=this.FULL_WIDTH_CHAR[r])}return e.join("")}}(n=r).byteUnits=["B","KB","MB","GB","TB","PB"],r.byteDifferenceValue=new Array(n.byteUnits.length-1).fill("").map((t,e)=>Math.pow(1024,e+1)),r.HALF_WIDTH_CHAR=",./<>\"\"''[]...!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",r.FULL_WIDTH_CHAR=",。、《》“”‘’【】…!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~",[{name:"usciToOibc",prototype:String.prototype,type:"property"},{name:"timeFormat",prototype:Date.prototype,type:"method"},{name:"byteFormat",prototype:Number.prototype,type:"method"},{name:"thousands",prototype:String.prototype,type:"property"},{name:"thousands",prototype:Number.prototype,type:"property"},{name:"hexToRgb",prototype:String.prototype,type:"property"},{name:"rgbToHex",prototype:String.prototype,type:"property"},{name:"xmlToText",prototype:String.prototype,type:"property"},{name:"numToAmountInWords",prototype:String.prototype,type:"property"},{name:"numToChinese",prototype:String.prototype,type:"property"},{name:"urlQueryToObject",prototype:String.prototype,type:"property"},{name:"snakeCaseToUpperCamelcase",prototype:String.prototype,type:"property"},{name:"snakeCaseToLowerCamelcase",prototype:String.prototype,type:"property"},{name:"camelcaseToSnakeCase",prototype:String.prototype,type:"property"},{name:"getConstellationByDate",prototype:String.prototype,type:"property"},{name:"getConstellationByDate",prototype:Date.prototype,type:"property"},{name:"citizenIdentificationNumberParse",prototype:String.prototype,type:"property"}].forEach(e=>{Object.defineProperty(e.prototype,e.name,{get:function(){let t=this;return"method"===e.type?function(){return r[e.name].apply(t,[t].concat(Object.values(arguments)))}:r[e.name](t)}})});export{r as Convertor};