• javascript extend


    interface  Date{
        addHours(h:number);
        addMinutes(m:number);
        format(str):string
    }
    
    interface String{
        trim():string;
        solrFormat():string;
    }
    
    interface Array<T> {
        clear();
        removeByIndex(index:number);
        remove(fn:(t:T)=>boolean);
    }
    
    (function(){
    
        Date.prototype.addHours= function(h:number) {
            this.setTime(this.getTime() + (h*60*60*1000));
            return this;
        } ;
        Date.prototype.addMinutes= function(m:number) {
            this.setTime(this.getTime() + (m*60*1000));
            return this;
        } ;
        Date.prototype.format=function(fmt:string="yyyy-MM-dd hh:mm:ss"):string {
            let date=this;
            if(!date){
                return date;
            }
            if (fmt === undefined || fmt === "" || fmt === null) {
                fmt = "yyyy-MM-dd hh:mm:ss";
            }
    
            let o = {
                "M+": date.getMonth() + 1,                 //月份
                "d+": date.getDate(),                    //
                "h+": date.getHours(),                   //小时
                "m+": date.getMinutes(),                 //
                "s+": date.getSeconds(),                 //
                "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds()             //毫秒
            };
            if (/(y+)/.test(fmt))
                fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (let k in o)
                if (new RegExp("(" + k + ")").test(fmt))
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        };
        if(!String.prototype.trim){
            String.prototype.trim=function () {
                return this.replace(/^[suFEFFxA0]+|[suFEFFxA0]+$/g, '')||"";
            }
        }
        String.prototype.solrFormat=function () {
           if(!this){
             return this;
           }
           let str= this.replace(/[(){}[]^*&%#$@!<>?=:'"]/g,"");
           str=this.replace(/-/,"\-");
           return str.replace(/[ +]/g,"\ ")
        };
        Array.prototype.clear=function () {
            this.splice(0,this.length);
        }
        Array.prototype.removeByIndex= function(index:number) {
            this.splice(index,1);
        };
        Array.prototype.remove= function(fn:(t:any)=>boolean) {
            let items = this.filter(fn);
            let that=this;
            items.forEach(function (item) {
                let index = that.indexOf(item);
                this.removeByIndex(index);
            })
        };
    })();
    d.ts  declare file
    declare  interface  Date{
        addHours(h:number);
        addMinutes(m:number);
        format(str):string
    }
    
    declare interface String{
        trim();
        solrFormat():string;
    }
    
    declare interface   Array<T> {
        clear();
        removeByIndex(index:number);
        remove(fn:(t:T)=>boolean);
    }
  • 相关阅读:
    Java集合类: Set、List、Map、Queue使用场景梳理
    Java通过JDBC进行简单的增删改查(以MySQL为例)
    Servlet学习笔记
    Tomcat原理详解
    Tomcat的JVM和连接数设置
    Google Chrome 未响应。是否立即重新启动?---解决方法(秒速解决)
    LDAP服务器的概念和原理简单介绍
    Java IO流学习
    JavaScript_js模拟键盘输入
    Java_类似java.lang.VerifyError: Expecting a stackmap frame at branch target 22 in method的解决方法
  • 原文地址:https://www.cnblogs.com/Zoes/p/7485194.html
Copyright © 2020-2023  润新知