Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example demonstrating Deno collections for user data processing #1107

Merged
merged 5 commits into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions examples/data-processing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @title User Data Processing with Deno Collections
* @difficulty intermediate
* @tags cli, deploy
* @group Data Processing
* @run --allow-read 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);
Loading