@inixiative/json-rules 4.0.0 → 4.1.0

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.
package/README.md CHANGED
@@ -302,6 +302,37 @@ What a consumer checks before saving one:
302
302
 
303
303
  Value expressions evaluate in memory only; `toPrisma` / `toSql` do not compile them.
304
304
 
305
+ ### Categorize
306
+
307
+ `categorize(categorization, row, options)` puts a row in a category: cases checked in order, each
308
+ an `if` (a rule) and a `then` (a value expression), with the options `check()` takes.
309
+
310
+ ```ts
311
+ categorize(
312
+ {
313
+ cases: [
314
+ { if: { field: 'deals', operator: 'greaterThanEquals', value: 10 }, then: 'gold' },
315
+ { if: { field: 'deals', operator: 'greaterThanEquals', value: 3 }, then: 'silver' },
316
+ ],
317
+ default: 'bronze',
318
+ },
319
+ { deals: 4 },
320
+ ); // 'silver'
321
+ ```
322
+
323
+ | Key | Meaning |
324
+ | --- | --- |
325
+ | `cases` | `[{ if, then }]`, in order |
326
+ | `match` | `'first'` (the default): the first case that holds gives the value, and later cases are not checked. `'all'`: every case that holds, in case order, as a list |
327
+ | `default` | the value when no case holds (under `all`, a list of it); with none, `null` (`[]` under `all`) |
328
+
329
+ A `then` can be any value expression: a field/value map is `{ value: { status: 'canceled' } }`.
330
+ A case that throws stops the categorization; it never falls through to a later case or the
331
+ default, which would give a value its author ordered below it. `validateCategorization` /
332
+ `assertValidCategorization` check the shape without data: known keys, a known `match`, each `if`
333
+ as `validateRule` sees it and each `then` and the `default` as `validateValue` does. In memory
334
+ only, like value expressions.
335
+
305
336
  ### Date Rule
306
337
 
307
338
  ```ts