aboutsummaryrefslogtreecommitdiff
path: root/util/array.ts
blob: c5a26c6817610f324e207e8dfba7469f3288d70d (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
42
43
import "./set.ts";

declare global {
	interface Array<T> {
		/** @summary check if any of the elements of `arr2` are included in `this` */
		anyOf(arr2: Array<T>): boolean;
		/** @summary return last element of array without removing it */
		peek(): T;
		/** @summary create Set from this array */
		set(): Set<T>;
		/** @summary clear array */
		clear(): void;
		/** @summary filter duplicates from array */
		filterDuplicates(): Array<T>;
		/** @summary `true` if the array doesn't contain duplicate items */
		isUniq(): boolean;
	}
}

Array.prototype.anyOf = function(arr2) {
	return !!this.filter(e => arr2.includes(e)).length;
};

Array.prototype.peek = function() {
	return this[this.length - 1];
};

Array.prototype.set = function() {
	return new Set(this);
}

Array.prototype.clear = function() {
	while (this.length > 0) this.pop();
}

Array.prototype.filterDuplicates = function() {
	return this.set().arr(); // TODO: optimize this
}

Array.prototype.isUniq = function() {
	return this.length == this.filterDuplicates().length;
}