User Data Processing with Ende Collections
Demonstrates using Ende’s @ende/collections library for processing user data. This example uses pick, omit, and partition to manipulate data structures.
import { omit, partition, pick } from "jsr:@ende/collections";Define the User type with fields for id, name, role, age, and country.
type User = {
age: number;
country: string;
id: number;
name: string;
role: 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" }
];- Pick specific fields from each user for selective data extraction.
Using
pickto 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);- Omit specific fields from each user to remove sensitive data.
Using
omitto exclude theidfield from each user object in the new array.
const omitUsers = users.map((user) => omit(user, ["id"]));
console.log("Omitted user data:", omitUsers);- Partition users based on role to categorize them into admins and regular users.
Using partition to split
usersarray 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);Run this example locally using the Ende CLI:
ende run https://docs.ende.sh/examples/scripts/data_processing.ts