stags
A simple library for complex logic.
- 🐭 4KB Gzipped!!!
- 💾 Serializable (perfect for localStorage)
- 🎁 Powerful OOTB (Functor, Monad, Bifunctor and more!)
- 🛠 Extensible ( Open specification, data first ✊)
- 🚀 Makes UI and API code safer, cleaner and more fun.
Quick Start
npm install stags@next
import { either } from "stags";
const Loaded =
either("Loaded")
const loaded =
Loaded.Y("Hello World")
const loading =
Loaded.N(55)
const render =
Loaded.bifold(
x => `Loading: ${x}%`
, x => `Loaded: ${x}`
)
const transform =
Loaded.map(
x => x.toUpperCase()
)
render( transform( loaded ) )
render( transform( loading ) )
transform( render( loading ) )
What is it
Freedom from booleans.
Scenario: You've solving a moderately difficult problem, and there's a degree of data modelling involved. You've got several booleans for tracking loading states, save states, modified states, selected states and on and on.
Oh! and you're tracking all those states for every item in a list separately.
Depending on a specific combination of these boolean flags you need to render differently, talk to the server differently, persist state differently.
It very quicky becomes a mess. We reach for complex tools to help us manage the mess. But instead all we needed was to make it impossible to not traverse every single state.
First step. Create a stags
for every state we want to track.
import * as S from 'stags'
const Data = S.tags('Data',
[ 'Deselected'
, 'Loading'
, 'Modified'
, 'Saved'
]
)
Our type Data
has been generated with 4 tags, each has a constructor:
Data.Saved(data)
Data.Deselected()
Our constructors just tag our data with a label that allows us to build logic
on top of it. Normally this label would be stored separately to the data.
Something like:
var selected = false
var data = ...
Which is fine at first, until it looks like this:
var selected = true
var loaded = true
var modified = true
var saved = false
var data = x
We'd model that in stags
like so:
import * as S from 'stags'
const Data = S.tags('Data',
[ 'Deselected'
, 'Loading'
, 'Modified'
, 'Saved'
]
)
const data = Data.Modified(x)
When we want to transform the value of data
we can use any number of useful helpers. Like map<Tag>
, chain<Tag>
, get<Tag>With
or get<Tag>Or
where <Tag>
is one of your tag names. E.g. mapSaved
.
const f = Data.mapSaved(
x => x * 2
)
f( Data.Saved(2) )
f( Data.Modified(1) )
Data.Modified(1)
const g = Data.getModifiedOr(0)
g( Data.Saved(2) )
g( Data.Modified(3) )
🤓 Types generated by stags are decorated with a lot of functions for free. Check out lib/decorate.js to see how it all works.
You can also fold
all the cases, as every type generated by stags is a discrimated union. Which means (in layperson's terms), you'll get a helpful error if you don't account for every possibly tag.
const NoData = S.otherwise(['Selected', 'Loading'])
const f =
Data.fold({
... NoData( () => 'Nothing' )
, Saved: x => 'Saved: ' + x
, Modified: x => 'Modified: ' + x
})
f( Data.Loading() )
//=> 'Nothing'
f( Data.Saved('cool') )
//=> 'Saved: Cool'
There's loads of other helpful utilities, and stags will guide you with helpful error messages if you make a mistake. That's one of our design goals!
Helpful Errors
If we pass the wrong data structure into our composition, we will get a specific, helpful error message explaining what your type looked like
and what that particular method was expecting.
Your stack trace is going to be particularly legible because stags
internally avoids point free composition and auto currying, and will report errors at the first incorrect invocation.
So even though fold
requires 3 calls:
fold (Type) ({ Y: () => 'hi', N: () => 'bye' }) ( Type.Y() )
stags will error if any one of those invocations didn't satisfy the required constraints.
🤓 Every error that stags
yields, is itself a transformation of
a stags
. All the error types are documented in the Errors section
Specification
stags
differentiates itself from other sum type library by documenting the internal structure used for types and instances of types. This allows you to create your
own constructors/transformers in userland. You can store the exact output
of a stags constructor in a redux-store
, localStorage
or even a json
column in your favourite database.
stags
does not care where your data came from, just that it adheres to a particular structure.
Ecosystem
Each module listed here adheres to the stags specification. That specification is defined at stags/docs/spec.md.
- superouter A Router that both exposes and internally uses stags to model route definitions, validation and more.
Project Goals and Motivations
- Serializable
- 0 Dependencies
- Tiny for frontend usage
- Avoid pitfalls found in other sum type libraries
- Helpful Error Messages designed for makers
How does stags differ from other libraries in the ecosystem?
stags
removes the following features because we believe they lead to brittle codebases.
- placeholder cases
- replaced by
otherwise
which is slightly more verbose but way safer and more powerful
- auto spreading of values in cata/fold
- stags just have 1 value, and that's what get's passed in to a fold
- use a list, or an object to have a stag carry more values
- auto curried constructors
- Functions are manually curried and report errors per invocation
- prototypes (reference equality checks / instanceof)
- Makes it hard to share type checkings for data across serialization boundaries and realms (e.g. Electron apps)
stags
is technically 0KB, it's an idea. You can use stags in your codebase without ever running npm install
. But this library is only 4kb gzipped, so even the non-idea part is pretty small.
API
These docs are a bit stale as stags goes through some API churn.
either
import { either } from 'stags'
const Loaded =
either('Loaded')
either::Y
a -> Either Y a | N b
either::N
b -> Either Y a | N b
either::map
( a -> c ) -> Either Y c | N b
either::bimap
(( a -> c ), ( b -> d )) -> Either Y c | N d
either::bifold
(( a -> c ), ( b -> c )) -> Either Y a | N b -> c
either::getWith
( c , ( b -> c )) -> Either Y a | N b -> c
either::getOr
c -> Either Y a | N b -> c
either::fold
Type -> { Y: a -> c, N: b -> c } -> tag -> c
either::chain
( a -> Either Y c | N b ) -> Either Y c | N b
toBoolean
Either Y a | N -> boolean
⚠ You should almost always avoid coercing a sum type to a boolean. If you are checking for Y
, try .map
. If you are checking for N
try getOr
. Booleans have no context, no associated data, but there's almost always associated data in your model so toBoolean
is much like moving from a lossless format to a lossy format.
either::encase
( a -> b ) -> Either Y b | N Error
Takes a potentially unsafe function and decorates it to return an Either where non thrown values are encased in Either.Y
and thrown values are encased in Either.N
.
maybe
import { maybe } from 'stags'
const Selected =
maybe('Selected')
maybe::Y
a -> Maybe Y a | N
maybe::N
() -> Maybe Y a | N
maybe::map
( a -> c ) -> Maybe Y c | N
maybe::bimap
(( () -> b ), ( a -> b )) -> Maybe Y b | N
maybe::bifold
(( () -> b ), ( a -> b )) -> Maybe Y a | N -> b
maybe::getWith
( b , ( a -> b )) -> Maybe Y a | N -> b
maybe::getOr
b -> Maybe Y a | N -> b
maybe::fold
Type -> { Y: a -> b, N: () -> b } -> tag -> b
maybe::chain
( a -> Maybe Y b | N ) -> Maybe Y b | N
toBoolean
Maybe Y a | N -> boolean
⚠ You should almost always avoid coercing a sum type to a boolean. If you are checking for Y
, try .map
. If you are checking for N
try getOr
. Booleans have no context, no associated data, but there's almost always associated data in your model so toBoolean
is much like moving from a lossless format to a lossy format.
maybe::encase
( a -> b ) -> Maybe Y b | N Error
Takes a potentially unsafe function and decorates it to return an Maybe where non thrown values are encased in Maybe.Y
and thrown values are represented as Maybe.N()
. If the specific error value is relevant try Either.encase
instead as it will return your error object in an Either.N
structure.
Canonical Either
In the future some functions will return optional values. This library encourages you to define your own but this library exports a pregenerated Either type that can be used canonically as the "real" Either which can be helpful when doing natural transformations and conversions between types and safe and unsafe data.
import { Y, N, getOr } from stags
const yes = Y(100)
const no = N()
const f = getOr(0)
f(yes)
f(no)
tags
import { tags } from 'stags'
const Geom =
tags ('Geom') (
['Point'
,'Line'
,'Poly'
]
)
const p1 = Geom.Point({ x:0, y: 0 })
const p2 = p1
const line = Geom.Line([p1, p2])
const poly = Geom.Poly([p1, p2, [p3]])
fold
Type -> { [tag]: a -> b } -> tag -> b
mapAll
Type -> { [tag]: a -> b } -> tag -> Type b
⚠ Both map
and chain
will skip executing tags when their instance has no .value
property (usually determined by their type constructor).
chainAll
Type -> { [tag]: a -> Type b } -> tag -> Type b
⚠ Both map
and chain
will skip executing tags when their instance has no .value
property (usually determined by their type constructor).
But when using map
and chain
you are still required to pass in a handler for every tag.
It's recommended to use otherwise
with map
and chain
to prefill values that are not relevant to the fold.
otherwise
string[] -> f -> { [key:string]: f }
A helper function for generating folds that are versioned separately to the type definition. It's useful when you want to avoid specifying each clause in a fold without losing type safety or introducing other modelling problems
Read more about otherwise
here
const { Y, N } = stags.Maybe
const Platform = stags.tags ('Platform') (
['ModernWindows'
,'XP'
,'Linux'
,'Darwin'
]
)
const rest = stags.otherwise([
'ModernWindows',
'XP',
'WSL',
'Linux',
'Darwin',
])
const windowsGUI = stags.otherwise([
'ModernWindows',
'XP',
])
const foldWindowsGUI = f => stags.mapAll(Platform) ({
... rest2(N),
... windowsGUI( () => Y(f()) )
})
const winPing =
foldWindowsGUI
( () => 'ping \\t www.google.com' )
winPing( Platform.Darwin() )
winPing( Platform.XP() )
Experimental
These functions are likely to change at any moment.
tagName
tagName
-> tagName:string
Extract the name of a tag from an instance of a type.
getTags
T
-> (tag:string)[]
Returns a list of the tag
's for a given type T
.
Errors
Below is the source code definition for the internal errors this library throws.
const StaticSumTypeError =
tags('StaticSumTypeError', {
, 'ExtraTags'
, 'MissingTags'
, 'InstanceNull'
, 'InstanceWrongType'
, 'InstanceShapeInvalid'
, 'tag'
, 'VisitorNotAFunction'
, 'NotAType'
})
Error | Throws ... |
---|
ExtraTags | when a fold specifies a visitor for tags that are not of the type. |
MissingTags | when a fold does not specify a visitor for each tag of the type. |
InstanceNull | when an argument was expected to be an instance of a sum type but was instead null. |
InstanceWrongType | when an instance is a valid stags but not the specifically expected type for that function. |
InstanceShapeInvalid | when an instance has the correct type property but an unknown tag property. |
VisitorNotAFunction | when a function was expected a visitor function but received anything else. |
NotAType | when a function expected a stags type but received anything else. |