UNPKG

fordata

Version:

Easy handle data by prototype for TreeData、Array、Date、Encode、String etc.

1 lines 9.06 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ForData=e():t.ForData=e()}(self,(()=>{return t={10:(t,e,r)=>{const n=r(706),o=r(582),s=r(961),a=r(639),h=r(30);t.exports={Tree:n,DateTime:o,Encode:s,EasyArray:a,EasyString:h}},582:t=>{t.exports=class{constructor(){return this.format(),this.getDays(),this.prev(),this.prevDay(),this.prevMonth(),this.prevYear(),this.next(),this.nextDay(),this.nextMonth(),this.nextYear(),this.getWeek(),this.getRangeYear(),this.getRangeMonth(),this.getRangeWeek(),this}prev(){Date.prototype.prev=function(t){return new Date(+this-t).format("yyyy-MM-dd hh:mm:ss")}}prevDay(){Date.prototype.prevDay=function(t={day:1,isShowAll:!1,includeCurrent:!1}){let{day:e=1,isShowAll:r=!1,includeCurrent:n=!1}=t;if(!r&&!n)return new Date(+this-1e3*e*60*60*24).format("yyyy-MM-dd hh:mm:ss");let o=[];for(let t=+!n;t<=e-+n;t++)o.unshift(new Date(+this-1e3*t*60*60*24)),1===e&&o.unshift(new Date(+this-864e5));return o.map((t=>t.format("yyyy-MM-dd hh:mm:ss")))}}prevMonth(){Date.prototype.prevMonth=function(t={month:1,isShowAll:!1,includeCurrent:!1}){let{month:e=1,isShowAll:r=!1,includeCurrent:n=!1}=t,o=this.getMonth()+1,s=this.getDays(),a=new Date(this.format("yyyy-MM-01 hh:mm:ss"));a.setMonth(a.getMonth()-e);let h=a.getDays();if(!r&&!n)return new Date(a.format(`yyyy-MM-${s>h?h:s} ${this.format(" hh:mm:ss")}`)).format("yyyy-MM-dd hh:mm:ss");let i=[];for(let t=1;t<=e;t++){a.setMonth(o-t-+!n),h=a.getDays();let e=new Date(a.format(`yyyy-MM-${s>h?h:s} ${this.format(" hh:mm:ss")}`));i.unshift(e)}return i.map((t=>t.format("yyyy-MM-dd hh:mm:ss")))}}prevYear(){Date.prototype.prevYear=function(t={year:1,isShowAll:!1,includeCurrent:!1}){let{year:e=1,isShowAll:r=!1,includeCurrent:n=!1}=t,o=this.getFullYear();if(!r&&!n)return new Date(this.setFullYear(o-e)).format("yyyy-MM-dd hh:mm:ss");let s=[];for(let t=0;t<e;t++)s.unshift(new Date(this.setFullYear(o-t-+!n)));return s.map((t=>t.format("yyyy-MM-dd hh:mm:ss")))}}next(){Date.prototype.next=function(t){return new Date(+this+t).format("yyyy-MM-dd hh:mm:ss")}}nextDay(){Date.prototype.nextDay=function(t={day:1,isShowAll:!1,includeCurrent:!1}){let{day:e=1,isShowAll:r=!1,includeCurrent:n=!1}=t;if(!r&&!n)return new Date(+this+1e3*e*60*60*24).format("yyyy-MM-dd hh:mm:ss");let o=[];for(let t=+!n;t<=e-+n;t++)o.push(new Date(+this+1e3*t*60*60*24)),1===e&&o.push(new Date(+this+864e5));return o.map((t=>t.format("yyyy-MM-dd hh:mm:ss")))}}nextMonth(){Date.prototype.nextMonth=function(t={month:1,isShowAll:!1,includeCurrent:!1}){let{month:e=1,isShowAll:r=!1,includeCurrent:n=!1}=t,o=this.getDays();if(!r&&!n){let t=new Date(this.format("yyyy-MM-01 hh:mm:ss"));t.setMonth(t.getMonth()+e);let r=t.getDays();return new Date(t.format(`yyyy-MM-${o>r?r:o} ${this.format(" hh:mm:ss")}`)).format("yyyy-MM-dd hh:mm:ss")}let s=[];for(let t=1;t<=e;t++){let e=new Date(this.format("yyyy-MM-01 hh:mm:ss"));e.setMonth(e.getMonth()+t-+n);let r=e.getDays(),a=new Date(e.format(`yyyy-MM-${o>r?r:o} ${this.format(" hh:mm:ss")}`));s.push(a)}return s.map((t=>t.format("yyyy-MM-dd hh:mm:ss")))}}nextYear(){Date.prototype.nextYear=function(t={year:1,isShowAll:!1,includeCurrent:!1}){let{year:e=1,isShowAll:r=!1,includeCurrent:n=!1}=t,o=this.getFullYear();if(!r&&!n)return new Date(this.setFullYear(o+e)).format("yyyy-MM-dd hh:mm:ss");let s=[];for(let t=+!n+0;t<e+ +!n;t++)s.push(new Date(this.setFullYear(o+t)));return s.map((t=>t.format("yyyy-MM-dd hh:mm:ss")))}}split(t={}){let{startTime:e,endTime:r,gap:n}=t,o=[],s=Math.floor(+r-+e)/n;for(let t=0;t<=s;t++)o.push(new Date(+e+n*t));return o.map((t=>t.format("yyyy-MM-dd hh:mm:ss")))}format(){Date.prototype.format=function(t){let e={"M+":this.getMonth()+1,"d+":this.getDate(),"h+":this.getHours(),"m+":this.getMinutes(),"s+":this.getSeconds(),"q+":Math.floor((this.getMonth()+3)/3),S:this.getMilliseconds()};/(y+)/.test(t)&&(t=t.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length)));for(let r in e)new RegExp("("+r+")").test(t)&&(t=t.replace(RegExp.$1,1==RegExp.$1.length?e[r]:("00"+e[r]).substr((""+e[r]).length)));return t}}calc(t={}){let{startTime:e,endTime:r,offset:n=8,type:o="year"}=t;if("year"===o){let t=new Date(Math.abs(+r-+e)-1e3*n*3600);return{year:t.getFullYear()-1970,month:t.getMonth(),day:t.getDate()-1,hour:t.getHours(),minutes:t.getMinutes(),seconds:t.getSeconds()}}if("day"===o){let t=Math.abs(+r-+e),o=Math.floor(t/864e5),s=new Date(t-24*o*36e5-1e3*n*3600);return{day:o,hour:s.getHours(),minutes:s.getMinutes(),seconds:s.getSeconds()}}}getWeek(){Date.prototype.getWeek=function(t=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]){return t[this.getDay()]}}getRangeYear(){Date.prototype.getRangeYear=function(){let t=this.getFullYear();return{start:`${t}-01-01 00:00:00`,end:`${t}-12-31 23:59:59`}}}getRangeMonth(){Date.prototype.getRangeMonth=function(){let t=this.format("yyyy-MM-01 00:00:00"),e=new Date(+new Date(this.format(`yyyy-${(this.getMonth()+2)%12}-01 00:00:00`))-1).getDate();return{start:t,end:new Date(t).format(`yyyy-MM-${e} 23:59:59`)}}}getRangeWeek(t=!0){Date.prototype.getRangeWeek=function(){let e=this.getDay();return t&&(e=e||7),{start:new Date(+this-3600*(e-1)*1e3*24).format("yyyy-MM-dd 00:00:00"),end:new Date(+this+3600*(7-e)*1e3*24).format("yyyy-MM-dd 23:59:59")}}}getDays(){Date.prototype.getDays=function(){return new Date(+new Date(this.format(`yyyy-${this.getMonth()+2}-01 00:00:00`))-1).getDate()}}}},639:t=>{t.exports=class{constructor(){Array.prototype.create=this.create,Array.prototype.asc=this.asc,Array.prototype.desc=this.desc,Array.prototype.sum=this.sum,Array.prototype.unique=this.unique,Array.prototype.groupByLength=this.groupByLength,Array.prototype.groupByName=this.groupByName,Array.prototype.groupByValue=this.groupByValue}create(t=1,e=(()=>null)){return Array.from({length:t},((t,r)=>e(r)))}asc(t){return t?this.sort(((e,r)=>e[t]-r[t])):this.sort(((t,e)=>t-e))}desc(t){return t?this.sort(((e,r)=>r[t]-e[t])):this.sort(((t,e)=>e-t))}sum(t){if(t){let e=0;return this.forEach((r=>e+=r[t]||0)),e}return this.reduce(((t,e)=>+t+ +e))}unique(t){if(t){let e=[];return this.forEach(((r,n)=>{e.includes(r[t])?this.splice(n,1):e.push(r[t])})),this}return[...new Set(this)]}groupByLength(t=1){let e=[];for(let r=0;r<Math.ceil(this.length/t);r++){let n=JSON.parse(JSON.stringify(this));e.push(n.splice(t*r,t))}return e}groupByName(t){let e={others:[]};return this.forEach((r=>{void 0!==r[t]?e[t]?e[t].push(r):e[t]=[r]:e.others.push(r)})),Object.values(e).reverse()}groupByValue(t){let e={};return this.forEach((r=>{void 0!==r[t]&&(e[r[t]]?e[r[t]].push(r):e[r[t]]=[r])})),e}}},30:t=>{t.exports=class{constructor(){String.prototype.getInfoByIdCard=this.getInfoByIdCard}getInfoByIdCard(){let t=this.substring(0,6),e=this.substring(6,10),r=this.substring(10,12),n=this.substring(12,14);return{areaCode:t,year:e,month:r,day:n,gender:this.substring(16,17)%2?"male":"female",age:new Date(new Date-new Date(`${e}-${r}-${n}`)).getFullYear()-1970}}}},961:t=>{t.exports=class{constructor(){String.prototype.toBase64=this.toBase64,String.prototype.toStr=this.toStr,String.prototype.toEntity=this.toEntity,String.prototype.entityToString=this.entityToString,String.prototype.encodeURL=this.encodeURL,String.prototype.decodeURL=this.decodeURL}toBase64(){return btoa(String.fromCharCode(...(new TextEncoder).encode(this)))}base64ToString(){return(new TextDecoder).decode(Uint8Array.from(atob(this),(t=>t.charCodeAt(0))))}toEntity(){let t=[];for(let e=this.length-1;e>=0;e--)t.unshift(["&#",this[e].charCodeAt(),";"].join(""));return t.join("")}entityToString(){return this.replace(/&#(\d+);/g,(function(t,e){return String.fromCharCode(e)}))}encodeURL(){return encodeURIComponent(this)}decodeURL(){return decodeURIComponent(this)}}},706:t=>{t.exports=class{constructor(t={}){let e=this,{label:r="label",id:n="id",parentId:o="parentId",children:s="children"}=t;this.props={label:r,id:n,parentId:o,children:s},Array.prototype.toTree=function(t){return e.toTree(this,t)},Array.prototype.toArray=function(t){return e.toArray(this,t)},Array.prototype.findPath=function(t){return e.findPath(this.toArray(),t)},Array.prototype.findChildren=function(t){return e.findChildren(this.toArray(),t)}}toTree(t,e){let{id:r,parentId:n,children:o}=this.props;const s=t.filter((t=>t[n]===e));if(s.length)return s.map((e=>({...e,[o]:this.toTree(t,e[r])})))}toArray(t,e){let r=[],{label:n,id:o,parentId:s,children:a}=this.props;for(let h of t){let t={...h,[n]:h[n],[s]:e,[o]:h[o]};delete t[a],r.push(t),h[a]&&(r=[...r,...this.toArray(h[a],h[o])])}return r}findPath(t,e){let r=[],{id:n,parentId:o}=this.props;return t.forEach((s=>{s[n]===e&&(r.unshift(s),r=[...this.findPath(t,s[o]),...r])})),r}findChildren(t,e){let r=[],{id:n,parentId:o}=this.props;return t.forEach((s=>{s[o]===e&&(r.push(s),r=[...r,...this.findChildren(t,s[n])])})),r}}}},e={},function r(n){var o=e[n];if(void 0!==o)return o.exports;var s=e[n]={exports:{}};return t[n](s,s.exports,r),s.exports}(10);var t,e}));