-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadvancedDataMapping.ts
37 lines (30 loc) · 1.15 KB
/
advancedDataMapping.ts
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
import type { Debugger, EvaluateCollectionType } from '../lib'
import { ClubRecord, DMap, FirestoreCollection } from '../lib'
export const advancedDataMappingSnippet = async (debug: Debugger) => {
/*
This example demonstrates some advance usages of DMap map() method.
By constructing each evaluation's state count for every club.
*/
// Initialise and read collection
const evalColl = new FirestoreCollection<EvaluateCollectionType>('evaluate')
const evalData = await evalColl.readFromCache(true)
if (!evalData) return
// Encapsulate evalData with ClubRecord
const evalMap = new ClubRecord(evalData.getRecord())
// Create each state count for every club
const allCountMap = evalMap.map((k, v) => {
// Group evaluation data by state value
const groupedByAction = new DMap(v.data()).groupBy((d) => d.action)
// Map grouped record to [state]: count form
const countMap = groupedByAction.map((gk, gv) => ({
[gk]: gv?.length
}))
// Return result object
return {
club: k,
...new DMap(countMap).getRecord()
}
})
debug.pauseForAnyKey('Press any key to get table result.')
debug.table(allCountMap)
}