@niscorp/prism 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/DESIGN.md +2 -1
  3. package/DOCS.md +29 -0
  4. package/README.md +49 -3
  5. package/dist/agent.cjs +4 -756
  6. package/dist/agent.cjs.map +1 -1
  7. package/dist/agent.js +2 -2
  8. package/dist/chunk-26VYDHHC.cjs +784 -0
  9. package/dist/chunk-26VYDHHC.cjs.map +1 -0
  10. package/dist/{chunk-BRIXDNJQ.js → chunk-O3OJAQWA.js} +3 -3
  11. package/dist/chunk-O3OJAQWA.js.map +1 -0
  12. package/dist/chunk-V426VIRI.cjs +1168 -0
  13. package/dist/chunk-V426VIRI.cjs.map +1 -0
  14. package/dist/{chunk-3GAYBBNH.js → chunk-VC4PRB4H.js} +21 -4
  15. package/dist/chunk-VC4PRB4H.js.map +1 -0
  16. package/dist/chunk-W32JXP33.cjs +147 -0
  17. package/dist/chunk-W32JXP33.cjs.map +1 -0
  18. package/dist/{chunk-3PYQITGE.js → chunk-XRK55NPE.js} +3 -3
  19. package/dist/{chunk-3PYQITGE.js.map → chunk-XRK55NPE.js.map} +1 -1
  20. package/dist/examples/index.cjs +1543 -0
  21. package/dist/examples/index.cjs.map +1 -0
  22. package/dist/examples/index.d.cts +26 -0
  23. package/dist/examples/index.d.ts +26 -0
  24. package/dist/examples/index.js +1540 -0
  25. package/dist/examples/index.js.map +1 -0
  26. package/dist/index.cjs +190 -2048
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.cts +5 -82
  29. package/dist/index.d.ts +5 -82
  30. package/dist/index.js +5 -5
  31. package/dist/migrations/index.cjs +7 -1775
  32. package/dist/migrations/index.cjs.map +1 -1
  33. package/dist/migrations/index.d.cts +3 -2
  34. package/dist/migrations/index.d.ts +3 -2
  35. package/dist/migrations/index.js +3 -18
  36. package/dist/migrations/index.js.map +1 -1
  37. package/dist/profiles-C6r-x-Vj.d.cts +83 -0
  38. package/dist/profiles-C6r-x-Vj.d.ts +83 -0
  39. package/dist/transform-DcVcOBzQ.d.cts +3 -0
  40. package/dist/transform-DcVcOBzQ.d.ts +3 -0
  41. package/package.json +19 -8
  42. package/dist/chunk-3GAYBBNH.js.map +0 -1
  43. package/dist/chunk-BRIXDNJQ.js.map +0 -1
@@ -0,0 +1,1540 @@
1
+ // src/examples/composition.ts
2
+ var COMPOSITION = [
3
+ {
4
+ id: "pick-and-rename",
5
+ group: "composition",
6
+ title: "Pick and rename",
7
+ description: "A plain object in a config is a template: each of its values is an expression. Reading fields into new names is a template of `$ref`s.",
8
+ source: {
9
+ raw: {
10
+ user_id: "u_42",
11
+ first_name: "Ada",
12
+ last_name: "Lovelace",
13
+ created_at: "2024-01-01",
14
+ internal_token: "sekret",
15
+ legacy_field: "ignored"
16
+ }
17
+ },
18
+ config: {
19
+ id: { $ref: "$.raw.user_id" },
20
+ firstName: { $ref: "$.raw.first_name" },
21
+ lastName: { $ref: "$.raw.last_name" },
22
+ createdAt: { $ref: "$.raw.created_at" }
23
+ },
24
+ expected: { id: "u_42", firstName: "Ada", lastName: "Lovelace", createdAt: "2024-01-01" }
25
+ },
26
+ {
27
+ id: "map-to-a-shape",
28
+ group: "composition",
29
+ title: "Map to a shape",
30
+ description: "The body of a `$map` may be a template, so every record of a list comes out in a new shape.",
31
+ source: {
32
+ users: [
33
+ { id: "u_1", first: "Ada", last: "Lovelace" },
34
+ { id: "u_2", first: "Grace", last: "Hopper" },
35
+ { id: "u_3", first: "Linus", last: "Torvalds" }
36
+ ]
37
+ },
38
+ config: {
39
+ $map: {
40
+ over: { $ref: "$.users" },
41
+ as: "u",
42
+ body: {
43
+ id: { $get: { from: { $var: "u" }, path: ["id"] } },
44
+ fullName: {
45
+ $join: {
46
+ sep: " ",
47
+ parts: [
48
+ { $get: { from: { $var: "u" }, path: ["first"] } },
49
+ { $get: { from: { $var: "u" }, path: ["last"] } }
50
+ ]
51
+ }
52
+ }
53
+ }
54
+ }
55
+ },
56
+ expected: [
57
+ { id: "u_1", fullName: "Ada Lovelace" },
58
+ { id: "u_2", fullName: "Grace Hopper" },
59
+ { id: "u_3", fullName: "Linus Torvalds" }
60
+ ]
61
+ },
62
+ {
63
+ id: "filter-then-map",
64
+ group: "composition",
65
+ title: "Filter, then map",
66
+ description: "Operators nest: what one gives is what the next takes. Here the list is filtered first, and what is left is mapped.",
67
+ source: {
68
+ products: [
69
+ { name: "Apple", price: 1.5, inStock: true },
70
+ { name: "Banana", price: 0.5, inStock: false },
71
+ { name: "Carrot", price: 2, inStock: true },
72
+ { name: "Date", price: 3, inStock: false }
73
+ ]
74
+ },
75
+ config: {
76
+ $map: {
77
+ over: {
78
+ $filter: {
79
+ over: { $ref: "$.products" },
80
+ as: "p",
81
+ when: { $get: { from: { $var: "p" }, path: ["inStock"] } }
82
+ }
83
+ },
84
+ as: "p",
85
+ body: { $get: { from: { $var: "p" }, path: ["name"] } }
86
+ }
87
+ },
88
+ expected: ["Apple", "Carrot"]
89
+ },
90
+ {
91
+ id: "calculated-fields",
92
+ group: "composition",
93
+ title: "Calculated fields",
94
+ description: "Derived fields from raw input: the lines of a cart become a subtotal, tax and total. `$with` names the subtotal once, and `$round` fixes the digits.",
95
+ source: {
96
+ items: [
97
+ { name: "Widget", price: 9.99, qty: 3 },
98
+ { name: "Gadget", price: 19.5, qty: 2 },
99
+ { name: "Sprocket", price: 4.25, qty: 5 }
100
+ ],
101
+ taxRate: 0.08
102
+ },
103
+ config: {
104
+ $with: {
105
+ let: {
106
+ subtotal: {
107
+ $sum: {
108
+ over: {
109
+ $map: {
110
+ over: { $ref: "$.items" },
111
+ as: "item",
112
+ body: {
113
+ $mul: [
114
+ { $get: { from: { $var: "item" }, path: ["price"] } },
115
+ { $get: { from: { $var: "item" }, path: ["qty"] } }
116
+ ]
117
+ }
118
+ }
119
+ }
120
+ }
121
+ }
122
+ },
123
+ value: {
124
+ subtotal: { $round: { value: { $var: "subtotal" }, digits: 2 } },
125
+ tax: {
126
+ $round: {
127
+ value: { $mul: [{ $var: "subtotal" }, { $ref: "$.taxRate" }] },
128
+ digits: 2
129
+ }
130
+ },
131
+ total: {
132
+ $round: {
133
+ value: {
134
+ $add: [
135
+ { $var: "subtotal" },
136
+ { $mul: [{ $var: "subtotal" }, { $ref: "$.taxRate" }] }
137
+ ]
138
+ },
139
+ digits: 2
140
+ }
141
+ },
142
+ itemCount: { $count: { over: { $ref: "$.items" } } }
143
+ }
144
+ }
145
+ },
146
+ expected: { subtotal: 90.22, tax: 7.22, total: 97.44, itemCount: 3 }
147
+ },
148
+ {
149
+ id: "denormalize-a-join",
150
+ group: "composition",
151
+ title: "Denormalize a join",
152
+ description: "Two lists that refer to each other become one: each user comes out with the posts that name them, found with a `$filter` inside the `$map`.",
153
+ source: {
154
+ users: [
155
+ { id: "u1", name: "Ada" },
156
+ { id: "u2", name: "Grace" },
157
+ { id: "u3", name: "Linus" }
158
+ ],
159
+ posts: [
160
+ { id: "p1", authorId: "u1", title: "On Bernoulli" },
161
+ { id: "p2", authorId: "u1", title: "Engine notes" },
162
+ { id: "p3", authorId: "u2", title: "COBOL origins" },
163
+ { id: "p4", authorId: "u3", title: "Just for fun" },
164
+ { id: "p5", authorId: "u3", title: "Linux history" }
165
+ ]
166
+ },
167
+ config: {
168
+ $map: {
169
+ over: { $ref: "$.users" },
170
+ as: "user",
171
+ body: {
172
+ id: { $get: { from: { $var: "user" }, path: ["id"] } },
173
+ name: { $get: { from: { $var: "user" }, path: ["name"] } },
174
+ posts: {
175
+ $filter: {
176
+ over: { $ref: "$.posts" },
177
+ as: "post",
178
+ when: {
179
+ $eq: [
180
+ { $get: { from: { $var: "post" }, path: ["authorId"] } },
181
+ { $get: { from: { $var: "user" }, path: ["id"] } }
182
+ ]
183
+ }
184
+ }
185
+ }
186
+ }
187
+ }
188
+ },
189
+ expected: [
190
+ {
191
+ id: "u1",
192
+ name: "Ada",
193
+ posts: [
194
+ { id: "p1", authorId: "u1", title: "On Bernoulli" },
195
+ { id: "p2", authorId: "u1", title: "Engine notes" }
196
+ ]
197
+ },
198
+ {
199
+ id: "u2",
200
+ name: "Grace",
201
+ posts: [{ id: "p3", authorId: "u2", title: "COBOL origins" }]
202
+ },
203
+ {
204
+ id: "u3",
205
+ name: "Linus",
206
+ posts: [
207
+ { id: "p4", authorId: "u3", title: "Just for fun" },
208
+ { id: "p5", authorId: "u3", title: "Linux history" }
209
+ ]
210
+ }
211
+ ]
212
+ }
213
+ ];
214
+
215
+ // src/examples/operators.ts
216
+ var OPERATORS = [
217
+ {
218
+ id: "ref",
219
+ group: "core",
220
+ title: "$ref",
221
+ description: "Reads a value out of the source by JSONPath. A path starts with `$.` and names fields or array indexes.",
222
+ op: "$ref",
223
+ source: {
224
+ user: { id: "u1", name: "Ada Lovelace", email: "ada@example.com" },
225
+ items: [{ sku: "A1" }, { sku: "A2" }, { sku: "A3" }]
226
+ },
227
+ config: { $ref: "$.user.name" },
228
+ expected: "Ada Lovelace"
229
+ },
230
+ {
231
+ id: "const",
232
+ group: "core",
233
+ title: "$const",
234
+ description: "A literal value, returned as it is written. Any JSON value can stand where an expression is expected by wrapping it in `$const`.",
235
+ op: "$const",
236
+ source: {},
237
+ config: { $const: 42 },
238
+ expected: 42
239
+ },
240
+ {
241
+ id: "var",
242
+ group: "core",
243
+ title: "$var",
244
+ description: "Reads a name bound further up: by `$with`, or by the `as` of `$map`, `$filter` and the other operators that go through a list. A name that is not in scope refuses with E_VAR_NOT_FOUND.",
245
+ op: "$var",
246
+ source: { price: 40, rate: 0.2 },
247
+ config: {
248
+ $with: {
249
+ let: { tax: { $mul: [{ $ref: "$.price" }, { $ref: "$.rate" }] } },
250
+ value: { $var: "tax" }
251
+ }
252
+ },
253
+ expected: 8
254
+ },
255
+ {
256
+ id: "get",
257
+ group: "core",
258
+ title: "$get",
259
+ description: "Walks into a value along a list of path segments: keys, indexes, or expressions that evaluate to one. With `fallback` a missing path gives the fallback; without it the config refuses with E_MISSING_PATH.",
260
+ op: "$get",
261
+ source: {
262
+ catalog: { A1: { title: "Apple", stock: 12 }, B1: { title: "Banana", stock: 0 } },
263
+ selected: "B1"
264
+ },
265
+ config: {
266
+ title: { $get: { from: { $ref: "$.catalog" }, path: [{ $ref: "$.selected" }, "title"] } },
267
+ shelf: {
268
+ $get: {
269
+ from: { $ref: "$.catalog" },
270
+ path: [{ $ref: "$.selected" }, "shelf"],
271
+ fallback: { $const: "unknown" }
272
+ }
273
+ }
274
+ },
275
+ expected: { title: "Banana", shelf: "unknown" }
276
+ },
277
+ {
278
+ id: "with",
279
+ group: "core",
280
+ title: "$with",
281
+ description: "Names values once for use further down. What `let` binds is read with `$var`, and only inside `value`.",
282
+ op: "$with",
283
+ source: {},
284
+ config: {
285
+ $with: {
286
+ let: { a: { $const: 10 }, b: { $const: 32 } },
287
+ value: { $add: [{ $var: "a" }, { $var: "b" }] }
288
+ }
289
+ },
290
+ expected: 42
291
+ },
292
+ {
293
+ id: "map",
294
+ group: "arrays",
295
+ title: "$map",
296
+ description: "Transforms each element of an array. The element is bound to the variable named by `as`, and `body` is evaluated once for each.",
297
+ op: "$map",
298
+ source: { numbers: [1, 2, 3, 4, 5] },
299
+ config: {
300
+ $map: {
301
+ over: { $ref: "$.numbers" },
302
+ as: "n",
303
+ body: { $mul: [{ $var: "n" }, { $const: 2 }] }
304
+ }
305
+ },
306
+ expected: [2, 4, 6, 8, 10]
307
+ },
308
+ {
309
+ id: "filter",
310
+ group: "arrays",
311
+ title: "$filter",
312
+ description: "Keeps the elements of an array for which `when` is truthy.",
313
+ op: "$filter",
314
+ source: {
315
+ items: [
316
+ { name: "Apple", price: 1.5 },
317
+ { name: "Banana", price: 0.5 },
318
+ { name: "Carrot", price: 2 },
319
+ { name: "Date", price: 3 }
320
+ ]
321
+ },
322
+ config: {
323
+ $filter: {
324
+ over: { $ref: "$.items" },
325
+ as: "item",
326
+ when: {
327
+ $gte: [{ $get: { from: { $var: "item" }, path: ["price"] } }, { $const: 1.5 }]
328
+ }
329
+ }
330
+ },
331
+ expected: [
332
+ { name: "Apple", price: 1.5 },
333
+ { name: "Carrot", price: 2 },
334
+ { name: "Date", price: 3 }
335
+ ]
336
+ },
337
+ {
338
+ id: "reduce",
339
+ group: "arrays",
340
+ title: "$reduce",
341
+ description: "Folds an array into one value. The element and what has been gathered so far are both in scope inside `body`; the accumulator is called `acc` unless named.",
342
+ op: "$reduce",
343
+ source: { numbers: [3, 1, 4, 1, 5, 9, 2, 6] },
344
+ config: {
345
+ $reduce: {
346
+ over: { $ref: "$.numbers" },
347
+ as: "n",
348
+ acc: "total",
349
+ init: { $const: 0 },
350
+ body: { $add: [{ $var: "total" }, { $var: "n" }] }
351
+ }
352
+ },
353
+ expected: 31
354
+ },
355
+ {
356
+ id: "slice",
357
+ group: "arrays",
358
+ title: "$slice",
359
+ description: "A stretch of an array or of a string, from `start` up to but not including `end`. `start` defaults to 0 and `end` to the length.",
360
+ op: "$slice",
361
+ source: { letters: ["a", "b", "c", "d", "e"], word: "transform" },
362
+ config: {
363
+ middle: { $slice: { from: { $ref: "$.letters" }, start: 1, end: 4 } },
364
+ prefix: { $slice: { from: { $ref: "$.word" }, start: 0, end: 5 } }
365
+ },
366
+ expected: { middle: ["b", "c", "d"], prefix: "trans" }
367
+ },
368
+ {
369
+ id: "flatten",
370
+ group: "arrays",
371
+ title: "$flatten",
372
+ description: "Flattens an array of arrays by one level. An element that is not an array is kept as it is.",
373
+ op: "$flatten",
374
+ source: { nested: [[1, 2], [3], 4, [5, [6]]] },
375
+ config: { $flatten: { $ref: "$.nested" } },
376
+ expected: [1, 2, 3, 4, 5, [6]]
377
+ },
378
+ {
379
+ id: "unique",
380
+ group: "arrays",
381
+ title: "$unique",
382
+ description: "Removes repeated values from an array, keeping the first of each. Values are compared by their JSON text, so objects and arrays count too.",
383
+ op: "$unique",
384
+ source: { tags: ["draft", "review", "draft", "done", "review"] },
385
+ config: { $unique: { $ref: "$.tags" } },
386
+ expected: ["draft", "review", "done"]
387
+ },
388
+ {
389
+ id: "sort-by",
390
+ group: "arrays",
391
+ title: "$sortBy",
392
+ description: "Sorts an array by a key computed for each element. `dir` is `asc` unless it says `desc`; keys may be numbers or strings.",
393
+ op: "$sortBy",
394
+ source: {
395
+ items: [
396
+ { name: "Carrot", price: 2 },
397
+ { name: "Apple", price: 1.5 },
398
+ { name: "Date", price: 3 }
399
+ ]
400
+ },
401
+ config: {
402
+ $sortBy: {
403
+ over: { $ref: "$.items" },
404
+ as: "item",
405
+ by: { $get: { from: { $var: "item" }, path: ["price"] } },
406
+ dir: "desc"
407
+ }
408
+ },
409
+ expected: [
410
+ { name: "Date", price: 3 },
411
+ { name: "Carrot", price: 2 },
412
+ { name: "Apple", price: 1.5 }
413
+ ]
414
+ },
415
+ {
416
+ id: "add",
417
+ group: "math",
418
+ title: "$add",
419
+ description: "Adds two numbers. Each of the four arithmetic operators takes exactly two operands, and an operand is any expression.",
420
+ op: "$add",
421
+ source: { price: 99, tax: 8 },
422
+ config: { $add: [{ $ref: "$.price" }, { $ref: "$.tax" }] },
423
+ expected: 107
424
+ },
425
+ {
426
+ id: "sub",
427
+ group: "math",
428
+ title: "$sub",
429
+ description: "Subtracts the second number from the first.",
430
+ op: "$sub",
431
+ source: { total: 100, discount: 15 },
432
+ config: { $sub: [{ $ref: "$.total" }, { $ref: "$.discount" }] },
433
+ expected: 85
434
+ },
435
+ {
436
+ id: "mul",
437
+ group: "math",
438
+ title: "$mul",
439
+ description: "Multiplies two numbers.",
440
+ op: "$mul",
441
+ source: { quantity: 3, unitPrice: 25 },
442
+ config: { $mul: [{ $ref: "$.quantity" }, { $ref: "$.unitPrice" }] },
443
+ expected: 75
444
+ },
445
+ {
446
+ id: "div",
447
+ group: "math",
448
+ title: "$div",
449
+ description: "Divides the first number by the second. Dividing by zero refuses with E_DIVISION_BY_ZERO.",
450
+ op: "$div",
451
+ source: { total: 100, people: 8 },
452
+ config: { $div: [{ $ref: "$.total" }, { $ref: "$.people" }] },
453
+ expected: 12.5
454
+ },
455
+ {
456
+ id: "round",
457
+ group: "math",
458
+ title: "$round",
459
+ description: "Rounds a number to a number of digits. `digits` is 0 unless given.",
460
+ op: "$round",
461
+ source: { total: 100, guests: 7 },
462
+ config: {
463
+ $round: { value: { $div: [{ $ref: "$.total" }, { $ref: "$.guests" }] }, digits: 2 }
464
+ },
465
+ expected: 14.29
466
+ },
467
+ {
468
+ id: "join",
469
+ group: "strings",
470
+ title: "$join",
471
+ description: "Joins parts into one string with `sep` between them. Parts are turned into strings, and `parts` may be a list or any expression that evaluates to one.",
472
+ op: "$join",
473
+ source: { first: "Ada", last: "Lovelace" },
474
+ config: { $join: { parts: [{ $ref: "$.first" }, { $ref: "$.last" }], sep: " " } },
475
+ expected: "Ada Lovelace"
476
+ },
477
+ {
478
+ id: "to-string",
479
+ group: "strings",
480
+ title: "$toString",
481
+ description: 'Any value as a string: a number as its digits, null as "null", an object or array as its JSON text.',
482
+ op: "$toString",
483
+ source: { count: 42, nothing: null, point: { x: 1, y: 2 } },
484
+ config: {
485
+ count: { $toString: { $ref: "$.count" } },
486
+ nothing: { $toString: { $ref: "$.nothing" } },
487
+ point: { $toString: { $ref: "$.point" } }
488
+ },
489
+ expected: { count: "42", nothing: "null", point: '{"x":1,"y":2}' }
490
+ },
491
+ {
492
+ id: "interpolate",
493
+ group: "strings",
494
+ title: "$interpolate",
495
+ description: "Fills the `{{key}}` placeholders of a template with the matching values. The template is written out; `values` is an object of expressions, or one expression that gives an object.",
496
+ op: "$interpolate",
497
+ source: { user: { first: "Ada", last: "Lovelace" }, count: 3 },
498
+ config: {
499
+ $interpolate: {
500
+ template: "Welcome, {{first}} {{last}}! You have {{count}} new messages.",
501
+ values: {
502
+ first: { $ref: "$.user.first" },
503
+ last: { $ref: "$.user.last" },
504
+ count: { $ref: "$.count" }
505
+ }
506
+ }
507
+ },
508
+ expected: "Welcome, Ada Lovelace! You have 3 new messages."
509
+ },
510
+ {
511
+ id: "fill",
512
+ group: "strings",
513
+ title: "$fill",
514
+ description: "Fills a counted phrase with its own slots: a value of the form `{ phrase, slots }` becomes the phrase with each `{slot}` replaced. A value of any other form passes through unchanged.",
515
+ op: "$fill",
516
+ source: {
517
+ progress: { phrase: "{n} of {total}", slots: { n: 1, total: 12 } },
518
+ plain: "done"
519
+ },
520
+ config: {
521
+ filled: { $fill: { $ref: "$.progress" } },
522
+ untouched: { $fill: { $ref: "$.plain" } }
523
+ },
524
+ expected: { filled: "1 of 12", untouched: "done" }
525
+ },
526
+ {
527
+ id: "trim",
528
+ group: "strings",
529
+ title: "$trim",
530
+ description: "Removes the whitespace at both ends of a string.",
531
+ op: "$trim",
532
+ source: { raw: " Ada Lovelace " },
533
+ config: { $trim: { $ref: "$.raw" } },
534
+ expected: "Ada Lovelace"
535
+ },
536
+ {
537
+ id: "lower",
538
+ group: "strings",
539
+ title: "$lower",
540
+ description: "A string in lowercase.",
541
+ op: "$lower",
542
+ source: { name: "Ada Lovelace" },
543
+ config: { $lower: { $ref: "$.name" } },
544
+ expected: "ada lovelace"
545
+ },
546
+ {
547
+ id: "upper",
548
+ group: "strings",
549
+ title: "$upper",
550
+ description: "A string in uppercase.",
551
+ op: "$upper",
552
+ source: { name: "Ada Lovelace" },
553
+ config: { $upper: { $ref: "$.name" } },
554
+ expected: "ADA LOVELACE"
555
+ },
556
+ {
557
+ id: "split",
558
+ group: "strings",
559
+ title: "$split",
560
+ description: "Cuts a string into a list at every `sep`.",
561
+ op: "$split",
562
+ source: { csv: "apple,banana,cherry" },
563
+ config: { $split: { value: { $ref: "$.csv" }, sep: "," } },
564
+ expected: ["apple", "banana", "cherry"]
565
+ },
566
+ {
567
+ id: "replace",
568
+ group: "strings",
569
+ title: "$replace",
570
+ description: "Swaps the first occurrence of `search` in a string for `replacement`.",
571
+ op: "$replace",
572
+ source: { csv: "apple,banana,cherry" },
573
+ config: { $replace: { value: { $ref: "$.csv" }, search: "banana", replacement: "***" } },
574
+ expected: "apple,***,cherry"
575
+ },
576
+ {
577
+ id: "eq",
578
+ group: "predicates",
579
+ title: "$eq",
580
+ description: "True when two values are the same. They are compared by their JSON text, so lists and objects compare as well as numbers and strings.",
581
+ op: "$eq",
582
+ source: { status: "active" },
583
+ config: { $eq: [{ $ref: "$.status" }, { $const: "active" }] },
584
+ expected: true
585
+ },
586
+ {
587
+ id: "neq",
588
+ group: "predicates",
589
+ title: "$neq",
590
+ description: "True when two values differ. The opposite of `$eq`, compared the same way.",
591
+ op: "$neq",
592
+ source: { role: "editor" },
593
+ config: { $neq: [{ $ref: "$.role" }, { $const: "admin" }] },
594
+ expected: true
595
+ },
596
+ {
597
+ id: "gt",
598
+ group: "predicates",
599
+ title: "$gt",
600
+ description: "True when the first value is greater than the second. Numbers compare by size, strings by alphabet.",
601
+ op: "$gt",
602
+ source: { age: 36 },
603
+ config: { $gt: [{ $ref: "$.age" }, { $const: 18 }] },
604
+ expected: true
605
+ },
606
+ {
607
+ id: "gte",
608
+ group: "predicates",
609
+ title: "$gte",
610
+ description: "True when the first value is greater than the second, or equal to it.",
611
+ op: "$gte",
612
+ source: { age: 18 },
613
+ config: { $gte: [{ $ref: "$.age" }, { $const: 18 }] },
614
+ expected: true
615
+ },
616
+ {
617
+ id: "lt",
618
+ group: "predicates",
619
+ title: "$lt",
620
+ description: "True when the first value is less than the second.",
621
+ op: "$lt",
622
+ source: { stock: 3 },
623
+ config: { $lt: [{ $ref: "$.stock" }, { $const: 5 }] },
624
+ expected: true
625
+ },
626
+ {
627
+ id: "lte",
628
+ group: "predicates",
629
+ title: "$lte",
630
+ description: "True when the first value is less than the second, or equal to it.",
631
+ op: "$lte",
632
+ source: { score: 100 },
633
+ config: { $lte: [{ $ref: "$.score" }, { $const: 100 }] },
634
+ expected: true
635
+ },
636
+ {
637
+ id: "empty",
638
+ group: "predicates",
639
+ title: "$empty",
640
+ description: "True for null, an empty string, an empty list and an empty object. Everything else is not empty, 0 and false included.",
641
+ op: "$empty",
642
+ source: { nothing: null, text: "", list: [], zero: 0 },
643
+ config: {
644
+ nothing: { $empty: { $ref: "$.nothing" } },
645
+ text: { $empty: { $ref: "$.text" } },
646
+ list: { $empty: { $ref: "$.list" } },
647
+ zero: { $empty: { $ref: "$.zero" } }
648
+ },
649
+ expected: { nothing: true, text: true, list: true, zero: false }
650
+ },
651
+ {
652
+ id: "starts-with",
653
+ group: "predicates",
654
+ title: "$startsWith",
655
+ description: "True when a string begins with `prefix`.",
656
+ op: "$startsWith",
657
+ source: { url: "https://nisc.dev" },
658
+ config: { $startsWith: { value: { $ref: "$.url" }, prefix: { $const: "https://" } } },
659
+ expected: true
660
+ },
661
+ {
662
+ id: "ends-with",
663
+ group: "predicates",
664
+ title: "$endsWith",
665
+ description: "True when a string ends with `suffix`.",
666
+ op: "$endsWith",
667
+ source: { file: "notes.json" },
668
+ config: { $endsWith: { value: { $ref: "$.file" }, suffix: { $const: ".json" } } },
669
+ expected: true
670
+ },
671
+ {
672
+ id: "contains",
673
+ group: "predicates",
674
+ title: "$contains",
675
+ description: "True when a string has `search` somewhere in it.",
676
+ op: "$contains",
677
+ source: { name: "Ada Lovelace" },
678
+ config: { $contains: { value: { $ref: "$.name" }, search: { $const: "Love" } } },
679
+ expected: true
680
+ },
681
+ {
682
+ id: "and",
683
+ group: "logic",
684
+ title: "$and",
685
+ description: "Gives its first falsy operand, or the last one when all are truthy, and evaluates no further than that.",
686
+ op: "$and",
687
+ source: { user: { age: 25, hasPaidPlan: true } },
688
+ config: {
689
+ $and: [{ $gte: [{ $ref: "$.user.age" }, { $const: 18 }] }, { $ref: "$.user.hasPaidPlan" }]
690
+ },
691
+ expected: true
692
+ },
693
+ {
694
+ id: "or",
695
+ group: "logic",
696
+ title: "$or",
697
+ description: "Gives its first truthy operand, or the last one when none is, and evaluates no further than that.",
698
+ op: "$or",
699
+ source: { user: { nickname: null, name: "Ada" } },
700
+ config: { $or: [{ $ref: "$.user.nickname" }, { $ref: "$.user.name" }] },
701
+ expected: "Ada"
702
+ },
703
+ {
704
+ id: "not",
705
+ group: "logic",
706
+ title: "$not",
707
+ description: "True for a falsy value, false for a truthy one.",
708
+ op: "$not",
709
+ source: { user: { isVerified: false } },
710
+ config: { $not: { $ref: "$.user.isVerified" } },
711
+ expected: true
712
+ },
713
+ {
714
+ id: "merge",
715
+ group: "structure",
716
+ title: "$merge",
717
+ description: "Merges objects one level deep, left to right. A key in a later object replaces the same key in an earlier one.",
718
+ op: "$merge",
719
+ source: {
720
+ defaults: { theme: "light", fontSize: 14, autoSave: true },
721
+ overrides: { theme: "dark", fontSize: 16 }
722
+ },
723
+ config: { $merge: [{ $ref: "$.defaults" }, { $ref: "$.overrides" }] },
724
+ expected: { theme: "dark", fontSize: 16, autoSave: true }
725
+ },
726
+ {
727
+ id: "coalesce",
728
+ group: "structure",
729
+ title: "$coalesce",
730
+ description: "The first of its values that is not null.",
731
+ op: "$coalesce",
732
+ source: { user: { name: null, displayName: null, email: "ada@example.com" } },
733
+ config: {
734
+ $coalesce: [
735
+ { $ref: "$.user.name" },
736
+ { $ref: "$.user.displayName" },
737
+ { $ref: "$.user.email" },
738
+ { $const: "(no name available)" }
739
+ ]
740
+ },
741
+ expected: "ada@example.com"
742
+ },
743
+ {
744
+ id: "case",
745
+ group: "structure",
746
+ title: "$case",
747
+ description: "Branches. They are tried in order: the first whose `when` is truthy gives its `then`, and `else` answers when none is.",
748
+ op: "$case",
749
+ source: { score: 87 },
750
+ config: {
751
+ $case: {
752
+ branches: [
753
+ {
754
+ when: { $gte: [{ $ref: "$.score" }, { $const: 90 }] },
755
+ then: { $const: "A" }
756
+ },
757
+ {
758
+ when: { $gte: [{ $ref: "$.score" }, { $const: 80 }] },
759
+ then: { $const: "B" }
760
+ },
761
+ {
762
+ when: { $gte: [{ $ref: "$.score" }, { $const: 70 }] },
763
+ then: { $const: "C" }
764
+ }
765
+ ],
766
+ else: { $const: "F" }
767
+ }
768
+ },
769
+ expected: "B"
770
+ },
771
+ {
772
+ id: "entries-of",
773
+ group: "structure",
774
+ title: "$entriesOf",
775
+ description: "Turns an object into a list of `[key, value]` pairs, in the object's own order.",
776
+ op: "$entriesOf",
777
+ source: { settings: { theme: "dark", fontSize: 16 } },
778
+ config: { $entriesOf: { $ref: "$.settings" } },
779
+ expected: [
780
+ ["theme", "dark"],
781
+ ["fontSize", 16]
782
+ ]
783
+ },
784
+ {
785
+ id: "key-by",
786
+ group: "structure",
787
+ title: "$keyBy",
788
+ description: "Turns a list of records into an object keyed by a value computed for each. Where two records give the same key, the later one stays.",
789
+ op: "$keyBy",
790
+ source: {
791
+ products: [
792
+ { sku: "A1", name: "Apple", price: 1.5 },
793
+ { sku: "B1", name: "Banana", price: 0.5 },
794
+ { sku: "C1", name: "Cherry", price: 3 }
795
+ ]
796
+ },
797
+ config: {
798
+ $keyBy: {
799
+ over: { $ref: "$.products" },
800
+ as: "p",
801
+ key: { $get: { from: { $var: "p" }, path: ["sku"] } }
802
+ }
803
+ },
804
+ expected: {
805
+ A1: { sku: "A1", name: "Apple", price: 1.5 },
806
+ B1: { sku: "B1", name: "Banana", price: 0.5 },
807
+ C1: { sku: "C1", name: "Cherry", price: 3 }
808
+ }
809
+ },
810
+ {
811
+ id: "group-by",
812
+ group: "structure",
813
+ title: "$groupBy",
814
+ description: "Sorts a list of records into an object of lists, one for each value of a computed key.",
815
+ op: "$groupBy",
816
+ source: {
817
+ items: [
818
+ { name: "Apple", category: "fruit" },
819
+ { name: "Banana", category: "fruit" },
820
+ { name: "Carrot", category: "vegetable" },
821
+ { name: "Date", category: "fruit" },
822
+ { name: "Eggplant", category: "vegetable" }
823
+ ]
824
+ },
825
+ config: {
826
+ $groupBy: {
827
+ over: { $ref: "$.items" },
828
+ as: "item",
829
+ key: { $get: { from: { $var: "item" }, path: ["category"] } }
830
+ }
831
+ },
832
+ expected: {
833
+ fruit: [
834
+ { name: "Apple", category: "fruit" },
835
+ { name: "Banana", category: "fruit" },
836
+ { name: "Date", category: "fruit" }
837
+ ],
838
+ vegetable: [
839
+ { name: "Carrot", category: "vegetable" },
840
+ { name: "Eggplant", category: "vegetable" }
841
+ ]
842
+ }
843
+ },
844
+ {
845
+ id: "keys",
846
+ group: "objects",
847
+ title: "$keys",
848
+ description: "The keys of an object as a list, in the object's own order.",
849
+ op: "$keys",
850
+ source: { settings: { theme: "dark", fontSize: 16, autoSave: true } },
851
+ config: { $keys: { $ref: "$.settings" } },
852
+ expected: ["theme", "fontSize", "autoSave"]
853
+ },
854
+ {
855
+ id: "values",
856
+ group: "objects",
857
+ title: "$values",
858
+ description: "The values of an object as a list, in the object's own order.",
859
+ op: "$values",
860
+ source: { settings: { theme: "dark", fontSize: 16, autoSave: true } },
861
+ config: { $values: { $ref: "$.settings" } },
862
+ expected: ["dark", 16, true]
863
+ },
864
+ {
865
+ id: "from-entries",
866
+ group: "objects",
867
+ title: "$fromEntries",
868
+ description: "Turns a list of `[key, value]` pairs into an object. The opposite of `$entriesOf`.",
869
+ op: "$fromEntries",
870
+ source: {
871
+ pairs: [
872
+ ["x", 1],
873
+ ["y", 2]
874
+ ]
875
+ },
876
+ config: { $fromEntries: { $ref: "$.pairs" } },
877
+ expected: { x: 1, y: 2 }
878
+ },
879
+ {
880
+ id: "pick",
881
+ group: "objects",
882
+ title: "$pick",
883
+ description: "Keeps the named keys of an object and drops the rest.",
884
+ op: "$pick",
885
+ source: {
886
+ user: {
887
+ id: "u_42",
888
+ name: "Ada Lovelace",
889
+ email: "ada@example.com",
890
+ passwordHash: "sekret-do-not-share",
891
+ sessionToken: "also-sekret",
892
+ createdAt: "2024-01-01"
893
+ }
894
+ },
895
+ config: { $pick: { from: { $ref: "$.user" }, keys: ["id", "name", "email", "createdAt"] } },
896
+ expected: {
897
+ id: "u_42",
898
+ name: "Ada Lovelace",
899
+ email: "ada@example.com",
900
+ createdAt: "2024-01-01"
901
+ }
902
+ },
903
+ {
904
+ id: "omit",
905
+ group: "objects",
906
+ title: "$omit",
907
+ description: "Drops the named keys of an object and keeps the rest.",
908
+ op: "$omit",
909
+ source: {
910
+ user: {
911
+ id: "u_42",
912
+ name: "Ada Lovelace",
913
+ email: "ada@example.com",
914
+ passwordHash: "sekret-do-not-share",
915
+ sessionToken: "also-sekret",
916
+ createdAt: "2024-01-01"
917
+ }
918
+ },
919
+ config: { $omit: { from: { $ref: "$.user" }, keys: ["passwordHash", "sessionToken"] } },
920
+ expected: {
921
+ id: "u_42",
922
+ name: "Ada Lovelace",
923
+ email: "ada@example.com",
924
+ createdAt: "2024-01-01"
925
+ }
926
+ },
927
+ {
928
+ id: "type",
929
+ group: "objects",
930
+ title: "$type",
931
+ description: "Names what kind of value something is: `string`, `number`, `boolean`, `null`, `array` or `object`.",
932
+ op: "$type",
933
+ source: { text: "hello", count: 3, on: true, nothing: null, list: [1, 2], record: { a: 1 } },
934
+ config: {
935
+ text: { $type: { $ref: "$.text" } },
936
+ count: { $type: { $ref: "$.count" } },
937
+ on: { $type: { $ref: "$.on" } },
938
+ nothing: { $type: { $ref: "$.nothing" } },
939
+ list: { $type: { $ref: "$.list" } },
940
+ record: { $type: { $ref: "$.record" } }
941
+ },
942
+ expected: {
943
+ text: "string",
944
+ count: "number",
945
+ on: "boolean",
946
+ nothing: "null",
947
+ list: "array",
948
+ record: "object"
949
+ }
950
+ },
951
+ {
952
+ id: "length",
953
+ group: "objects",
954
+ title: "$length",
955
+ description: "How many elements a list has, or how many characters a string has.",
956
+ op: "$length",
957
+ source: { tags: ["math", "engine", "notes"], name: "Ada Lovelace" },
958
+ config: { tags: { $length: { $ref: "$.tags" } }, name: { $length: { $ref: "$.name" } } },
959
+ expected: { tags: 3, name: 12 }
960
+ },
961
+ {
962
+ id: "has",
963
+ group: "transform",
964
+ title: "$has",
965
+ description: "True when the whole path exists. A key that is there with the value null counts; a key that is missing does not.",
966
+ op: "$has",
967
+ source: { document: { id: "save", props: { label: null } } },
968
+ config: {
969
+ label: { $has: { from: { $ref: "$.document" }, path: ["props", "label"] } },
970
+ tone: { $has: { from: { $ref: "$.document" }, path: ["props", "tone"] } }
971
+ },
972
+ expected: { label: true, tone: false }
973
+ },
974
+ {
975
+ id: "rename-keys",
976
+ group: "transform",
977
+ title: "$renameKeys",
978
+ description: "Renames keys of an object where they stand, so the order of the keys is kept. A key that is not there is passed over.",
979
+ op: "$renameKeys",
980
+ source: { endpoint: { url: "/api/todos", body: { done: false }, transform: "rows" } },
981
+ config: {
982
+ $renameKeys: {
983
+ from: { $ref: "$.endpoint" },
984
+ map: { body: "request", transform: "response", headers: "meta" }
985
+ }
986
+ },
987
+ expected: { url: "/api/todos", request: { done: false }, response: "rows" }
988
+ },
989
+ {
990
+ id: "update",
991
+ group: "transform",
992
+ title: "$update",
993
+ description: "Changes the value at one path and returns everything else identical. The value now at the path is bound as `current` (null when it is absent) for the new value to be computed from.",
994
+ op: "$update",
995
+ source: { document: { component: "Stepper", props: { count: 3, label: "Seats" } } },
996
+ config: {
997
+ $update: {
998
+ from: { $ref: "$.document" },
999
+ path: ["props", "count"],
1000
+ value: { $add: [{ $var: "current" }, { $const: 1 }] }
1001
+ }
1002
+ },
1003
+ expected: { component: "Stepper", props: { count: 4, label: "Seats" } }
1004
+ },
1005
+ {
1006
+ id: "assert",
1007
+ group: "transform",
1008
+ title: "$assert",
1009
+ description: "Gives `value` when `when` is truthy, and otherwise refuses with E_ASSERT and the message. It is how a config says what it will not work on.",
1010
+ op: "$assert",
1011
+ source: { document: { id: "todos.list", title: "Todos" } },
1012
+ config: {
1013
+ $assert: {
1014
+ when: { $has: { from: { $ref: "$.document" }, path: ["id"] } },
1015
+ message: "A document without an id cannot be migrated.",
1016
+ value: { $ref: "$.document" }
1017
+ }
1018
+ },
1019
+ expected: { id: "todos.list", title: "Todos" }
1020
+ },
1021
+ {
1022
+ id: "walk",
1023
+ group: "transform",
1024
+ title: "$walk",
1025
+ description: "Visits every node of a tree, at any depth, and replaces a node with the first rule whose `when` holds for it. Children are rewritten before their parent unless `order` is `pre`. A walk visits objects and lists as well as leaves, so a rule for strings checks the type first.",
1026
+ op: "$walk",
1027
+ source: {
1028
+ document: {
1029
+ component: "Stack",
1030
+ children: [
1031
+ { component: "Text", children: "{{$.q}}" },
1032
+ { component: "Field", props: { value: "$.q" } }
1033
+ ]
1034
+ }
1035
+ },
1036
+ config: {
1037
+ $walk: {
1038
+ over: { $ref: "$.document" },
1039
+ as: "n",
1040
+ rules: [
1041
+ {
1042
+ when: { $eq: [{ $var: "n" }, { $const: "$.q" }] },
1043
+ then: { $const: "$.search" }
1044
+ },
1045
+ {
1046
+ when: { $eq: [{ $var: "n" }, { $const: "{{$.q}}" }] },
1047
+ then: { $const: "{{$.search}}" }
1048
+ }
1049
+ ]
1050
+ }
1051
+ },
1052
+ expected: {
1053
+ component: "Stack",
1054
+ children: [
1055
+ { component: "Text", children: "{{$.search}}" },
1056
+ { component: "Field", props: { value: "$.search" } }
1057
+ ]
1058
+ }
1059
+ },
1060
+ {
1061
+ id: "date",
1062
+ group: "time",
1063
+ title: "$date",
1064
+ description: "Writes a date in a given format. The format is a dayjs format string.",
1065
+ op: "$date",
1066
+ source: { createdAt: "2024-01-15T08:30:00Z" },
1067
+ config: { $date: { value: { $ref: "$.createdAt" }, format: "YYYY-MM-DD" } },
1068
+ expected: "2024-01-15"
1069
+ },
1070
+ {
1071
+ id: "date-add",
1072
+ group: "time",
1073
+ title: "$dateAdd",
1074
+ description: "Moves a date by an amount of a unit. A negative amount moves it back.",
1075
+ op: "$dateAdd",
1076
+ source: { createdAt: "2024-01-15T08:30:00Z" },
1077
+ config: {
1078
+ $date: {
1079
+ value: { $dateAdd: { date: { $ref: "$.createdAt" }, amount: 30, unit: "day" } },
1080
+ format: "YYYY-MM-DD"
1081
+ }
1082
+ },
1083
+ expected: "2024-02-14"
1084
+ },
1085
+ {
1086
+ id: "date-diff",
1087
+ group: "time",
1088
+ title: "$dateDiff",
1089
+ description: "Measures from one date to another, in a unit.",
1090
+ op: "$dateDiff",
1091
+ source: { createdAt: "2024-01-15T08:30:00Z", now: "2024-04-22T10:00:00Z" },
1092
+ config: { $dateDiff: { from: { $ref: "$.createdAt" }, to: { $ref: "$.now" }, unit: "day" } },
1093
+ expected: 98
1094
+ },
1095
+ {
1096
+ id: "locale-money",
1097
+ group: "locale",
1098
+ title: "$localeMoney",
1099
+ description: "An amount as money, the way a reader in a given locale expects it. The value is in minor units (cents) unless `minorUnits` is false. `locale` is required: there is no default, because any default is wrong for most readers.",
1100
+ op: "$localeMoney",
1101
+ source: { price_cents: 4500, currency: "EUR" },
1102
+ config: {
1103
+ $localeMoney: {
1104
+ value: { $ref: "$.price_cents" },
1105
+ currency: { $ref: "$.currency" },
1106
+ locale: "en-IE"
1107
+ }
1108
+ },
1109
+ expected: "\u20AC45.00"
1110
+ },
1111
+ {
1112
+ id: "locale-date",
1113
+ group: "locale",
1114
+ title: "$localeDate",
1115
+ description: "A date for a reader in a given locale. `locale` is required. A date without a time is read as UTC, so it does not slip a day west of Greenwich.",
1116
+ op: "$localeDate",
1117
+ source: { starts_on: "2026-03-14" },
1118
+ config: { $localeDate: { value: { $ref: "$.starts_on" }, locale: "en-GB" } },
1119
+ expected: "14 Mar 2026"
1120
+ },
1121
+ {
1122
+ id: "locale-number",
1123
+ group: "locale",
1124
+ title: "$localeNumber",
1125
+ description: "A number for a reader in a given locale: grouping and the decimal mark follow the locale. `locale` is required, and `style` may be `percent`.",
1126
+ op: "$localeNumber",
1127
+ source: { attendance: 1234.5 },
1128
+ config: {
1129
+ $localeNumber: { value: { $ref: "$.attendance" }, locale: "de-DE", minDigits: 1 }
1130
+ },
1131
+ expected: "1.234,5"
1132
+ }
1133
+ ];
1134
+
1135
+ // src/examples/real-world.ts
1136
+ var REAL_WORLD = [
1137
+ {
1138
+ id: "api-to-ui",
1139
+ group: "real-world",
1140
+ title: "An API response, for a screen",
1141
+ description: "A response with paging and bookkeeping around its records becomes the smaller document a screen reads.",
1142
+ source: {
1143
+ response: {
1144
+ meta: { total: 3, page: 1, perPage: 10 },
1145
+ data: [
1146
+ {
1147
+ id: "user_001",
1148
+ attributes: {
1149
+ displayName: "Ada Lovelace",
1150
+ email: "ada@example.com",
1151
+ avatarUrl: "/avatars/ada.png"
1152
+ },
1153
+ permissions: { canEdit: true, canDelete: false }
1154
+ },
1155
+ {
1156
+ id: "user_002",
1157
+ attributes: {
1158
+ displayName: "Grace Hopper",
1159
+ email: "grace@example.com",
1160
+ avatarUrl: "/avatars/grace.png"
1161
+ },
1162
+ permissions: { canEdit: true, canDelete: true }
1163
+ },
1164
+ {
1165
+ id: "user_003",
1166
+ attributes: {
1167
+ displayName: "Linus Torvalds",
1168
+ email: "linus@example.com",
1169
+ avatarUrl: "/avatars/linus.png"
1170
+ },
1171
+ permissions: { canEdit: false, canDelete: false }
1172
+ }
1173
+ ]
1174
+ }
1175
+ },
1176
+ config: {
1177
+ total: { $ref: "$.response.meta.total" },
1178
+ users: {
1179
+ $map: {
1180
+ over: { $ref: "$.response.data" },
1181
+ as: "u",
1182
+ body: {
1183
+ id: { $get: { from: { $var: "u" }, path: ["id"] } },
1184
+ name: { $get: { from: { $var: "u" }, path: ["attributes", "displayName"] } },
1185
+ email: { $get: { from: { $var: "u" }, path: ["attributes", "email"] } },
1186
+ avatar: { $get: { from: { $var: "u" }, path: ["attributes", "avatarUrl"] } },
1187
+ isAdmin: { $get: { from: { $var: "u" }, path: ["permissions", "canDelete"] } }
1188
+ }
1189
+ }
1190
+ }
1191
+ },
1192
+ expected: {
1193
+ total: 3,
1194
+ users: [
1195
+ {
1196
+ id: "user_001",
1197
+ name: "Ada Lovelace",
1198
+ email: "ada@example.com",
1199
+ avatar: "/avatars/ada.png",
1200
+ isAdmin: false
1201
+ },
1202
+ {
1203
+ id: "user_002",
1204
+ name: "Grace Hopper",
1205
+ email: "grace@example.com",
1206
+ avatar: "/avatars/grace.png",
1207
+ isAdmin: true
1208
+ },
1209
+ {
1210
+ id: "user_003",
1211
+ name: "Linus Torvalds",
1212
+ email: "linus@example.com",
1213
+ avatar: "/avatars/linus.png",
1214
+ isAdmin: false
1215
+ }
1216
+ ]
1217
+ }
1218
+ },
1219
+ {
1220
+ id: "top-of-a-list",
1221
+ group: "real-world",
1222
+ title: "The top of a list",
1223
+ description: "A leaderboard: sort by a field, highest first, and keep the first three.",
1224
+ source: {
1225
+ players: [
1226
+ { name: "Ada", score: 187 },
1227
+ { name: "Grace", score: 245 },
1228
+ { name: "Linus", score: 132 },
1229
+ { name: "Margaret", score: 298 },
1230
+ { name: "Donald", score: 91 },
1231
+ { name: "Tim", score: 211 }
1232
+ ]
1233
+ },
1234
+ config: {
1235
+ $slice: {
1236
+ from: {
1237
+ $sortBy: {
1238
+ over: { $ref: "$.players" },
1239
+ as: "p",
1240
+ by: { $get: { from: { $var: "p" }, path: ["score"] } },
1241
+ dir: "desc"
1242
+ }
1243
+ },
1244
+ start: 0,
1245
+ end: 3
1246
+ }
1247
+ },
1248
+ expected: [
1249
+ { name: "Margaret", score: 298 },
1250
+ { name: "Grace", score: 245 },
1251
+ { name: "Tim", score: 211 }
1252
+ ]
1253
+ },
1254
+ {
1255
+ id: "search-sort-paginate",
1256
+ group: "real-world",
1257
+ title: "Search, sort, paginate",
1258
+ description: "What a list view does, as one config: keep what matches the search, sort it, and cut out one page.",
1259
+ source: {
1260
+ products: [
1261
+ { name: "Apple iPhone", price: 999 },
1262
+ { name: "Samsung Galaxy", price: 899 },
1263
+ { name: "Apple Watch", price: 399 },
1264
+ { name: "Apple iPad", price: 599 },
1265
+ { name: "Google Pixel", price: 699 },
1266
+ { name: "Apple MacBook", price: 1299 }
1267
+ ],
1268
+ pageSize: 2,
1269
+ pageIndex: 1
1270
+ },
1271
+ config: {
1272
+ $with: {
1273
+ let: {
1274
+ filtered: {
1275
+ $sortBy: {
1276
+ over: {
1277
+ $filter: {
1278
+ over: { $ref: "$.products" },
1279
+ as: "p",
1280
+ when: {
1281
+ $contains: {
1282
+ value: { $get: { from: { $var: "p" }, path: ["name"] } },
1283
+ search: { $const: "Apple" }
1284
+ }
1285
+ }
1286
+ }
1287
+ },
1288
+ as: "p",
1289
+ by: { $get: { from: { $var: "p" }, path: ["price"] } },
1290
+ dir: "asc"
1291
+ }
1292
+ }
1293
+ },
1294
+ value: {
1295
+ total: { $count: { over: { $var: "filtered" } } },
1296
+ page: {
1297
+ $take: { from: { $drop: { from: { $var: "filtered" }, count: 2 } }, count: 2 }
1298
+ }
1299
+ }
1300
+ }
1301
+ },
1302
+ expected: {
1303
+ total: 4,
1304
+ page: [
1305
+ { name: "Apple iPhone", price: 999 },
1306
+ { name: "Apple MacBook", price: 1299 }
1307
+ ]
1308
+ }
1309
+ },
1310
+ {
1311
+ id: "analytics-summary",
1312
+ group: "real-world",
1313
+ title: "A summary of orders",
1314
+ description: "A small dashboard from raw orders: revenue, the average and the largest order, how many there were, and who ordered.",
1315
+ source: {
1316
+ orders: [
1317
+ { id: "o1", customer: "Ada", amount: 120 },
1318
+ { id: "o2", customer: "Grace", amount: 85 },
1319
+ { id: "o3", customer: "Ada", amount: 200 },
1320
+ { id: "o4", customer: "Linus", amount: 45 },
1321
+ { id: "o5", customer: "Grace", amount: 175 },
1322
+ { id: "o6", customer: "Ada", amount: 60 }
1323
+ ]
1324
+ },
1325
+ config: {
1326
+ totalRevenue: { $sum: { over: { $pluck: { over: { $ref: "$.orders" }, key: "amount" } } } },
1327
+ avgOrder: { $avg: { over: { $pluck: { over: { $ref: "$.orders" }, key: "amount" } } } },
1328
+ maxOrder: { $max: { over: { $pluck: { over: { $ref: "$.orders" }, key: "amount" } } } },
1329
+ orderCount: { $count: { over: { $ref: "$.orders" } } },
1330
+ uniqueCustomers: { $unique: { $pluck: { over: { $ref: "$.orders" }, key: "customer" } } }
1331
+ },
1332
+ expected: {
1333
+ totalRevenue: 685,
1334
+ avgOrder: 114.16666666666667,
1335
+ maxOrder: 200,
1336
+ orderCount: 6,
1337
+ uniqueCustomers: ["Ada", "Grace", "Linus"]
1338
+ }
1339
+ }
1340
+ ];
1341
+
1342
+ // src/examples/sugar.ts
1343
+ var SUGAR = [
1344
+ {
1345
+ id: "sum",
1346
+ group: "sugar",
1347
+ title: "$sum",
1348
+ description: "Adds up the numbers of a list. Shorthand for a `$reduce` with an `$add`.",
1349
+ op: "$sum",
1350
+ source: { values: [10, 20, 30, 40] },
1351
+ config: { $sum: { over: { $ref: "$.values" } } },
1352
+ expected: 100
1353
+ },
1354
+ {
1355
+ id: "count",
1356
+ group: "sugar",
1357
+ title: "$count",
1358
+ description: "Counts the elements of a list. Shorthand for a `$reduce` that adds one for each.",
1359
+ op: "$count",
1360
+ source: { items: ["apple", "banana", "cherry", "date", "elderberry"] },
1361
+ config: { $count: { over: { $ref: "$.items" } } },
1362
+ expected: 5
1363
+ },
1364
+ {
1365
+ id: "avg",
1366
+ group: "sugar",
1367
+ title: "$avg",
1368
+ description: "The mean of the numbers of a list. Shorthand for the `$sum` divided by the `$count`.",
1369
+ op: "$avg",
1370
+ source: { scores: [80, 90, 100, 70] },
1371
+ config: { $avg: { over: { $ref: "$.scores" } } },
1372
+ expected: 85
1373
+ },
1374
+ {
1375
+ id: "min",
1376
+ group: "sugar",
1377
+ title: "$min",
1378
+ description: "The smallest number of a list. Shorthand for a `$reduce` that keeps the smaller of two.",
1379
+ op: "$min",
1380
+ source: { temps: [22, 18, 25, 14, 27, 19] },
1381
+ config: { $min: { over: { $ref: "$.temps" } } },
1382
+ expected: 14
1383
+ },
1384
+ {
1385
+ id: "max",
1386
+ group: "sugar",
1387
+ title: "$max",
1388
+ description: "The largest number of a list. Shorthand for a `$reduce` that keeps the larger of two.",
1389
+ op: "$max",
1390
+ source: { scores: [42, 67, 89, 33, 95, 78] },
1391
+ config: { $max: { over: { $ref: "$.scores" } } },
1392
+ expected: 95
1393
+ },
1394
+ {
1395
+ id: "pluck",
1396
+ group: "sugar",
1397
+ title: "$pluck",
1398
+ description: "One field out of every record of a list. Shorthand for a `$map` whose body is a `$get` of that key.",
1399
+ op: "$pluck",
1400
+ source: {
1401
+ users: [
1402
+ { id: "u1", name: "Ada" },
1403
+ { id: "u2", name: "Grace" },
1404
+ { id: "u3", name: "Linus" }
1405
+ ]
1406
+ },
1407
+ config: { $pluck: { over: { $ref: "$.users" }, key: "name" } },
1408
+ expected: ["Ada", "Grace", "Linus"]
1409
+ },
1410
+ {
1411
+ id: "take",
1412
+ group: "sugar",
1413
+ title: "$take",
1414
+ description: "The first elements of a list, as many as asked for. Shorthand for a `$slice` from the start.",
1415
+ op: "$take",
1416
+ source: { numbers: [10, 20, 30, 40, 50, 60, 70, 80] },
1417
+ config: { $take: { from: { $ref: "$.numbers" }, count: 3 } },
1418
+ expected: [10, 20, 30]
1419
+ },
1420
+ {
1421
+ id: "drop",
1422
+ group: "sugar",
1423
+ title: "$drop",
1424
+ description: "A list without its first elements, as many as asked for. Shorthand for a `$slice` to the end.",
1425
+ op: "$drop",
1426
+ source: { numbers: [10, 20, 30, 40, 50, 60, 70, 80] },
1427
+ config: { $drop: { from: { $ref: "$.numbers" }, count: 3 } },
1428
+ expected: [40, 50, 60, 70, 80]
1429
+ },
1430
+ {
1431
+ id: "match",
1432
+ group: "sugar",
1433
+ title: "$match",
1434
+ description: "Keeps the strings of a list that contain a given text. Shorthand for a `$filter` with a `$contains`.",
1435
+ op: "$match",
1436
+ source: { tags: ["frontend", "backend", "devops", "frontend-react", "database"] },
1437
+ config: { $match: { over: { $ref: "$.tags" }, as: "tag", search: { $const: "front" } } },
1438
+ expected: ["frontend", "frontend-react"]
1439
+ },
1440
+ {
1441
+ id: "flat-map",
1442
+ group: "sugar",
1443
+ title: "$flatMap",
1444
+ description: "Maps each element to a list and joins those lists into one. Shorthand for a `$flatten` over a `$map`.",
1445
+ op: "$flatMap",
1446
+ source: {
1447
+ folders: [
1448
+ { name: "work", files: ["report.pdf", "notes.md"] },
1449
+ { name: "personal", files: ["photo.jpg"] },
1450
+ { name: "archive", files: ["old.txt", "older.txt", "oldest.txt"] }
1451
+ ]
1452
+ },
1453
+ config: {
1454
+ $flatMap: {
1455
+ over: { $ref: "$.folders" },
1456
+ as: "folder",
1457
+ body: { $get: { from: { $var: "folder" }, path: ["files"] } }
1458
+ }
1459
+ },
1460
+ expected: ["report.pdf", "notes.md", "photo.jpg", "old.txt", "older.txt", "oldest.txt"]
1461
+ }
1462
+ ];
1463
+
1464
+ // src/examples/groups.ts
1465
+ var PRISM_EXAMPLE_GROUPS = [
1466
+ {
1467
+ id: "core",
1468
+ title: "Core",
1469
+ description: "Where the values in a config come from: what it was given, a value written out, or a name bound further up."
1470
+ },
1471
+ {
1472
+ id: "arrays",
1473
+ title: "Arrays",
1474
+ description: "Going through a list: each element in turn, some of them, or all of them into one value."
1475
+ },
1476
+ { id: "math", title: "Math", description: "Arithmetic on two numbers, and rounding." },
1477
+ {
1478
+ id: "strings",
1479
+ title: "Strings",
1480
+ description: "Putting strings together and taking them apart."
1481
+ },
1482
+ {
1483
+ id: "predicates",
1484
+ title: "Predicates",
1485
+ description: "Comparisons and checks. Each gives true or false."
1486
+ },
1487
+ { id: "logic", title: "Logic", description: "Combining what is true: and, or, not." },
1488
+ {
1489
+ id: "structure",
1490
+ title: "Structure",
1491
+ description: "Choosing between values, and turning lists and objects into each other."
1492
+ },
1493
+ {
1494
+ id: "objects",
1495
+ title: "Objects",
1496
+ description: "Taking an object apart: its keys, its values, some of them, and what kind of value something is."
1497
+ },
1498
+ {
1499
+ id: "transform",
1500
+ title: "Transform",
1501
+ description: "Rewriting a document in place instead of deriving a value from it. Built for migrations: a config that returns what it was given with one exact edit."
1502
+ },
1503
+ {
1504
+ id: "time",
1505
+ title: "Time",
1506
+ description: "Dates as data: written in a format, moved, and measured."
1507
+ },
1508
+ {
1509
+ id: "locale",
1510
+ title: "Locale",
1511
+ description: "Values written the way a reader in a given language expects them."
1512
+ },
1513
+ {
1514
+ id: "sugar",
1515
+ title: "Sugar",
1516
+ description: "Shorthands for what configs do most. Each is rewritten into the operators it stands for before a config is evaluated."
1517
+ },
1518
+ {
1519
+ id: "composition",
1520
+ title: "Composition",
1521
+ description: "Operators working together: templates, nesting, and naming a value once."
1522
+ },
1523
+ {
1524
+ id: "real-world",
1525
+ title: "Real world",
1526
+ description: "Whole configs, of the size an app writes."
1527
+ }
1528
+ ];
1529
+
1530
+ // src/examples/index.ts
1531
+ var PRISM_EXAMPLES = [
1532
+ ...OPERATORS,
1533
+ ...SUGAR,
1534
+ ...COMPOSITION,
1535
+ ...REAL_WORLD
1536
+ ];
1537
+
1538
+ export { PRISM_EXAMPLES, PRISM_EXAMPLE_GROUPS };
1539
+ //# sourceMappingURL=index.js.map
1540
+ //# sourceMappingURL=index.js.map