-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.js
36 lines (34 loc) · 1016 Bytes
/
utils.js
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
export function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[array[i], array[j]] = [array[j], array[i]]
}
return array
}
export const isTransportStop = (point) => point.tags.highway === 'bus_stop'
export const isTownhall = (point) => point.tags.amenity === 'townhall'
export function groupBy(list, keyGetter) {
const map = new Map()
list.forEach((item) => {
const key = keyGetter(item)
const collection = map.get(key)
if (!collection) {
map.set(key, [item])
} else {
collection.push(item)
}
})
return Object.fromEntries(map)
}
export const fetchRetry = async (url, options, n) => {
try {
return await fetch(url, options)
} catch (err) {
console.log('Erreur', err)
if (n === 1) throw err
console.log('retry fetch points, ', n, ' try left')
return await fetchRetry(url, options, n - 1)
}
}
export const objectMap = (obj, fn) =>
Object.fromEntries(Object.entries(obj).map(([k, v], i) => [k, fn(v, k, i)]))