Skip to content

Examples

Advanced XPR expression patterns for real-world use cases.


Top-N Leaderboard

Sort by score descending, take the top 3, return names only.

xpr
scores.sortBy(x => x.score).reverse().take(3).map(x => x.name)

Context: {"scores": [{"name": "Alice", "score": 95}, {"name": "Bob", "score": 87}, {"name": "Carol", "score": 92}, {"name": "Dave", "score": 78}]}

["Alice", "Carol", "Bob"]


Destructuring in Pipeline

Extract fields from each object while mapping.

xpr
users.map(({name, age}) => `${name} (${age})`)

Context: {"users": [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]}

["Alice (30)", "Bob (25)"]


Age Calculation from Birthdate

Compute age in years from an ISO 8601 date string.

xpr
dateDiff(parseDate(user.dob), now(), "years")

Context: {"user": {"dob": "1990-06-15T00:00:00Z"}}

35 (approximate, depends on current date)


Regex Extraction

Extract a branch name from a Git ref string.

xpr
match(event.ref, "refs/heads/(.+)") ?? "unknown"

Context: {"event": {"ref": "refs/heads/main"}}

"main"


Object Reshape with Spread

Build a new object shape from an existing one, overriding specific fields.

xpr
{...item, fullName: item.first + " " + item.last, age: null}

Context: {"item": {"first": "Jane", "last": "Doe", "age": 28, "role": "admin"}}

{"first": "Jane", "last": "Doe", "age": null, "role": "admin", "fullName": "Jane Doe"}


Active/Inactive Partition

Split a list into two groups and count each.

xpr
let parts = items.partition(x => x.active); [parts[0].length, parts[1].length]

Context: {"items": [{"active": true}, {"active": false}, {"active": true}, {"active": true}]}

[3, 1]


Null-Safe Address Resolution

Walk a deeply nested nullable path with a fallback.

xpr
user?.address?.city ?? user?.location?.city ?? "Unknown"

Context: {"user": {"address": null, "location": {"city": "Paris"}}}

"Paris"


Circle Radius from Area

Compute radius from area using PI and sqrt.

xpr
let r = sqrt(area / PI); round(r * 100) / 100

Context: {"area": 78.54}

5.0


Type-Safe Numeric Sum

Filter to numeric fields only, then sum.

xpr
fields.filter(x => isNumber(x.value)).map(x => x.value).sum()

Context: {"fields": [{"name": "price", "value": 29.99}, {"name": "tag", "value": "sale"}, {"name": "qty", "value": 3}]}

32.99


Normalize Array to Object Lookup

Index a list by a unique key for O(1) access.

xpr
items.keyBy(x => x.id)

Context: {"items": [{"id": "a", "val": 1}, {"id": "b", "val": 2}, {"id": "c", "val": 3}]}

{"a": {"id": "a", "val": 1}, "b": {"id": "b", "val": 2}, "c": {"id": "c", "val": 3}}


Strip Null Fields

Remove null-valued entries from an object.

xpr
user.entries().filter(([k, v]) => v != null) |> fromEntries

Context: {"user": {"name": "Alice", "age": null, "city": "NYC", "phone": null}}

{"name": "Alice", "city": "NYC"}


Date Range Report

Format a date and compute days elapsed since an event.

xpr
let ts = parseDate(event.date); `${formatDate(ts, "yyyy-MM-dd")}: ${dateDiff(ts, now(), "days")} days ago`

Context: {"event": {"date": "2024-01-01T00:00:00Z"}}

"2024-01-01: 804 days ago" (approximate)