| 
1 |  | -import { define } from "./Util";  | 
 | 1 | +import { define } from './Util'  | 
2 | 2 | 
 
  | 
3 | 3 | define(Array.prototype, {  | 
4 |  | -	remove: function <T>(this: T[], elem: T): T[] {  | 
5 |  | -		// do not use filter to modify current array  | 
6 |  | -		const index = this.indexOf(elem);  | 
7 |  | -		if (index === -1) return this;  | 
 | 4 | +  remove: function <T> (this: T[], elem: T): T[] {  | 
 | 5 | +    // do not use filter to modify current array  | 
 | 6 | +    const index = this.indexOf(elem)  | 
 | 7 | +    if (index === -1) return this  | 
8 | 8 | 
 
  | 
9 |  | -		this.splice(index, 1);  | 
10 |  | -		return this; //.filter((e) => e !== elem);  | 
11 |  | -	},  | 
12 |  | -	insert: function <T>(elem: T, index: number) {  | 
13 |  | -		if (!index) index = this.length;  | 
14 |  | -		this.splice(index, 0, elem);  | 
15 |  | -		return this;  | 
16 |  | -	},  | 
17 |  | -	flat: function (depth: number = 1) {  | 
18 |  | -		return this.reduce(  | 
19 |  | -			(acc: any, val: any) => ((Array.isArray(val) && depth >= 1) || depth === -1 ? acc.concat(val.flat(depth--)) : acc.concat(val)),  | 
20 |  | -			[]  | 
21 |  | -		);  | 
22 |  | -	},  | 
23 |  | -	last: function () {  | 
24 |  | -		return this[this.length - 1];  | 
25 |  | -	},  | 
26 |  | -	first: function () {  | 
27 |  | -		return this[0];  | 
28 |  | -	},  | 
29 |  | -	unique: function <T>(predicate?: (value: T, index: number, obj: T[]) => any) {  | 
30 |  | -		if (predicate) {  | 
31 |  | -			return [...new Map(this.map((item: T, index: number, obj: T[]) => [predicate(item, index, obj), item])).values()];  | 
32 |  | -		}  | 
33 |  | -		return [...new Set(this)];  | 
34 |  | -	},  | 
35 |  | -	random: function () {  | 
36 |  | -		return this[Math.floor(Math.random() * this.length)];  | 
37 |  | -	},  | 
38 |  | -	shuffle: function () {  | 
39 |  | -		for (let i = this.length - 1; i > 0; i--) {  | 
40 |  | -			const j = Math.floor(Math.random() * (i + 1));  | 
41 |  | -			[this[i], this[j]] = [this[j], this[i]];  | 
42 |  | -		}  | 
43 |  | -		return this;  | 
44 |  | -	},  | 
45 |  | -	findMap: function <T>(predicate: (value: T, index: number, obj: T[]) => any | undefined): number {  | 
46 |  | -		for (let i = 0; i < this.length; i++) {  | 
47 |  | -			var result = predicate(this[i], i, this);  | 
48 |  | -			if (result) {  | 
49 |  | -				return result;  | 
50 |  | -			}  | 
51 |  | -		}  | 
52 |  | -	},  | 
53 |  | -	findLast: function <T>(predicate: (value: T, index: number, obj: T[]) => any | undefined): T | undefined {  | 
54 |  | -		for (let i = this.length; i >= 0; i--) {  | 
55 |  | -			if (predicate(this[i], i, this)) return this[i];  | 
56 |  | -		}  | 
57 |  | -		return null;  | 
58 |  | -	},  | 
59 |  | -	findLastIndex: function <T>(predicate: (value: T, index: number, obj: T[]) => any | undefined): number {  | 
60 |  | -		for (let i = this.length - 1; i >= 0; i--) {  | 
61 |  | -			if (predicate(this[i], i, this)) return i;  | 
62 |  | -		}  | 
63 |  | -		return -1;  | 
64 |  | -	},  | 
65 |  | -	count: function (search: RegExp | any) {  | 
66 |  | -		const nativeTypes = ["string", "number", "object", "array"];  | 
67 |  | -		let count: number = 0;  | 
 | 9 | +    this.splice(index, 1)  | 
 | 10 | +    return this // .filter((e) => e !== elem);  | 
 | 11 | +  },  | 
 | 12 | +  insert: function <T> (elem: T, index: number) {  | 
 | 13 | +    if (!index) index = this.length  | 
 | 14 | +    this.splice(index, 0, elem)  | 
 | 15 | +    return this  | 
 | 16 | +  },  | 
 | 17 | +  flat: function (depth: number = 1) {  | 
 | 18 | +    return this.reduce(  | 
 | 19 | +      (acc: any, val: any) =>  | 
 | 20 | +        (Array.isArray(val) && depth >= 1) || depth === -1  | 
 | 21 | +          ? acc.concat(val.flat(depth--))  | 
 | 22 | +          : acc.concat(val),  | 
 | 23 | +      []  | 
 | 24 | +    )  | 
 | 25 | +  },  | 
 | 26 | +  last: function () {  | 
 | 27 | +    return this[this.length - 1]  | 
 | 28 | +  },  | 
 | 29 | +  first: function () {  | 
 | 30 | +    return this[0]  | 
 | 31 | +  },  | 
 | 32 | +  unique: function <T> (predicate?: (value: T, index: number, obj: T[]) => any) {  | 
 | 33 | +    if (predicate) {  | 
 | 34 | +      return [  | 
 | 35 | +        ...new Map(  | 
 | 36 | +          this.map((item: T, index: number, obj: T[]) => [  | 
 | 37 | +            predicate(item, index, obj),  | 
 | 38 | +            item  | 
 | 39 | +          ])  | 
 | 40 | +        ).values()  | 
 | 41 | +      ]  | 
 | 42 | +    }  | 
 | 43 | +    return [...new Set(this)]  | 
 | 44 | +  },  | 
 | 45 | +  random: function () {  | 
 | 46 | +    return this[Math.floor(Math.random() * this.length)]  | 
 | 47 | +  },  | 
 | 48 | +  shuffle: function () {  | 
 | 49 | +    for (let i = this.length - 1; i > 0; i--) {  | 
 | 50 | +      const j = Math.floor(Math.random() * (i + 1));  | 
 | 51 | +      [this[i], this[j]] = [this[j], this[i]]  | 
 | 52 | +    }  | 
 | 53 | +    return this  | 
 | 54 | +  },  | 
 | 55 | +  findMap: function <T> (  | 
 | 56 | +    predicate: (value: T, index: number, obj: T[]) => any | undefined  | 
 | 57 | +  ): number {  | 
 | 58 | +    for (let i = 0; i < this.length; i++) {  | 
 | 59 | +      const result = predicate(this[i], i, this)  | 
 | 60 | +      if (result) {  | 
 | 61 | +        return result  | 
 | 62 | +      }  | 
 | 63 | +    }  | 
 | 64 | +  },  | 
 | 65 | +  findLast: function <T> (  | 
 | 66 | +    predicate: (value: T, index: number, obj: T[]) => any | undefined  | 
 | 67 | +  ): T | undefined {  | 
 | 68 | +    for (let i = this.length; i >= 0; i--) {  | 
 | 69 | +      if (predicate(this[i], i, this)) return this[i]  | 
 | 70 | +    }  | 
 | 71 | +    return null  | 
 | 72 | +  },  | 
 | 73 | +  findLastIndex: function <T> (  | 
 | 74 | +    predicate: (value: T, index: number, obj: T[]) => any | undefined  | 
 | 75 | +  ): number {  | 
 | 76 | +    for (let i = this.length - 1; i >= 0; i--) {  | 
 | 77 | +      if (predicate(this[i], i, this)) return i  | 
 | 78 | +    }  | 
 | 79 | +    return -1  | 
 | 80 | +  },  | 
 | 81 | +  count: function (search: RegExp | any) {  | 
 | 82 | +    const nativeTypes = ['string', 'number', 'object', 'array']  | 
 | 83 | +    let count: number = 0  | 
68 | 84 | 
 
  | 
69 |  | -		this.forEach((element: any) => {  | 
70 |  | -			if (element === search) {  | 
71 |  | -				count++;  | 
72 |  | -				return;  | 
73 |  | -			}  | 
 | 85 | +    this.forEach((element: any) => {  | 
 | 86 | +      if (element === search) {  | 
 | 87 | +        count++  | 
 | 88 | +        return  | 
 | 89 | +      }  | 
74 | 90 | 
 
  | 
75 |  | -			// check if searchparam is a native class (l: 99)  | 
76 |  | -			if (typeof search == "function") {  | 
77 |  | -				const className = search.name.toLowerCase();  | 
78 |  | -				if (nativeTypes.includes(className) && typeof element === className) {  | 
79 |  | -					count++;  | 
80 |  | -					return;  | 
81 |  | -				} else if (element instanceof search) {  | 
82 |  | -					count++;  | 
83 |  | -					return;  | 
84 |  | -				}  | 
85 |  | -			}  | 
 | 91 | +      // check if searchparam is a native class (l: 99)  | 
 | 92 | +      if (typeof search === 'function') {  | 
 | 93 | +        const className = search.name.toLowerCase()  | 
 | 94 | +        // eslint-disable-next-line valid-typeof  | 
 | 95 | +        if (nativeTypes.includes(className) && typeof element === className) {  | 
 | 96 | +          count++  | 
 | 97 | +          return  | 
 | 98 | +        } else if (element instanceof search) {  | 
 | 99 | +          count++  | 
 | 100 | +          return  | 
 | 101 | +        }  | 
 | 102 | +      }  | 
86 | 103 | 
 
  | 
87 |  | -			// if element of array is a string: allow regex patterns  | 
88 |  | -			if (typeof element === "string") {  | 
89 |  | -				if (element.match(search)) {  | 
90 |  | -					count++;  | 
91 |  | -					return;  | 
92 |  | -				}  | 
93 |  | -			}  | 
94 |  | -		});  | 
95 |  | -		return count;  | 
96 |  | -	},  | 
97 |  | -	missing: function <T>(arr: T[]): T[] {  | 
98 |  | -		return this.filter((x: any) => !arr.includes(x));  | 
99 |  | -	},  | 
100 |  | -	similarities: function <T>(arr: T[]): T[] {  | 
101 |  | -		return this.filter((x: any) => arr.includes(x));  | 
102 |  | -	},  | 
103 |  | -});  | 
 | 104 | +      // if element of array is a string: allow regex patterns  | 
 | 105 | +      if (typeof element === 'string') {  | 
 | 106 | +        if (element.match(search)) {  | 
 | 107 | +          count++  | 
 | 108 | +        }  | 
 | 109 | +      }  | 
 | 110 | +    })  | 
 | 111 | +    return count  | 
 | 112 | +  },  | 
 | 113 | +  missing: function <T> (arr: T[]): T[] {  | 
 | 114 | +    return this.filter((x: any) => !arr.includes(x))  | 
 | 115 | +  },  | 
 | 116 | +  similarities: function <T> (arr: T[]): T[] {  | 
 | 117 | +    return this.filter((x: any) => arr.includes(x))  | 
 | 118 | +  }  | 
 | 119 | +})  | 
104 | 120 | 
 
  | 
105 | 121 | declare global {  | 
106 |  | -	interface Array<T> {  | 
107 |  | -		remove(o: T): this;  | 
108 |  | -		flat(depth?: number): T;  | 
109 |  | -		first(): T | undefined;  | 
110 |  | -		last(): T | undefined;  | 
111 |  | -		findLastIndex(predicate: (value: T, index: number, obj: T[]) => any | undefined): number;  | 
112 |  | -		findLast(predicate: (value: T, index: number, obj: T[]) => any | undefined): T | undefined;  | 
113 |  | -		findMap(predicate: (value: T, index: number, obj: T[]) => any | undefined): any | undefined;  | 
114 |  | -		random(): T | undefined;  | 
115 |  | -		unique(predicate?: (value: T, index: number, obj: T[]) => any | undefined): T[];  | 
116 |  | -		shuffle(): T[];  | 
117 |  | -		insert(elem: T, index: number): this;  | 
118 |  | -		count(search: RegExp | any): number;  | 
119 |  | -		similarities(arr: T[]): T[];  | 
120 |  | -		missing(arr: T[]): T[];  | 
121 |  | -	}  | 
 | 122 | +  interface Array<T> {  | 
 | 123 | +    remove(o: T): this;  | 
 | 124 | +    flat(depth?: number): T;  | 
 | 125 | +    first(): T | undefined;  | 
 | 126 | +    last(): T | undefined;  | 
 | 127 | +    findLastIndex(  | 
 | 128 | +      predicate: (value: T, index: number, obj: T[]) => any | undefined  | 
 | 129 | +    ): number;  | 
 | 130 | +    findLast(  | 
 | 131 | +      predicate: (value: T, index: number, obj: T[]) => any | undefined  | 
 | 132 | +    ): T | undefined;  | 
 | 133 | +    findMap(  | 
 | 134 | +      predicate: (value: T, index: number, obj: T[]) => any | undefined  | 
 | 135 | +    ): any | undefined;  | 
 | 136 | +    random(): T | undefined;  | 
 | 137 | +    unique(  | 
 | 138 | +      predicate?: (value: T, index: number, obj: T[]) => any | undefined  | 
 | 139 | +    ): T[];  | 
 | 140 | +    shuffle(): T[];  | 
 | 141 | +    insert(elem: T, index: number): this;  | 
 | 142 | +    count(search: RegExp | any): number;  | 
 | 143 | +    similarities(arr: T[]): T[];  | 
 | 144 | +    missing(arr: T[]): T[];  | 
 | 145 | +  }  | 
122 | 146 | }  | 
123 | 147 | 
 
  | 
124 |  | -export {};  | 
 | 148 | +export {}  | 
0 commit comments