simplify 'enum'
This commit is contained in:
parent
366b1ec4e0
commit
aee8d5b5e1
6 changed files with 113 additions and 59 deletions
46
tests/enum.js
Normal file
46
tests/enum.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
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 { makeModuleEnum } from "../lib/structures/enum.types.js";
|
||||
import { newProduct } from "../lib/structures/product.js";
|
||||
import { lsType } from "../lib/structures/type_constructors.types.js";
|
||||
|
||||
import assert from "node:assert";
|
||||
|
||||
const variants = [
|
||||
newProduct("fever")(_ => Double),
|
||||
newProduct("confused")(_ => Unit),
|
||||
newProduct("other")(_ => lsType(_ => Char)),
|
||||
];
|
||||
|
||||
const symbolSymptom = "Symptom__c0442736bb6f11ae99a51b14e4db44b4";
|
||||
|
||||
const Symptom = makeTypeConstructor(symbolSymptom)(0);
|
||||
|
||||
const [
|
||||
// constructors
|
||||
[, {i: newFever}],
|
||||
[, {i: newConfused}],
|
||||
[, {i: newOther}],
|
||||
|
||||
// match function
|
||||
[, {i: matchSymptom}],
|
||||
] = makeModuleEnum(Symptom)(variants);
|
||||
|
||||
const fever = newFever(38.5);
|
||||
const confused = newConfused(unit);
|
||||
const other = newOther("sweating");
|
||||
|
||||
console.log("observe the encoding of different variant instances:");
|
||||
console.log(" ", fever);
|
||||
console.log(" ", confused);
|
||||
console.log(" ", other);
|
||||
|
||||
const description = symptom => matchSymptom(symptom)
|
||||
(fever => `Fever (${fever} degrees)`)
|
||||
(_confused => `Confused`)
|
||||
(other => `Other: ${other}`);
|
||||
|
||||
assert.strictEqual("Fever (38.5 degrees)", description(fever));
|
||||
assert.strictEqual("Confused" , description(confused));
|
||||
assert.strictEqual("Other: sweating" , description(other));
|
||||
Loading…
Add table
Add a link
Reference in a new issue