-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy patharray.ts
More file actions
190 lines (154 loc) · 4.52 KB
/
array.ts
File metadata and controls
190 lines (154 loc) · 4.52 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { InvalidValue } from '../exceptions/invalid-value';
import { Obj } from './object';
export class Arr {
static toObj(
nestedArray: Array<any>,
keyIndexMap: string[],
): Record<string, any>[] {
Arr.isArray(nestedArray, true);
const objArr = [];
for (const arr of nestedArray) {
if (!Arr.isArray(arr)) {
continue;
}
const obj = {} as Record<string, any>;
for (const [index, value] of arr.entries()) {
obj[keyIndexMap[index]] = value;
}
objArr.push(obj);
}
return objArr;
}
static isArray(value: any, throwError = false): boolean {
if (Array.isArray(value)) return true;
if (throwError) {
throw new InvalidValue('Passed value is not an object');
}
return false;
}
static collapse(arr: any[]): any[] {
const newArr = [];
for (const el of arr) {
if (Arr.isArray(el)) {
newArr.push(...Arr.collapse(el));
} else {
newArr.push(el);
}
}
return newArr;
}
static random(arr: any[]): any[] {
let currentIndex = arr.length,
randomIndex;
while (currentIndex > 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[arr[currentIndex], arr[randomIndex]] = [
arr[randomIndex],
arr[currentIndex],
];
}
return arr;
}
static sort(arr: any[]): any[] {
return arr.sort();
}
static sortDesc(arr: any[]): any[] {
return arr.sort((a, b) => b - a);
}
static pick<T = any>(arr: T[], props: Array<string>): T[] {
const newArr = [];
for (const prop of props) {
const propArr = prop.split('.');
let startIndex = propArr[0] === '*' ? 0 : +propArr[0];
const endIndex = propArr[0] === '*' ? arr.length - 1 : +propArr[0];
while (startIndex <= endIndex) {
const newPropsArr = [propArr.slice(1).join('.')];
if (Obj.isObj(arr[startIndex])) {
newArr[startIndex] = Obj.pick(
arr[startIndex],
newPropsArr[0] === '' ? [] : newPropsArr,
);
}
if (Array.isArray(arr[startIndex])) {
newArr[startIndex] = Arr.pick(arr[startIndex] as T[], newPropsArr);
}
startIndex++;
}
}
return newArr;
}
static except<T = any>(arr: T[], props: Array<string>): T[] {
let newArr = [...arr];
for (const prop of props) {
const propArr = prop.split('.');
let startIndex = propArr[0] === '*' ? 0 : +propArr[0];
const endIndex = propArr[0] === '*' ? arr.length - 1 : +propArr[0];
while (startIndex <= endIndex) {
if (!propArr[1]) {
newArr = arr.splice(startIndex, 1);
startIndex++;
continue;
}
const newPropsArr = [propArr.slice(1).join('.')];
if (Obj.isObj(arr[startIndex])) {
newArr[startIndex] = Obj.except(arr[startIndex], newPropsArr);
}
if (Array.isArray(arr[startIndex])) {
newArr[startIndex] = Arr.except(
arr[startIndex] as T[],
newPropsArr,
) as T;
}
startIndex++;
}
}
return newArr;
}
static get<T = any>(arr: T[], key: string | number): T {
if (key in arr) return arr[key];
key = key as string;
const splitKeys = key.split('.');
if (!splitKeys.length) return;
if (Arr.isArray(arr[splitKeys[0]])) {
return Arr.get(arr[splitKeys[0]], splitKeys.slice(1).join('.'));
}
if (Obj.isObj(arr[splitKeys[0]])) {
return Obj.get(arr[splitKeys[0]], splitKeys.slice(1).join('.'));
}
return undefined;
}
static exists<T = any>(arr: T[], key: string | number): boolean {
if (typeof key === 'number' && key >= 0 && key < arr.length) {
return true;
}
if (typeof key === 'string') {
const splitKeys = key.split('.');
if (!splitKeys.length) return false;
if (Arr.isArray(arr[splitKeys[0]])) {
return Arr.exists(arr[splitKeys[0]], splitKeys.slice(1).join('.'));
}
if (Obj.isObj(arr[splitKeys[0]])) {
return Obj.get(arr[splitKeys[0]], splitKeys.slice(1).join('.')) !== undefined;
}
}
return false;
}
static last<T = any>(
arr: T[],
predicate?: ((item: T, index: number, array: T[]) => boolean) | null
): T | undefined {
if (!arr || arr.length === 0) {
return undefined;
}
if (!predicate) {
return arr[arr.length - 1];
}
for (let i = arr.length - 1; i >= 0; i--) {
if (predicate(arr[i], i, arr)) {
return arr[i];
}
}
return undefined;
}
}