-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
74 lines (64 loc) · 2.6 KB
/
utils.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
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
import { type ClassValue, clsx } from "clsx";
import { IFormsDataEntity } from "oneentry/dist/formsData/formsDataInterfaces";
import { twMerge } from "tailwind-merge";
import { AttributeCount, FormDataItem } from "./definitions";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function getResponses(formsData: IFormsDataEntity[], id: string) {
// get all responses for a form with a given id
return Object.values(formsData).filter(
(formData) => formData.formIdentifier === id
);
}
export function getNumberOfResponses(
responses: IFormsDataEntity[],
marker: string
) {
const attributeCounts = responses
// doing flatMap here because the formData is an array of objects, why not map? because we want to flatten the array of objects into a single array
// doing filter here because we only want to count the attributes that have a value
// doing map here because we want to return the marker of the attribute
// doing reduce here because we want to count the number of times the attribute appears in the array
.flatMap((response) =>
response.formData
// @ts-ignore
.filter((item: FormDataItem) => item.value !== "")
.map((item: FormDataItem) => item.marker)
) // array now looks like ["name", "name", "name", "email", "email", "phone", "phone", "phone", "phone"]
.reduce((counts, attribute) => {
// Below is a fancy way of saying: if the attribute exists in the counts object, then increment it by 1, otherwise set it to 1
counts[attribute] = (counts[attribute] || 0) + 1;
return counts;
}, {}) as AttributeCount;
return attributeCounts[marker] || 0;
}
export function getIndividualResponses(
responses: IFormsDataEntity[],
marker: string
) {
const items = responses.flatMap((response) => {
const formData = response.formData;
// @ts-ignore
const attribute = formData.find(
(item: FormDataItem) => item.marker === marker
);
if (!attribute?.value) return [];
return {
...attribute,
} as FormDataItem;
});
// group similar responses only once, and count the number of times they appear and return the item with the count
const groupedItems = items.reduce((counts, item) => {
if (!item.value) return counts;
// Below is a fancy way of saying: if the item exists in the counts object, then increment it by 1, otherwise set it to 1
// @ts-ignore
counts[item.value] = (counts[item.value] || 0) + 1;
return counts;
}, {}) as AttributeCount;
return Object.entries(groupedItems).map(([value, count]) => ({
value,
count,
marker,
}));
}