webstudio/ui/src/core/extensions/array.ts

49 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-11-20 00:48:40 +03:00
/* eslint-disable @typescript-eslint/no-this-alias */
2023-11-10 12:06:40 +03:00
export const ArrayExtensions = () => {
if ([].equals === undefined) {
// eslint-disable-next-line no-extend-native
Array.prototype.equals = function (array, strict = true) {
if (!array) return false;
if (arguments.length === 1) strict = true;
if (this.length !== array.length) return false;
for (let i = 0; i < this.length; i++) {
if (this[i] instanceof Array && array[i] instanceof Array) {
if (!this[i].equals(array[i], strict)) return false;
} else if (strict && this[i] !== array[i]) {
return false;
} else if (!strict) {
return this.sort().equals(array.sort(), true);
}
}
return true;
};
}
if ([].lastElement === undefined) {
// eslint-disable-next-line no-extend-native
Array.prototype.lastElement = function () {
2023-11-20 00:48:40 +03:00
const instanceCheck = this;
2023-11-10 12:06:40 +03:00
if (instanceCheck === undefined) {
return undefined;
} else {
2023-11-20 00:48:40 +03:00
const instance = instanceCheck as [];
2023-11-10 12:06:40 +03:00
return instance[instance.length - 1];
}
};
}
2023-11-10 21:43:57 +03:00
if ([].isEmpty === undefined) {
// eslint-disable-next-line no-extend-native
Array.prototype.isEmpty = function () {
return this.length === 0;
};
}
2023-11-20 00:48:40 +03:00
if ([].isNotEmpty === undefined) {
// eslint-disable-next-line no-extend-native
Array.prototype.isNotEmpty = function () {
return this.length !== 0;
};
}
2023-11-10 12:06:40 +03:00
};