self.addEventListener('activate', event => {
event.waitUntil(self.clients.claim())
})
/*
* Caching
*/
const CACHE_NAME = 'v_17_' + __BUILD_TIME__
const precacheResources = [__BASE_PATH__, __BASE_PATH__ + '/']
self.addEventListener('install', event => {
event.waitUntil((async () => {
const cache = await caches.open(CACHE_NAME)
await cache.addAll(precacheResources)
self.skipWaiting()
})())
})
self.addEventListener('activate', async event => {
event.waitUntil(self.clients.claim())
self.registration.unregister()
// remove old cache from old versions of service worker
const cacheNames = await caches.keys()
await Promise.all(cacheNames.map(async cacheName => {
if (cacheName !== CACHE_NAME) await caches.delete(cacheName)
}))
})
self.addEventListener('fetch', async event => {
event.respondWith(
caches.match(event.request).then(cachedResponse => {
if (cachedResponse) return cachedResponse
return fetch(event.request).then(async response => {
if (event.request.method === 'GET' && event.request.url.startsWith('http') &&
!event.request.url.includes('/api/')) {
const cache = await caches.open(CACHE_NAME)
await cache.put(event.request, response.clone())
}
return response
})
})
)
})
function objectKeys(o) {
return [...new Set([
Object.keys(o),
...Object.values(o).map(objectKeys)
].flat())]
}
const arr = [
{channelid: 190,
childrenHash: [{id: 0, hash: 'sdfsdfsfsdf'}]},
{channelid: 191,
childrenHash: [{id: 0, hash: 'ABC'}, {id: 1, hash: 'ABC'}, {id: 2, hash: 'LLL'}, {id: 3, hash: 'LLL'}, {id: 4, hash: 'SSS'}]},
]
const res = arr.map(item=>{
item.childrenHash = Object.values(
item.childrenHash.reduce((obj,h)=>{
obj[h.hash] = obj[h.hash] || []
obj[h.hash].push(h.id)
return obj
},{})
).filter(l=>l.length>1)
if (!item.childrenHash.length) item.childrenHash = null
return item
})
console.log(JSON.stringify(res, null, 2))