npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tsum

v0.3.0

Published

Typescript sum types with pattern matching, using multimethods

Downloads

18

Readme

tsum

Typescript sum types with pattern matching, using multimethods

Install

npm install --save tsum

yarn add tsum

Motivation

Being able to simply represent, manipulate, and pattern match on immutable values is one of the most important things a programming language can provide.

Typescript offers two built-in solutions:

  1. Add a 'type' field to all your data, and switch on it. This is verbose, requires explicit serialize/deserialize, and error prone when specifying literal data. Additionally, switch offers no exhaustiveness guarantee, and is a statement rather than an expression.
  2. Put your data into classes. This is verbose, requires explicit serialize/deserialize and lacks named fields:
    new Person('Garrett') vs {name: 'Garrett'}
    Most importantly, you lose the ability to easily make new immutable values from previous values.

Using plain data structures solves many of these problems, and improves notation:

  • Values are trivial to persist or send over a wire, and trivial to read when receiving data from the wire or disk
  • Data definitions are as terse and straightforward as possible in Typescript
  • Fields are named
  • Open system, as long as a piece of data matches one of these interfaces, it can participate in this polymorphism (unlike classes)
  • Matching every possible case is enforced by the compiler, and matches are expressions which return a value
  • Critically, you gain the ability to use spread notation to flexibly produce new values:
    const olderPerson = {...person, age: person.age + 1}
    Classes offer no equivalent to this

Example

import Sum from 'tsum'

Describe each of your types:

interface Dog {
  name: string
  color: string
}

interface Cat {
  name: string 
  dogFriendly: boolean
}

interface Chicken {
  eggsLaid: number
}

interface Cow {
  mooVolume: number
}

Put them together:

interface Animals {
  Dog: Dog
  Cat: Cat
  Chicken: Chicken
  Cow: Cow
}

Provide the Animals interface, along with a set of predicates to determine the type of an arbitrary Animal:

const Animal = Sum<Animals>({
  predicates: ({isString, isBoolean, isNumber}) => ({
    Dog:     _ => isString(_.color),
    Cat:     _ => isBoolean(_.dogFriendly),
    Chicken: _ => isNumber(_.eggsLaid),
    Cow:     _ => isNumber(_.mooVolume)
  })
})

Create functions that act on any Animal:

// The type of describe is: (animal: Animal) => string
const describe = Animal.f({
  // Typescript knows the type in each branch, and makes sure you cover all cases
  Dog: dog => `a ${dog.color} dog named ${dog.name}`,
  Cat: cat => cat.dogFriendly ? `a cat named ${cat.name} (dog-friendly)` : `a cat named ${cat.name} (not dog-friendly)`,
  Chicken: chicken => `a chicken who has laid ${chicken.eggsLaid} eggs`,
  Cow: cow => cow.mooVolume > 5 ? 'a loud cow' : 'a quiet cow'
})

const dog = {name: 'Zorro', color: 'grey'}
const cat = {name: 'Oberyn', dogFriendly: false}
const chicken = {eggsLaid: 13}
const cow = {mooVolume: 6}

console.log(describe(dog))     // a grey dog named Zorro
console.log(describe(cat))     // a cat named Oberyn (not dog-friendly)
console.log(describe(chicken)) // a chicken who has laid 13 eggs
console.log(describe(cow))     // a loud cow

Or match inline:

const cat = {name: 'Oberyn', dogFriendly: false}
const cost = Animal.match(cat, {
  Dog: dog => dog.name === 'Zorro' ? 99999 : 300,
  Cat: cat => cat.name === 'Oberyn' ? 99999 : 800,
  Chicken: chicken => chicken.eggsLaid * 10,
  Cow: cow => cow.mooVolume > 5 ? 5000 : 6000
})
console.log(cost) // 99999

Convenience to avoid repeating yourself:

// Evaluates to Dog | Cat | Chicken | Cow
type Animal = typeof Animal.types

Thanks to:

The Value of Values