-
Notifications
You must be signed in to change notification settings - Fork 4
/
GeometryDataSimple.ts
51 lines (43 loc) · 1 KB
/
GeometryDataSimple.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Alge } from '../src/index.js'
import { z } from 'zod'
/**
* Builder
*/
const Length = z.number().positive()
export const Shape = Alge.data(`Shape`, {
Rectangle: {
width: Length,
height: Length,
},
Circle: {
radius: Length,
},
Square: {
size: Length,
},
})
/**
* Controller
*/
const circle = Shape.Circle.create({ radius: 11 })
const square = Shape.Square.create({ size: 13 })
const shape = Math.random() > 0.5 ? circle : square
Shape.Square.is(circle)
Shape.Circle.is(circle)
Shape.Circle.to.json(circle)
Shape.Circle.from.json(`{ "radius" 11, "_tag": "Circle" }`)
Shape.to.json(shape)
Shape.from.json(`{ "width" 17, "height": 22 "_tag": "Square" }`)
/**
* Matcher
*/
Alge.match(shape)
.Circle({ radius: 10 }, () => true)
.else(null)
Alge.match(shape)
.Circle({ radius: 11 }, () => `big circle`)
.Circle({ radius: 1 }, () => `little circle`)
.Circle(() => `other circle`)
.Square({ size: 13 }, () => `unlucky square`)
.Square(() => `a good square`)
.done()