Skip to content

Commit

Permalink
Add JavaScript example
Browse files Browse the repository at this point in the history
  • Loading branch information
hojberg committed Mar 14, 2019
1 parent a237639 commit 92dbee0
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
npm install sums-up
```

### Example:
### Example

In TypeScript:

```typescript
import SumType from 'sums-up';
Expand All @@ -30,6 +32,29 @@ const result = x.caseOf({
});
```

Or in JavaScript

```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.
Expand Down

0 comments on commit 92dbee0

Please sign in to comment.