Last active
August 2, 2022 03:32
-
-
Save jasonkuhrt/7ad238bcc13e853ff7456f624f794879 to your computer and use it in GitHub Desktop.
Explore New Alge APIs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Shape = pipe( | |
union(`Shape`), | |
pipe( | |
record('Rectangle', { width: Length, height: Length }), | |
codec('graphic', { | |
to: (rectangle) => `${rectangle.width}x${rectangle.height}`, | |
from: (graphic) => { | |
const match = graphic.match(`(\d+)x(\d+)`) | |
if (!match) return null | |
const [_, width, height] = match | |
return { | |
width: Number(width), | |
height: Number(height), | |
} | |
}, | |
}) | |
), | |
pipe( | |
record('Square', { size: Length }), | |
codec('graphic', { | |
to: (square) => `[${square}]`, | |
from: (graphic) => { | |
const match = graphic.match(`[(\d+)]`) | |
if (!match) return null | |
const [_, size] = match | |
return { | |
size: Number(size), | |
} | |
}, | |
}) | |
), | |
pipe( | |
record('Circle', { radius: Length }), | |
codec('graphic', { | |
to: (circle) => `(---|${circle.radius})`, | |
from: (graphic) => { | |
const match = graphic.match(`\(---|(\d+)\)`) | |
if (!match) return null | |
const [_, radius] = match | |
return { | |
radius: Number(radius), | |
} | |
}, | |
}) | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Overload 1: Simple Centralized | |
const Shape = Alge.union(`Shape`, { | |
Rectangle: { | |
width: Length, | |
height: Length, | |
}, | |
Circle: { | |
radius: Length, | |
}, | |
Square: { | |
size: Length, | |
}, | |
}) | |
//-------------------------------------------------------------------------------------- | |
// Overload 2: Simple Separated | |
const Rectangle = Alge.record(`Rectangle`, { | |
width: Length, | |
height: Length, | |
}) | |
const Circle = Alge.record(`Circle`, { | |
radius: Length, | |
}) | |
const Square = Alge.record(`Square`, { | |
size: Length, | |
}) | |
const Shape = Alge.union(`Shape`, { | |
Rectangle, | |
Circle, | |
Square, | |
}) | |
// -------------------------------------------------------------------------------------- | |
// Overload 3: Complex Separated | |
const Rectangle = Alge.record('Rectangle') | |
.schema({ | |
width: Length, | |
height: Length, | |
}) | |
.codec('graphic', { | |
to: (rectangle) => `${rectangle.width}x${rectangle.height}`, | |
from: (graphic) => { | |
const match = graphic.match(`(\d+)x(\d+)`) | |
if (!match) return null | |
const [_, width, height] = match | |
return { | |
width: Number(width), | |
height: Number(height), | |
} | |
}, | |
}) | |
.done() | |
const Circle = Alge.record('Circle') | |
.schema({ | |
radius: Length, | |
}) | |
.codec('graphic', { | |
to: (circle) => `(---|${circle.radius})`, | |
from: (graphic) => { | |
const match = graphic.match(`\(---|(\d+)\)`) | |
if (!match) return null | |
const [_, radius] = match | |
return { | |
radius: Number(radius), | |
} | |
}, | |
}) | |
.done() | |
const Square = Alge.record('Square') | |
.schema({ | |
size: Length, | |
}) | |
.codec('graphic', { | |
to: (square) => `[${square}]`, | |
from: (graphic) => { | |
const match = graphic.match(`[(\d+)]`) | |
if (!match) return null | |
const [_, size] = match | |
return { | |
size: Number(size), | |
} | |
}, | |
}) | |
.done() | |
const Shape = Alge.union('Shape', { | |
Rectangle, | |
Circle, | |
Square, | |
}) | |
//or also? | |
//prettier-ignore | |
const Shape = Alge.union('Shape'). | |
.record(Rectangle) | |
.record(Circle) | |
.record(Square) | |
.done() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Overload 1: Simple Centralized | |
const Shape = Alge.union(`Shape`, { | |
Rectangle: { | |
width: Length, | |
height: Length, | |
}, | |
Circle: { | |
radius: Length, | |
}, | |
Square: { | |
size: Length, | |
}, | |
}) | |
//-------------------------------------------------------------------------------------- | |
// Overload 2: Simple Separated | |
const Rectangle = Alge.record(`Rectangle`, { | |
width: Length, | |
height: Length, | |
}) | |
const Circle = Alge.record(`Circle`, { | |
radius: Length, | |
}) | |
const Square = Alge.record(`Square`, { | |
size: Length, | |
}) | |
const Shape = Alge.union(`Shape`, { | |
Rectangle, | |
Circle, | |
Square, | |
}) | |
//-------------------------------------------------------------------------------------- | |
// Overload 3: Complex Centralized | |
const Shape = Alge.union(`Shape`, { | |
Rectangle: { | |
schema: { | |
width: Length, | |
height: Length, | |
}, | |
codecs: [ | |
{ | |
name: 'graphic', | |
to: (rectangle) => `${rectangle.width}x${rectangle.height}`, | |
from: (graphic) => { | |
const match = graphic.match(`(\d+)x(\d+)`) | |
if (!match) return null | |
const [_, width, height] = match | |
return { | |
width: Number(width), | |
height: Number(height), | |
} | |
}, | |
}, | |
], | |
}, | |
Circle: { | |
schema: { | |
radius: Length, | |
}, | |
codecs: [ | |
{ | |
name: 'graphic', | |
to: (circle) => `(---|${circle.radius})`, | |
from: (graphic) => { | |
const match = graphic.match(`\(---|(\d+)\)`) | |
if (!match) return null | |
const [_, radius] = match | |
return { | |
radius: Number(radius), | |
} | |
}, | |
}, | |
], | |
}, | |
Square: { | |
schema: { | |
size: Length, | |
}, | |
codecs: [ | |
{ | |
name: 'graphic', | |
to: (square) => `[${square}]`, | |
from: (graphic) => { | |
const match = graphic.match(`[(\d+)]`) | |
if (!match) return null | |
const [_, size] = match | |
return { | |
size: Number(size), | |
} | |
}, | |
}, | |
], | |
}, | |
}) | |
//-------------------------------------------------------------------------------------- | |
// Overload 3: Complex Separated | |
const Rectangle = Alge.record(`Rectangle`, { | |
schema: { | |
width: Length, | |
height: Length, | |
}, | |
codecs: [ | |
{ | |
name: 'graphic', | |
to: (rectangle) => `${rectangle.width}x${rectangle.height}`, | |
from: (graphic) => { | |
const match = graphic.match(`(\d+)x(\d+)`) | |
if (!match) return null | |
const [_, width, height] = match | |
return { | |
width: Number(width), | |
height: Number(height), | |
} | |
}, | |
}, | |
], | |
}) | |
const Circle = Alge.record(`Circle`, { | |
schema: { | |
radius: Length, | |
}, | |
codecs: [ | |
{ | |
name: 'graphic', | |
to: (circle) => `(---|${circle.radius})`, | |
from: (graphic) => { | |
const match = graphic.match(`\(---|(\d+)\)`) | |
if (!match) return null | |
const [_, radius] = match | |
return { | |
radius: Number(radius), | |
} | |
}, | |
}, | |
], | |
}) | |
const Square = Alge.record(`Square`, { | |
schema: { | |
size: Length, | |
}, | |
codecs: [ | |
{ | |
name: 'graphic', | |
to: (square) => `[${square}]`, | |
from: (graphic) => { | |
const match = graphic.match(`[(\d+)]`) | |
if (!match) return null | |
const [_, size] = match | |
return { | |
size: Number(size), | |
} | |
}, | |
}, | |
], | |
}) | |
const Shape = Alge.union(`Shape`, { | |
Rectangle, | |
Circle, | |
Square, | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Overload 1: Simple Centralized | |
const Shape = Alge.union(`Shape`, { | |
Rectangle: { | |
width: Length, | |
height: Length, | |
}, | |
Circle: { | |
radius: Length, | |
}, | |
Square: { | |
size: Length, | |
}, | |
}) | |
// -------------------------------------------------------------------------------------- | |
// Overload 2: Simple Separated | |
const Rectangle = Alge.record(`Rectangle`, { | |
width: Length, | |
height: Length, | |
}) | |
const Circle = Alge.record(`Circle`, { | |
radius: Length, | |
}) | |
const Square = Alge.record(`Square`, { | |
size: Length, | |
}) | |
const Shape = Alge.union(`Shape`, { | |
Rectangle, | |
Circle, | |
Square, | |
}) | |
// -------------------------------------------------------------------------------------- | |
// Overload 3: Complex Centralized | |
const Shape = Alge.union(`Shape`, { | |
Rectangle: (def) => | |
def | |
.schema({ | |
width: Length, | |
height: Length, | |
}) | |
.codec('graphic', { | |
to: (rectangle) => `${rectangle.width}x${rectangle.height}`, | |
from: (graphic) => { | |
const match = graphic.match(`(\d+)x(\d+)`) | |
if (!match) return null | |
const [_, width, height] = match | |
return { | |
width: Number(width), | |
height: Number(height), | |
} | |
}, | |
}), | |
Circle: (def) => | |
def | |
.schema({ | |
radius: Length, | |
}) | |
.codec('graphic', { | |
to: (circle) => `(---|${circle.radius})`, | |
from: (graphic) => { | |
const match = graphic.match(`\(---|(\d+)\)`) | |
if (!match) return null | |
const [_, radius] = match | |
return { | |
radius: Number(radius), | |
} | |
}, | |
}), | |
Square: (def) => | |
def | |
.schema({ | |
size: Length, | |
}) | |
.codec('graphic', { | |
to: (square) => `[${square}]`, | |
from: (graphic) => { | |
const match = graphic.match(`[(\d+)]`) | |
if (!match) return null | |
const [_, size] = match | |
return { | |
size: Number(size), | |
} | |
}, | |
}), | |
}) | |
// -------------------------------------------------------------------------------------- | |
// Overload 4: Complex Separated | |
const Rectangle = Alge.record('Rectangle', (def) => | |
def | |
.schema({ | |
width: Length, | |
height: Length, | |
}) | |
.codec('graphic', (def) => | |
def | |
.to((rectangle) => `${rectangle.width}x${rectangle.height}`) | |
.from((graphic) => { | |
const match = graphic.match(`(\d+)x(\d+)`) | |
if (!match) return null | |
const [_, width, height] = match | |
return { | |
width: Number(width), | |
height: Number(height), | |
} | |
}) | |
) | |
) | |
const Circle = Alge.record('Circle', (def) => | |
def | |
.schema({ | |
radius: Length, | |
}) | |
.codec('graphic', (def) => | |
// chaining api consistency? | |
def | |
.to((circle) => `(---|${circle.radius})`) | |
.from((graphic) => { | |
const match = graphic.match(`\(---|(\d+)\)`) | |
if (!match) return null | |
const [_, radius] = match | |
return { | |
radius: Number(radius), | |
} | |
}) | |
) | |
) | |
const Square = Alge.record('Square', (def) => | |
def | |
.schema({ | |
size: Length, | |
}) | |
.codec('graphic', { | |
to: (square) => `[${square}]`, | |
from: (graphic) => { | |
const match = graphic.match(`[(\d+)]`) | |
if (!match) return null | |
const [_, size] = match | |
return { | |
size: Number(size), | |
} | |
}, | |
}) | |
) | |
const Shape = Alge.union(`Shape`, { | |
Rectangle, | |
Circle, | |
Square, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment