-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathexercise6.js
More file actions
89 lines (71 loc) · 2.33 KB
/
Copy pathexercise6.js
File metadata and controls
89 lines (71 loc) · 2.33 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
// A utility module for data processing
function deduplicateBy(array, getKey) {
const uniqueItems = [];
const seenKeys = new Map();
for (const item of array) {
const key = getKey(item);
if (!seenKeys.has(key)) {
seenKeys.set(key, true);
uniqueItems.push(item);
}
}
return uniqueItems;
}
function groupAndAggregate(data, options) {
return data.reduce((groups, item) => {
const groupName = options.groupBy(item);
if (!groups[groupName]) {
groups[groupName] = { items: [], total: 0 };
}
groups[groupName].items.push(options.transform(item));
groups[groupName].total += options.getValue(item);
return groups;
}, {});
}
function validate(object, validationRules) {
const errors = [];
for (const [fieldName, rules] of Object.entries(validationRules)) {
const fieldValue = object[fieldName];
for (const rule of rules) {
if (!rule.test(fieldValue)) {
errors.push({ field: fieldName, message: rule.message });
}
}
}
return errors.length === 0 ? { valid: true } : { valid: false, errors };
}
function main() {
const orders = [
{ id: 1, customer: "Alice", product: "Book", amount: 25 },
{ id: 2, customer: "Bob", product: "Pen", amount: 5 },
{ id: 3, customer: "Alice", product: "Notebook", amount: 15 },
{ id: 4, customer: "Alice", product: "Book", amount: 25 },
{ id: 5, customer: "Bob", product: "Pencil", amount: 3 },
];
console.log("--- input ---");
console.log(orders);
console.log("\n--- deduplicateBy ---");
const uniqueOrders = deduplicateBy(
orders,
(order) => order.customer + order.product,
);
console.log(uniqueOrders);
console.log("\n--- groupAndAggregate ---");
const ordersByCustomer = groupAndAggregate(orders, {
groupBy: (order) => order.customer,
transform: (order) => order.product,
getValue: (order) => order.amount,
});
console.log(ordersByCustomer);
console.log("\n--- validate ---");
const newOrder = { id: 6, customer: "", product: "Eraser", amount: -2 };
const validationResult = validate(newOrder, {
customer: [
{ test: (value) => value && value.length > 0, message: "required" },
],
amount: [{ test: (value) => value > 0, message: "must be positive" }],
});
console.log(validationResult);
}
main();
export { deduplicateBy, groupAndAggregate, validate };