nicer pattern matching

This commit is contained in:
Joeri Exelmans 2025-06-17 11:12:34 +02:00
parent d5821c9cb9
commit 794cd3f120
2 changed files with 30 additions and 0 deletions

View file

@ -1,6 +1,7 @@
import { makeTypeConstructor } from "../lib/meta/type_constructor.js";
import { Char, Double, Unit } from "../lib/primitives/primitive_types.js";
import { unit } from "../lib/primitives/unit.js";
import { makeNiceMatchFn, symbolElse } from "../lib/structures/enum.js";
import { makeModuleEnum } from "../lib/structures/enum.types.js";
import { newProduct } from "../lib/structures/product.js";
import { lsType } from "../lib/structures/type_constructors.types.js";
@ -44,3 +45,13 @@ const description = symptom => matchSymptom(symptom)
assert.strictEqual("Fever (38.5 degrees)", description(fever));
assert.strictEqual("Confused" , description(confused));
assert.strictEqual("Other: sweating" , description(other));
// looks almost the same, but no currying, and the ability to have 'else'
const description2 = makeNiceMatchFn(variants)({
fever: fever => `Fever (${fever} degrees)`,
[symbolElse]: _ => `Something else`,
});
assert.strictEqual("Fever (38.5 degrees)", description2(fever));
assert.strictEqual("Something else" , description2(confused));
assert.strictEqual("Something else" , description2(other));