-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayUtils.js
More file actions
135 lines (123 loc) · 2.68 KB
/
Copy patharrayUtils.js
File metadata and controls
135 lines (123 loc) · 2.68 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
// arrayUtils.js - Array and collection utilities
/**
* Deduplicate array
* @param {Array} arr
* @returns {Array}
*/
export function deduplicate(arr = []) {
return [...new Set(arr)];
}
/**
* Find item in array by property
* @param {Array} arr
* @param {string} prop
* @param {*} value
* @returns {*|null}
*/
export function findByProp(arr = [], prop, value) {
return arr.find(item => item?.[prop] === value) ?? null;
}
/**
* Filter array by property value
* @param {Array} arr
* @param {string} prop
* @param {*} value
* @returns {Array}
*/
export function filterByProp(arr = [], prop, value) {
return arr.filter(item => item?.[prop] === value);
}
/**
* Map array to single property
* @param {Array} arr
* @param {string} prop
* @returns {Array}
*/
export function mapToProp(arr = [], prop) {
return arr.map(item => item?.[prop]).filter(item => item != null);
}
/**
* Remove duplicates from array of objects by property
* @param {Array} arr
* @param {string} prop
* @returns {Array}
*/
export function deduplicateByProp(arr = [], prop) {
const seen = new Set();
return arr.filter(item => {
const val = item?.[prop];
if (seen.has(val)) return false;
seen.add(val);
return true;
});
}
/**
* Remove item from array
* @param {Array} arr
* @param {*} item
* @returns {Array}
*/
export function remove(arr = [], item) {
const idx = arr.indexOf(item);
if (idx > -1) arr.splice(idx, 1);
return arr;
}
/**
* Remove item from array by property
* @param {Array} arr
* @param {string} prop
* @param {*} value
* @returns {Array}
*/
export function removeByProp(arr = [], prop, value) {
const idx = arr.findIndex(item => item?.[prop] === value);
if (idx > -1) arr.splice(idx, 1);
return arr;
}
/**
* Chunk array into smaller arrays
* @param {Array} arr
* @param {number} size
* @returns {Array[]}
*/
export function chunk(arr = [], size = 1) {
const chunks = [];
for (let i = 0; i < arr.length; i += size) {
chunks.push(arr.slice(i, i + size));
}
return chunks;
}
/**
* Get first item of array
* @param {Array} arr
* @returns {*|null}
*/
export function first(arr = []) {
return arr[0] ?? null;
}
/**
* Get last item of array
* @param {Array} arr
* @returns {*|null}
*/
export function last(arr = []) {
return arr[arr.length - 1] ?? null;
}
/**
* Check if any item matches condition
* @param {Array} arr
* @param {Function} predicate
* @returns {boolean}
*/
export function some(arr = [], predicate) {
return arr.some(predicate);
}
/**
* Check if all items match condition
* @param {Array} arr
* @param {Function} predicate
* @returns {boolean}
*/
export function every(arr = [], predicate) {
return arr.every(predicate);
}