alge
[![trunk](https://github.com/jasonkuhrt/alge/actions/workflows/trunk.yml/badge.svg)](https://github.com/jasonkuhrt/alge/actions/workflows/trunk.yml) [![npm version](https://img.shields.io/npm/v/alge.svg)](https://www.npmjs.com/package/alge)
Last updated 4 years ago by jasonkuhrt .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install alge 
SYNC missed versions from official npm registry.

Alge 🌱

trunk npm version

TL;DR

Type safe library for creating Algebraic Data Types (ADTs) in TypeScript. Pronounced "AL GEE" like the plant (or whatever it is). Schemas powered by Zod <3.

There are three distinct conceptual levels in Alge. Firstly there is a builder for defining your ADT in the first place. Secondly there is a controller for working with your defined ADT such as construction, type guards, and codecs. Finally there are the actual pure-data instances of your ADT.

Alge Anatomy

Here's how it looks like in code. First the Builder which outputs a Controller.

import { Alge } from '../src/index.js'
import { z } from 'zod'

const Length = z.number().positive()

//           v---------- 2. ADT Controller
//           |            v--------- 1. ADT Builder
export const Shape = Alge.data(`Shape`, {
  Rectangle: {
    width: Length,
    height: Length,
  },
  Circle: {
    radius: Length,
  },
  Rectangle: {
    size: Length,
  },
})

Now the Controller:

//    v--------- 3. Instance
//    |        v--------- 2. ADT Controller
const circle = Shape.Circle.create({ radius: 50 })
// { _tag: 'Circle', radius: 50 }

if (Shape.Circle.is(circle)) {
  console.log(`I Am Circle`)
}

const circleForTheOutsideWorld = Shape.Circle.to.json(circle)
// '{ "_tag": "Circle", "radius": 50 }'

const squareFromTheOutsideWorld = Shape.Square.from.json({ _tag: 'Square', size: 10 })
// { _tag: 'Square', size: 10 }

Finally are the instances which you can see above are created by the controller. Instances are just data. They are not like class instances that couple logic and data. All logic related to an ADT, like type guards, resides in the controller.

If you don't need a full blown ADT but just one record, Alge can do that:

//    v---------- 2. Record Controller
//    |             v--------- 1. Record Builder
const Circle = Alge.record(`Circle`, { radius: Length })

If you want to compose your ADT incrementally Alge can do that:

//    v---------- 2. Record Controller
//    |             v--------- 1. Record Builder
const Circle = Alge.record(`Circle`, { radius: Length })
const Square = Alge.record(`Square`, { size: Length })
//    v---------- 2. ADT Controller
//    |            v--------- 1. ADT Builder
const Shape = Alge.data(`Shape`, {
  Circle,
  Square,
})

This is just a taster. Places you can go next:

  1. Install and learn interactively (JSDoc is coming soon!)

  2. A formal features breakdown

  3. Code examples

  4. A simple introduction to Algebraic Data Types (for those unfamiliar)

  5. A admittedly rambling 😃 video introduction if you like that format

    Video Cover

Contents

Installation

npm add alge

Roadmap

There is no timeline but there are priorities. Refer to the currently three pinned issues.

About Algebraic Data Types

Alge is a Type Script library for creating Algebraic Data Types (ADTs). This guide will take you from not knowing what ADTs are to why you might want to use Alge for them in your code.

What?

Algebraic Data Types (ADTs for short) are a methodology of modelling data. They could appear in any context that is about defining and/or navigating the shape of data. One of their fundamental benefits is that they can express different states/inrecords/facts about/of data. They are the combination of two other concepts, product types and union types.

A product type is like:

interface Foo {
  bar: string
  qux: number
}

A union type is like:

type Foo = 1 | 2 | 3

Basically, when the power of these two data modelling techniques are combined, we get something far greater than the sum of its parts: ADTs.

ADTs can particularly shine at build time. While dynamically typed programing languages ("scripting language", e.g. Ruby, JavaScript, Python, ...) can support ADTs at runtime, adding static type support into the mix increases the ADT value proposition. Then there are yet other more minor programing language features like pattern matching that if supporting ADTs make them feel that much more beneficial too.

References:

Why?

Now that we have some understanding of what ADTs are let's build some understanding about why we might want to use them. To do this we'll work with an example.

Let's say we want to accept some user input about an npm package dependency version pin. It might come in the form of an exact version or a range of acceptable versions. How would we model this? Let's start without ADTs and then refactor with them to appreciate the difference. Let's assume that input parsing has been taken care of and so here we're only concerned with structured data modelling.

interface Pin {
  isExact: boolean
  patch?: number
  minor?: number
  major?: number
  release?: string
  build?: string
  range?: Array<{
    operator: `~` | `>=` | `...` // etc.
    patch: number
    minor: number
    major: number
    release?: string
    build?: string
  }>
}

This data modelling is flawed. There is out-of-band information about important data relationships. release and build are legitimately optional properties but range patch minor major all depend on the state of isExact. When true then range is undefined and the others are not, and vice-versa. In other words these configurations of the data are impossible:

const pin = {
  isExact: true,
  patch: 1,
  minor: 2,
  major: 3,
  range: [
    {
      operator: `~`,
      patch: 1,
      minor: 0,
      major: 0,
    },
  ],
}
const pin = {
  isExact: false,
  patch: 1,
  minor: 2,
  major: 3,
}

While these are possible:

const pin = {
  isExact: true,
  patch: 1,
  minor: 2,
  major: 3,
}
const pin = {
  isExact: true,
  patch: 1,
  minor: 2,
  major: 3,
  release: `beta`,
}
const pin = {
  isExact: false,
  range: [
    {
      operator: `~`,
      patch: 1,
      minor: 0,
      major: 0,
    },
  ],
}

But since our data modelling doesn't encode these facts our code suffers. For example:

if (pin.isExact) {
  doSomething(pin.major!)
  //                       ^
}

Notice the !. Its us telling Type Script that major is definitely not undefined and so the type error can be ignored. In JS its even worse, as we wouldn't even be prompted to think about such cases, unless we remember to. Seems trivial in this case, but at scale day after day often with unfamiliar code a mistake will inevitably be made. Another approach could have been this:

if (pin.isExact) {
  if (!pin.major) throw new Error(`Bad pin data!`)
  doSomething(pin.major)
}

So, poor data modelling affects the quality of our code by our code either needing to deal with apparently possible states that are actually impossible OR by our code carefully ignoring those impossible states. Both solutions are terrible because they make code harder to read. There is more code, and the chance that wires about impossible and possible states will cross becomes a real possibility leading to potential runtime errors.

ADTs solve this. Let's refactor our Pin type into an ADT to see how!

type Pin = ExactPin | RangePin

interface ExactPin {
  tag: `ExactPin`
  patch: number
  minor: number
  major: number
  release?: string
  build?: string
}

interface RangePin {
  tag: `RangePin`
  values: Array<{
    operator: `~` | `>=` | `...` // etc.
    isExact: boolean
    patch: number
    minor: number
    major: number
    release?: string
    build?: string
  }>
}

Now we've encoded the possible states we cared about. Our code quality increases:

if (pin.tag === 'ExactPin') {
  doSomething(pin.major) // No problem, `pin` has been narrowed from `Pin` to `ExactPin`!
}

When a developer deals with values of Pin type they will have an immediately much better understanding of the possible states.

In fact every optional property in some data represents possibly different state representations and thus potentially a use case for an ADT. So for example we could go further with our above data modelling and define things like ExactPreReleasePin and ExactPreReleaseBuildPin:

interface ExactPreReleasePin {
  tag: `ExactPreReleasePin`
  patch: number
  minor: number
  major: number
  release: string
}
interface ExactPreReleaseBuildPin {
  tag: `ExactPreReleasePin`
  patch: number
  minor: number
  major: number
  release: string
  build: string
}

Of course like any technique there is a point where ADT modelling is probably overkill for your use-case. That said, that line might be further out than you think. For example while the above might seem excessive, it actually answers a question the previous data modelling left ambiguous which is the question of, is the following state possible?

const pin = {
  isExact: true,
  patch: 1,
  minor: 2,
  major: 3,
  build: `5`,
}

The answer is no! But without the ADT that fact would have to managed by humans, rather than the machine.

At scale, having well modelled data can be a life saver. The up front verbosity pays dividends downstream for all the impossible branches removed from programs' possibility space. ADTs help you (or your consumers) focus on what can actually happen.



Features

All code blocks below assume these imports:

import { Alge } from 'alge'
import { z } from 'zod'

Records

Definition (.record)

Use the Record Builder to define a record. At a minimum you specify the name and schema. Names should be in pascal case to avoid name collisions with the Alge API (see below).

const Circle = Alge.record('Circle', {
  radius: z.number().positive(),
})

If you already have a Zod Object defined from somewhere else you can just pass it in:

const Circle = Alge.record('Circle', CircleSchema)

Construction (.create)

Once you've defined a record with the Record Builder you get back a Record Controller. Use it to create instances of your record:

const circle = Circle.create({ radius: 10 })
// { _tag: 'circle', radius: 10 }

The _tag property is present to track the name of your record. you normally shouldn't have to interact directly with it.

Input Defaults

Leverage Zod to get defaults for your properties (zod docs):

const Circle = Alge.record('Circle', {
  radius: z.number().positive().default(0),
})

const circle = Circle.create()
// { _tag: 'circle', radius: 0 }

Input Validation

Input is validated via Zod. For example a negative number where only positives are accepted.

const circle = circle.create({ radius: -10 })
// throws

Metadata

The controller gives you access to metadata about your record:

circle.name // 'Circle'
circle.schema // a Zod schema instance

Chaining API

There is a chaining API available which is more verbose but also affords more features (see further down).

const Circle = Alge.record('Circle')
  .schema({
    radius: z.number().positive(),
  })
  .done()

Like in the shorthand form, if you already have a Zod Object defined from somewhere else you can just pass it in:

const Circle = Alge.record('Circle').schema(CircleSchema).done()

Codecs

Definition (.codec)

You can define a named codec which allows your record to be encoded to/decoded from another representation.

The encoder (to) transforms your record into a string.

The decoder (from) transforms a string into your record, or null if the string is invalid.

const Circle = Alge.record('Circle')
  .schema({
    radius: z.number().positive().default(1),
  })
  .codec('graphic', {
    //    ^[1]
    to: (circle) => `(${circle.radius})`,
    from (string) => {
      const match = string.match(^/\((\d+)\)/$)
      return match ? { radius: radius[1]! } : null
    //               ^[2]
    }
  })
  .done()

Notes:

  1. We give our codec a name. This name is used for the derived API (see "usage" below).
  2. When returning the parsed data for our record we do not need to deal with the _tag property.

Usage (.to., .from)

Codecs are exposed under the .from and .to (decoders/encoders) properties:

const circle = Circle.create()
// { _tag: 'Circle', radius: 1 }

Circle.to.graphic(circle)
// (1)

Circle.from.graphic(`(1)`)
// { _tag: 'Circle', radius: 1 }

Circle.from.graphic(`()`)
// null

Built In JSON

All records have a JSON codec:

Circle.to.json(circle)
// '{ "_tag": "Circle", "radius": 1 }'

Circle.from.json('{ "_tag": "Circle", "radius": 1 }')
// { _tag: 'Circle', radius: 1 }

OrThrow Decoders

All decoders, JSON or your custom ones, have a variant of decode that will throw an Error when decoding fails:

Circle.from.graphicOrThrow(`()`)
// throws

Circle.from.jsonOrThrow(`bad`)
// throws

Data (Algebraic Data Types)

The ADT Builder is an extension of the Record Builder and the ADT Controller it returns is an extension of the Record Controller.

Definition

Referencing Records

Records can be passes into the Data Builder:

const Circle = Alge.record('Circle', { radius: z.number() })
const Square = Alge.record('Square', { size: z.number() })

const Shape = Alge.data('Shape', { Circle, Square })

Inline Records

Records can also be defined inline:

const Shape = Alge.data('Shape', {
  Circle: { radius: z.number() },
  Square: { size: z.number() },
})

Construction

The ADT Controller contains one Record Controller for every record defined under a property of that records name. Use it just like you did before:

const circle = Shape.Circle.create({ radius: 1 })
// { _tag: 'Circle', radius: 1 }
const square = Shape.Square.create({ size: 2 })
// { _tag: 'Square', size: 2 }

Chaining API

As with records before there is a chaining API for ADTs that is more verbose but has additional features.

const Shape = Alge.data('shape').record(Circle).record(Square).done()

Identity (.is, .is$)

Use the .is Record Controller method as a TypeScript type guard. It checks if the given value is that record or not:

const onlyCircle = (shape: Shape): null | Shape.Circle => {
  return Shape.Circle.is(shape) ? shape : null
}

When you're working with unknown values there is the .$is method which takes unknown input. It is less type safe than .is so avoid .is$ when you can:

const onlyCircle = (someValue: unknown): null | Shape.Circle => {
  return Shape.Circle.$is(someValue) ? someValue : null
}

Codecs

When a codec of some name is defined for every record in an ADT then something special happens. The ADT gets access to a generalized version of the codec with these features:

  1. A generalized decoder that will take a string and return a record instance of the first record decoder to return non-null. The static type is a union of all the records in the ADT (plus null).
  2. A generalized encoder that will dispatch automatically to the correct Record encoder based on the passed in record's _tag value.

Definition (.codec)

Here is an example of defining a custom codec for each record in an ADT.

const circlePattern = /^\(( *)\)$/
const squarePattern = /^\[( *)\]$/

const shape = Alge.data('Shape')
  .record(`Circle`)
  .schema({
    radius: z.number(),
  })
  .codec('graphic', {
    to: (circle) => `(${' '.repeat(circle.radius)})`,
    from: (string) => {
      const match = string.exec(circleString)
      return match ? { radius: match[1]!.length } : null
    },
  })
  .record(`square`)
  .schema({
    size: z.number(),
  })
  .codec('graphic', {
    to: (square) => `[${' '.repeat(square.size)}]`,
    from: (string) => {
      const match = squarePattern.exec(string)
      return match ? { size: match[1]!.length } : null
    },
  })
  .done()

Usage (to, from)

const circle = Shape.Circle.create({ radius: 3 })
// { _tag: 'circle', radius: 3 }
const circleString = Shape.Circle.to.graphic(circle)
// '(   )'
const circle2 = Shape.Circle.from.graphic(circleString)
// { _tag: 'circle', radius: 3 }
const circle3 = Shape.Circle.from.graphic('(]')
// null
const shape1 = shape.from.graphic('()')
// type: circle | square | null
// value: { _tag: 'circle', radius: 0 }
const shape2 = Shape.from.graphic('[]')
// type: circle | square | null
// value: { _tag: 'square', size: 0 }
const shape3 = Shape.from.graphic('!')
// type: circle | square | null
// value: null
const shape4 = Shape.from.graphicOrThrow('!')
// type: circle | square
// value: throws

As mentioned all records have a JSON codec, thus all ADTs have a generalized one.

const circleJson = Shape.to.json(circle)
// '{ "_tag": "circle", "radius": 50 }'
const circle2 = shape.from.json(circleJson)
// { "_tag": "circle", "radius": 50 }

Static Types

Often you will write code (e.g. your own functions) that need to be typed with your adt. alge has "type functions" for this which leverages typescript inference.

For adts there is alge.Infer. it return an object with a property per record of the adt as well as a special property * which is a union of all records.

type Shape = Alge.Infer<typeof shape>
/*
{
  Circle: { _tag: 'Circle', radius: number }
  Square: { _tag: 'Square', size: number }
  '*':    | { _tag: 'Circle', radius: number }
          | { _tag: 'Square', size: number }
}
*/

const doSomething = (shape: Shape['*']): null | Shape['circle'] => {
  // todo
}

For lone records there is alge.InferRecord.

type Circle = Alge.InferRecord<typeof Circle>

const doSomething = (circle: Circle) => {
  // todo
}

Namespaces

When working with inferred adt types, if you prefer to work with namespaces rather than objects to reference types you can use the following pattern:

type ShapeInferred = Alge.Infer<typeof Shape>

type Shape = ShapeInferred['*']

namespace Shape {
  export type Circle = ShapeInferred['Circle']
  export type Square = ShapeInferred['Square']
}

const doSomething = (shape: Shape): null | Shape.Circle => {
  // todo
}



alt

Current Tags

  • 0.8.1                                ...           latest (4 years ago)
  • 0.8.1                                ...           next (4 years ago)

71 Versions

  • 0.8.1                                ...           4 years ago
  • 0.8.1-next.1                                ...           4 years ago
  • 0.8.0                                ...           4 years ago
  • 0.8.0-next.1                                ...           4 years ago
  • 0.7.0                                ...           4 years ago
  • 0.7.0-next.4                                ...           4 years ago
  • 0.7.0-next.3                                ...           4 years ago
  • 0.7.0-next.2                                ...           4 years ago
  • 0.7.0-next.1                                ...           4 years ago
  • 0.6.1                                ...           4 years ago
  • 0.6.1-next.1                                ...           4 years ago
  • 0.6.0                                ...           4 years ago
  • 0.6.0-next.1                                ...           4 years ago
  • 0.5.1                                ...           4 years ago
  • 0.5.1-next.4                                ...           4 years ago
  • 0.5.1-next.3                                ...           4 years ago
  • 0.5.1-next.2                                ...           4 years ago
  • 0.5.1-next.1                                ...           4 years ago
  • 0.5.0                                ...           4 years ago
  • 0.5.0-next.1                                ...           4 years ago
  • 0.4.3                                ...           4 years ago
  • 0.4.3-next.1                                ...           4 years ago
  • 0.4.2                                ...           4 years ago
  • 0.4.2-next.2                                ...           4 years ago
  • 0.4.2-next.1                                ...           4 years ago
  • 0.4.1                                ...           4 years ago
  • 0.4.1-next.1                                ...           4 years ago
  • 0.4.0                                ...           4 years ago
  • 0.4.0-next.2                                ...           4 years ago
  • 0.4.0-next.1                                ...           4 years ago
  • 0.3.0                                ...           4 years ago
  • 0.3.0-next.1                                ...           4 years ago
  • 0.2.0                                ...           4 years ago
  • 0.2.0-next.6                                ...           4 years ago
  • 0.2.0-next.5                                ...           4 years ago
  • 0.2.0-next.4                                ...           4 years ago
  • 0.2.0-next.3                                ...           4 years ago
  • 0.2.0-next.2                                ...           4 years ago
  • 0.2.0-next.1                                ...           4 years ago
  • 0.1.1                                ...           4 years ago
  • 0.1.1-next.1                                ...           4 years ago
  • 0.1.0                                ...           4 years ago
  • 0.1.0-next.28                                ...           4 years ago
  • 0.1.0-next.27                                ...           5 years ago
  • 0.1.0-next.26                                ...           5 years ago
  • 0.1.0-next.25                                ...           5 years ago
  • 0.1.0-next.24                                ...           5 years ago
  • 0.1.0-next.23                                ...           5 years ago
  • 0.1.0-next.22                                ...           5 years ago
  • 0.1.0-next.21                                ...           5 years ago
  • 0.1.0-next.20                                ...           5 years ago
  • 0.1.0-next.19                                ...           5 years ago
  • 0.1.0-next.18                                ...           5 years ago
  • 0.1.0-next.17                                ...           5 years ago
  • 0.1.0-next.16                                ...           5 years ago
  • 0.1.0-next.15                                ...           5 years ago
  • 0.1.0-next.14                                ...           5 years ago
  • 0.1.0-next.13                                ...           5 years ago
  • 0.1.0-next.12                                ...           5 years ago
  • 0.1.0-next.11                                ...           5 years ago
  • 0.1.0-next.10                                ...           5 years ago
  • 0.1.0-next.9                                ...           5 years ago
  • 0.1.0-next.8                                ...           5 years ago
  • 0.1.0-next.7                                ...           5 years ago
  • 0.1.0-next.6                                ...           5 years ago
  • 0.1.0-next.5                                ...           5 years ago
  • 0.1.0-next.4                                ...           5 years ago
  • 0.1.0-next.3                                ...           5 years ago
  • 0.1.0-next.2                                ...           5 years ago
  • 0.1.0-next.1                                ...           5 years ago
  • 0.0.1-next.1                                ...           5 years ago
Maintainers (1)
Downloads
Total 159
Today 0
This Week 0
This Month 109
Last Day 4
Last Week 11
Last Month 0
Dependencies (3)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE