-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathdata_processing.ts
49 lines (43 loc) · 1.85 KB
/
data_processing.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
/**
* @title User Data Processing with Deno Collections
* @difficulty intermediate
* @tags cli, deploy
* @group Data Processing
* @run -R data-processing.ts
*
* Demonstrates using Deno's @std/collections library for processing user data.
* This example uses pick, omit, and partition to manipulate data structures.
*/
import { omit, partition, pick } from "jsr:@std/collections";
// Define the User type with fields for id, name, role, age, and country
type User = {
id: number;
name: string;
role: string;
age: number;
country: string;
};
// Sample array of user data for demonstration purposes
const users: User[] = [
{ id: 1, name: "Alice", role: "admin", age: 30, country: "USA" },
{ id: 2, name: "Bob", role: "user", age: 25, country: "Canada" },
{ id: 3, name: "Charlie", role: "user", age: 28, country: "USA" },
{ id: 4, name: "Dave", role: "admin", age: 35, country: "Canada" },
{ id: 5, name: "Eve", role: "user", age: 22, country: "UK" },
];
// 1. Pick specific fields from each user for selective data extraction
// Using pick to include only id, name, and country for each user in the new array
const pickedUsers = users.map((user) => pick(user, ["id", "name", "country"]));
console.log("Picked user data:", pickedUsers);
// 2. Omit specific fields from each user to remove sensitive data
// Using omit to exclude the "id" field from each user object in the new array
const omitUsers = users.map((user) => omit(user, ["id"]));
console.log("Omitted user data:", omitUsers);
// 3. Partition users based on role to categorize them into admins and regular users
// Using partition to split users array into two groups: admins and regular users
const [admins, regularUsers] = partition(
users,
(user) => user.role === "admin", // Condition to check if user role is admin
);
console.log("Admins:", admins);
console.log("Regular Users:", regularUsers);