aboutsummaryrefslogtreecommitdiff
path: root/src/extensions/Extensions_Array.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/extensions/Extensions_Array.ts')
-rw-r--r--src/extensions/Extensions_Array.ts37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/extensions/Extensions_Array.ts b/src/extensions/Extensions_Array.ts
new file mode 100644
index 000000000..a50fb330f
--- /dev/null
+++ b/src/extensions/Extensions_Array.ts
@@ -0,0 +1,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() {
+ // eslint-disable-next-line no-extend-native
+ 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 };