-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin-max-sum.ts
More file actions
82 lines (70 loc) · 2.48 KB
/
min-max-sum.ts
File metadata and controls
82 lines (70 loc) · 2.48 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
/**
* Demonstrates how to use the `min`, `max` and `sum` methods chained to `atomic()`.
*/
import { assert } from "https://deno.land/std@0.153.0/testing/asserts.ts";
const kv = await Deno.openKv();
const deleteOldData = async () => {
await kv.delete(["sum"]);
await kv.delete(["min"]);
await kv.delete(["max"]);
};
// Get rid of old indexes
await deleteOldData();
// sum of integers between 1 to 10
for (let i = 1; i <= 10; i++) {
const u64 = new Deno.KvU64(BigInt(i));
await kv.atomic().sum(["sum"], u64.value).commit();
}
const sum = await kv.get(["sum"]);
const sumU64 = sum.value as Deno.KvU64;
const valSumU64 = sumU64.value;
console.log(`Sum of 1 to 10: ${valSumU64}`);
assert(valSumU64 === 55n, `Sum '${valSumU64}' not correct`);
// min and max of 10 random numbers
console.log("Ten random numbers between 1 and 100");
for (let i = 1; i <= 10; i++) {
// generate random number
const val = BigInt(Math.floor(Math.random() * 100));
console.log(Number(val)); // convert bigint to number
await kv.atomic().max(["max"], val).commit();
await kv.atomic().min(["min"], val).commit();
}
console.log(
"Smallest of 10 random numbers: ",
Number((await kv.get(["min"]) as Deno.KvEntry<Deno.KvU64>).value.value),
);
console.log(
"Largest of 10 random numbers: ",
Number((await kv.get(["max"]) as Deno.KvEntry<bigint>).value),
);
// Get rid of old indexes
await deleteOldData();
// Now, lets calculate, the max, min and sum of shopping cart item prices
interface CartItem {
userId: string;
itemDesc: string;
price: number;
}
const cart: CartItem[] = [
{ userId: "100", itemDesc: "Arduino Uno kit", price: 60 },
{ userId: "100", itemDesc: "Temp sensor", price: 10 },
{ userId: "100", itemDesc: "Humidity sensor", price: 15 },
{ userId: "100", itemDesc: "Power cord with 5V regulator", price: 18 },
{ userId: "100", itemDesc: "Servo", price: 8 },
];
// add data to indexes
for (const item of cart) {
kv.atomic()
.set(["cart", item.userId], item) // primary index
.min(["cart_min"], BigInt(item.price))
.max(["cart_max"], BigInt(item.price))
.sum(["cart_sum"], BigInt(item.price))
.commit();
}
const cartMin = await kv.get(["cart_min"]);
const cartMax = await kv.get(["cart_max"]);
const cartSum = await kv.get(["cart_sum"]);
console.log("Shopping cart data");
console.log(`Min price: ${(cartMin as Deno.KvEntry<bigint>).value}`);
console.log(`Max price: ${(cartMax as Deno.KvEntry<bigint>).value}`);
console.log(`Total price: ${(cartSum as Deno.KvEntry<bigint>).value}`);