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

ctors-union

v0.2.0

Published

[![npm version][version-image]][version-url] [![build][build-image]][build-url] [![Coverage Status][codecov-image]][codecov-url] [![code style: prettier][prettier-image]][prettier-url] [![types][types-image]][types-url] [![MIT license][license-image]][lic

Downloads

684

Readme

ctors-union

npm version build Coverage Status code style: prettier types MIT license

Define unions types via constructor functions

How to install

NOTE: Typescript version >=3.7 is needed becuase of recursive type alias

npm install ctors-union --save

Introduction

To create a union type for use with for example Redux actions you can do this:

type Action = Foo | Bar | Curry;

type Foo = { type: "Foo"; foo: number };
type Bar = { type: "Bar"; bar: string };
type Curry = { type: "Curry"; one: string; two: number };

function Foo(foo: number) {
  return { type: "Foo", foo };
}

function Bar(bar: string) {
  return { type: "Bar", bar };
}

function Curry(one: string) {
  return function (two: number) {
    return { type: "Bar", one, two };
  };
}

When you have many actions this code can take a lot of space. To shorten it you can use this library like this:

import { ctorsUnion, CtorsUnion } from "ctors-union";

const Action = ctorsUnion({
  Foo: (foo: string) => ({ foo }),
  Bar: (bar: number) => ({ bar }),
  Curry: (one: string) => (two: number) => ({ one, two }),
});
type Action = CtorsUnion<typeof Action>;

With this in place you can construct values for each type and discriminate them in a switch statement like this:

export const fooAction = Action.Foo("myfoo");
export const barAction = Action.Bar(1);
export const curriedAction = Action.Curry(1);
export const curry: Action = curriedAction(2);

export function update(action: Action): void {
  switch (action.type) {
    case "Foo": {
      break;
    }
    case "Bar": {
      break;
    }
    case "Curry": {
      break;
    }
    default: {
      const _ignored: never = action;
      break;
    }
  }
}

How it works

The goal of this library is to add as little "magic" as possible but still cut down the bolierplate and support currying in the constructor functions. To acheive this we use one function ctorsUnion and one type CtorsUnion<T>.

ctorsUnion

This function takes an object where each key is a string and the value is a function that contstucts an object. It returns the same object with the constructor functions wrapped so they produce objects that include the type key. The value of the type key will be the name of the key on which the function is located.

CtorsUnion

This type takes a type T that is an object where each key is a string and each value is a function that constructs an object. It returns a type that is a union of the return type of the constructor functions.

Using different discriminator

If you don't want to use the key type as the discriminator you can use ctorsUnionWithTypeKey instead of ctorsUnion.

How to publish

yarn version --patch
yarn version --minor
yarn version --major