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

coproduct

v3.0.0

Published

A small library aims to improve better tagged-unions/discriminated-unions supporting for TypeScript

Downloads

10

Readme

coproduct

npm version coproduct workflow Documentation Maintenance License: MIT Twitter: guyingjie129

A small library aims to improve better tagged-unions/discriminated-unions supporting for TypeScript

Benefits

  • Small bundled size(just 1kb)
  • Easy to use with just a few apis to learn
  • Improving Type-Safety for your TypeScript project via exhaustive pattern-matching

Installation

yarn add coproduct
npm install --save coproduct

Usage

For redux app

// state type
type CounterState = {
  count: number;
};

// action type
type CounterAction =
  | {
      type: 'incre';
    }
  | {
      type: 'decre';
    }
  | {
      type: 'increBy';
      step: number;
    }
  | {
      type: 'decreBy';
      step: number;
    };

// reducer with match
const counterReducer = (
  state: CounterState,
  action: CounterAction
): CounterState => {
  return match(action).case({
    incre: () => ({
      ...state,
      count: state.count + 1,
    }),
    decre: () => ({
      ...state,
      count: state.count - 1,
    }),
    increBy: ({ step }) => ({
      ...state,
      count: state.count + step,
    }),
    decreBy: ({ step }) => ({
      ...state,
      count: state.count - step,
    }),
  });
};

// reducer without match
const counterReducer = (
  state: CounterState,
  action: CounterAction
): CounterState => {
  if (action.type === 'incre') {
    return {
      ...state,
      count: state.count + 1,
    };
  } else if (action.type === 'decre') {
    return {
      ...state,
      count: state.count - 1,
    };
  } else if (action.type === 'increBy') {
    return {
      ...state,
      count: state.count + action.step,
    };
  } else if (action.type === 'decreBy') {
    return {
      ...state,
      count: state.count - action.step,
    };
  }

  throw new Error(`Unexpected action: ${action}`);
};

Basic usage

import { match } from 'coproduct';

export type Option<T> = {
  type: 'Some',
  value: T
} | {
  type: 'None'
}

export const None = {
  type: 'None' as const;
}
export const Some = <T>(value: T) => ({
  type: 'Some' as const,
  value,
});

const show = <T>(data: Option<T>) => {
  return match(data).case({
    Some: data => `some: ${data.value}`,
    None: () => 'none',
  });
};

const value0 = Some(1);
const value1 = None;

expect(show(value0)).toBe('some: 1');
expect(show(value1)).toBe('none');

// you can use if/else to match manually if you want
const show = <T>(data: Option<T>) => {
  if (data.type === 'Some') {
    return `some: ${data.some}`;
  } else if (data.type === 'None') {
    return 'none';
  }
  throw new Error(`Unexpected data: ${data}`);
};

You don't need to define your own option type, coproduct has built-in Option and Result.

import { match, Option, Some, None, Result, Ok, Err } from 'coproduct';

const show = <T>(data: Option<T>) => {
  return match(data).case({
    Some: data => `some: ${data.value}`,
    None: () => 'none',
  });
};

expect(show(Some(1))).toBe('some: 1');
expect(show(None)).toBe('none');

const showResult = <T>(result: Result<T>) => {
  return match(result).case({
    Ok: data => `ok: ${data.value}`,
    Err: error => `err: ${error.info}`,
  });
};

expect(showResult(Ok(1))).toBe('ok: 1');
expect(showResult(Err('error'))).toBe('err: error');

Api

match(data).case(patterns)

match(data).case(patterns) perform exhaustive pattern-matching for data, every case in data should has its own visitor function.

Note: you can use _: () => R as default handler for unmatched case.

createMatch(tagField) => match

You can create your own match function with tagField to match your data.

The default match of coproduct was created via createMatch('type')

const match = createMatch('tag');

type Data =
  | {
      tag: 'a';
      value: string;
    }
  | {
      tag: 'b';
      value: number;
    };

const handleData = (data: Data) => {
  return match(data).case({
    a: data => `a: ${data.value}`,
    b: data => `b: ${data.value}`,
  });
};

handleData({ tag: 'a', value: 'hello' }); // 'a: hello'
handleData({ tag: 'b', value: 1 }); // 'b: 1'

Some(value)

Some(value) return the value with the Some<T> case of Option Type.

None

None is the value with the None case of Option Type

Ok(value)

Ok(value) return the value with the Ok<T> case of Result Type.

Err(message)

Err(message) return the value with the Err<E> case of Result Type.

Caveats

  • The name $tag is reserved for $tag property of tagged object, it can't be used as a tag name.
  • The symbol _ can't be used as a tag name since it's a reserved filed in coproduct as placeholder for default case.

Contribution Guide

# test
npm run test

# build
npm run build

Author

👤 Jade Gu

🤝 Contributing

Contributions, issues and feature requests are welcome!

Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2022 Jade Gu.

This project is MIT licensed.