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(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/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);
}

 

posted @ 2017-09-06 15:52  N1900  阅读(264)  评论(0编辑  收藏  举报