aboutsummaryrefslogtreecommitdiff
path: root/src/extensions/ArrayExtensions.ts
blob: 8e125766df682d5803a6ea74d15768b0c6bf07dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
export default class ArrayExtension {
    private readonly property: string;
    private readonly body: <T>(this: Array<T>) => any;

    constructor(property: string, body: <T>(this: Array<T>) => any) {
        this.property = property;
        this.body = body;
    }

    assign() {
        Object.defineProperty(Array.prototype, this.property, {
            value: this.body,
            enumerable: false
        });
    }

}

/**
 * IMPORTANT: Any extension you add here *must* have a corresponding type definition
 * in the Array<T> interface in ./General/ExtensionsTypings.ts. Otherwise,
 * Typescript will not recognize your new function.
 */
const extensions = [
    new ArrayExtension("lastElement", function () {
        if (!this.length) {
            return undefined;
        }
        return this[this.length - 1];
    })
];

function Assign() {
    extensions.forEach(extension => extension.assign());
}

export { Assign };