Prism · guide

Branch and default

A value that depends on another, and a value that falls back when one is missing.

Install it
pnpm add @niscorp/prism @niscorp/strata zod
Step 1 of 3

A booking

A booking as the box office stores it: who, which show, which seats, and what a seat costs in cents. Every guide here reads this row or a few like it.

A booking
import { evaluate } from '@niscorp/prism';

const booking = {
  id: 'B-2041',
  member: { name: 'Grace', pass: 'season' },
  show: { title: 'The Tempest', starts_at: '2026-10-24T19:30:00+02:00' },
  seats: ['C4', 'C5'],
  seat_price_cents: 3800,
};
Step 2 of 3

One branch or another

$case takes the first branch whose when holds, and else when none does. A season pass owes nothing, and any other booking owes a seat’s price for every seat.

One branch or another
const gross = { $mul: [{ $ref: '$.seat_price_cents' }, { $length: { $ref: '$.seats' } }] };
const due = {
  $case: {
    branches: [{ when: { $eq: [{ $ref: '$.member.pass' }, { $const: 'season' }] }, then: { $const: 0 } }],
    else: gross,
  },
};
console.log(evaluate({ gross, due }, booking));
Step 3 of 3

The first that is there

$coalesce answers the first of its values that is not null. A member with no nickname is called by name.

The first that is there
const called = { $coalesce: [{ $ref: '$.member.nickname' }, { $ref: '$.member.name' }] };
console.log(evaluate(called, { member: { nickname: null, name: 'Grace' } }));
Then

Run it.

The gross and what is due on a season pass, and then the name a member with no nickname goes by.

Run it
npx tsx branch.mts
It prints
{ gross: 7600, due: 0 }
Grace