2.2.0 • Published 4 years ago

sums-up v2.2.0

Weekly downloads
579
License
MIT
Repository
-
Last release
4 years ago

👍 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

2.2.0

4 years ago

2.2.0-0

4 years ago

2.2.0-1

4 years ago

2.1.0

4 years ago

2.0.2

4 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.1.0

6 years ago

0.0.1

6 years ago