@housinganywhere/match
v1.1.1
Published
Poor man's pattern matching
Downloads
27
Readme
poor man's pattern matching :traffic_light:
Install
yarn add @housinganywhere/match
npm i @housinganywhere/match
Use
import * as React from 'react';
import match from '@housinganywhere/match';
type Status = 'loading' | 'error' | 'success';
enum Status {
loading = 'loading',
error = 'error',
success = 'success',
}
const StatusMsg: React.SFC<{ status: Status }> = ({ status }) =>
match<Status, React.ReactNode>({
loading: () => <Spinner />,
error: () => <Alert type="danger">There was an error</Alert>,
success: () => <Alert type="success">Yay! It worked</Alert>,
})(status);
For matching several cases together use wildMatch. All the missing cases
will be handled by case _
.
import { wildMatch } from '@housinganywhere/match';
type Vowels = 'a' | 'e' | 'i' | 'o' | 'u';
const isA = wildMatch<Vowels, string>({
a: () => 'Yay!',
_: (v) => `Nope, "${v}" is not "a"`,
});
isA('a'); // 'Yay!'
isA('e'); // 'Nope, "e" is not "a"'
isA('i'); // 'Nope, "i" is not "a"'
isA('o'); // 'Nope, "o" is not "a"'
isA('u'); // 'Nope, "u" is not "a"'
License
MIT © 2019 HousingAnywhere