blob: d61585e2849f81c035046a5007fa0ae3470a57fd (
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
38
39
40
41
|
export default class ArrayExtension {
private readonly property: string;
private readonly body: <T>(this: Array<T>, args: unknown) => unknown;
constructor(property: string, body: <T>(this: Array<T>, args: unknown) => unknown) {
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];
}),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
new ArrayExtension('getIndex', function (val: any) {
const index = this.indexOf(val);
return index === -1 ? undefined : index;
}),
];
function Assign() {
extensions.forEach(extension => extension.assign());
}
export { Assign };
|