TypeScript扩展类方法

以数组删除元素为例

javascript数组删除一般是这样

1 const idx = selectedIDs.findIndex(x => x === deSelected);
2 selectedIDs.splice(idx, 1);    

或者 

const deleteId='xxxx';
const selectedIDs
= selectedIDs.filter(x => x!==deleteId)

不方便

在tyscript中扩展数组增加常用方法

1 建立接口声明文件

extension.d.ts

declare global {  
    interface Number {  
        thousandsSeperator(): String;  
    }  
}  
export {}; 

2 建立实现文件 number-extensions.ts

Number.prototype.thousandsSeperator = function(): string {  
    return Number(this).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');  
}  
export {}; 

3 使用

import "../utils/number-extensions";
const num=111111;
console.log(num.thousandsSeperator() );

4 数组常用方法

接口声明

export { }; // this file needs to be a module
declare global {
    interface Array<T> {
        firstOrDefault(predicate: (item: T) => boolean): T;
        where(predicate: (item: T) => boolean): T[];
        orderBy(propertyExpression: (item: T) => any): T[];
        orderByDescending(propertyExpression: (item: T) => any): T[];
        orderByMany(propertyExpressions: [(item: T) => any]): T[];
        orderByManyDescending(propertyExpressions: [(item: T) => any]): T[];
        remove(item: T): boolean;
        add(item: T): void;
        addRange(items: T[]): void;
        removeRange(items: T[]): void;
    }
    interface String {
        isNullOrEmpty(this: string): boolean;
     format(...replacements: string[]): string;
} }

实现

export { }; // this file needs to be a module


(function () {
    if (!String.prototype.isNullOrEmpty) {
        String.prototype.isNullOrEmpty = function (this: string): boolean {
            return !this;
        };
    }
    if (!String.prototype.format) { 
    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function(match, number) { 
          return typeof args[number] != 'undefined' ? args[number]: match ;
        });
    };}
if (!Array.prototype.firstOrDefault) { Array.prototype.firstOrDefault = function (predicate: (item: any) => boolean) { for (var i = 0; i < (<Array<any>>this).length; i++) { let item = (<Array<any>>this)[i]; if (predicate(item)) { return item; } } return null; } } if (!Array.prototype.where) { Array.prototype.where = function (predicate: (item: any) => boolean) { let result = []; for (var i = 0; i < (<Array<any>>this).length; i++) { let item = (<Array<any>>this)[i]; if (predicate(item)) { result.push(item); } } return result; } } if (!Array.prototype.remove) { Array.prototype.remove = function (item: any): boolean { let index = (<Array<any>>this).indexOf(item); if (index >= 0) { (<Array<any>>this).splice(index, 1); return true; } return false; } } if (!Array.prototype.removeRange) { Array.prototype.removeRange = function (items: any[]): void { for (var i = 0; i < items.length; i++) { (<Array<any>>this).remove(items[i]); } } } if (!Array.prototype.add) { Array.prototype.add = function (item: any): void { (<Array<any>>this).push(item); } } if (!Array.prototype.addRange) { Array.prototype.addRange = function (items: any[]): void { for (var i = 0; i < items.length; i++) { (<Array<any>>this).push(items[i]); } } } if (!Array.prototype.orderBy) { Array.prototype.orderBy = function (propertyExpression: (item: any) => any) { let result = []; var compareFunction = (item1: any, item2: any): number => { if (propertyExpression(item1) > propertyExpression(item2)) return 1; if (propertyExpression(item2) > propertyExpression(item1)) return -1; return 0; } for (var i = 0; i < (<Array<any>>this).length; i++) { return (<Array<any>>this).sort(compareFunction); } return result; } } if (!Array.prototype.orderByDescending) { Array.prototype.orderByDescending = function (propertyExpression: (item: any) => any) { let result = []; var compareFunction = (item1: any, item2: any): number => { if (propertyExpression(item1) > propertyExpression(item2)) return -1; if (propertyExpression(item2) > propertyExpression(item1)) return 1; return 0; } for (var i = 0; i < (<Array<any>>this).length; i++) { return (<Array<any>>this).sort(compareFunction); } return result; } } if (!Array.prototype.orderByMany) { Array.prototype.orderByMany = function (propertyExpressions: [(item: any) => any]) { let result = []; var compareFunction = (item1: any, item2: any): number => { for (var i = 0; i < propertyExpressions.length; i++) { let propertyExpression = propertyExpressions[i]; if (propertyExpression(item1) > propertyExpression(item2)) return 1; if (propertyExpression(item2) > propertyExpression(item1)) return -1; } return 0; } for (var i = 0; i < (<Array<any>>this).length; i++) { return (<Array<any>>this).sort(compareFunction); } return result; } } if (!Array.prototype.orderByManyDescending) { Array.prototype.orderByManyDescending = function (propertyExpressions: [(item: any) => any]) { let result = []; var compareFunction = (item1: any, item2: any): number => { for (var i = 0; i < propertyExpressions.length; i++) { let propertyExpression = propertyExpressions[i]; if (propertyExpression(item1) > propertyExpression(item2)) return -1; if (propertyExpression(item2) > propertyExpression(item1)) return 1; } return 0; } for (var i = 0; i < (<Array<any>>this).length; i++) { return (<Array<any>>this).sort(compareFunction); } return result; } } })();

 

参考:

https://www.c-sharpcorner.com/article/learn-about-extension-methods-in-typescript/

 

posted @ 2019-07-30 18:03  wolbo  阅读(7266)  评论(0编辑  收藏  举报