Socket
Book a DemoInstallSign in
Socket

sums-up

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sums-up

👍 Sums Up ===

latest
npmnpm
Version
2.2.0
Version published
Weekly downloads
301
33.19%
Maintainers
1
Weekly downloads
 
Created
Source

👍 Sums Up

Installation

npm install sums-up

Example

In TypeScript:

import SumType from 'sums-up';

class Maybe<T> extends SumType<{ Just: [T]; Nothing: [] }> {}

function Just<T>(value: T): Maybe<T> {
  return new Maybe("Just", value);
}

function Nothing<T>(): Maybe<T> {
  return new Maybe("Nothing");
}

const x = Just("foo");

const result = x.caseOf({
  Nothing: () => "nope",
  Just: (a) => a + "bar",
});

Or in JavaScript

import SumType from 'sums-up';

class Maybe extends SumType {}

function Just(value) {
  return new Maybe("Just", value);
}

function Nothing() {
  return new Maybe("Nothing");
}

const x = Just("foo");

const result = x.caseOf({
  Nothing: () => "nope",
  Just: (a) => a + "bar",
});

Wildcard Matching

If the kind of a sum type instance isn't present in the pattern given to caseOf, a default key called _ will be used instead.

import SumType from 'sums-up';

class RequestState<T = never> extends SumType<{
  NotStarted: [];
  Connecting: [];
  Downloading: [number];
  Completed: [T];
  Failed: [string];
}> {}

const state = new RequestState('Failed', 'Connection reset.');
const progressPercentage = state.caseOf({
  Downloading: pct => pct,
  Completed: () => 100,
  _: () => 0
});

Contributors:

@hojberg @dfreeman @AdamEdgett

Keywords

functionalprogramming

FAQs

Package last updated on 03 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.