Skip to content
/ bwitch Public

A tiny weeny pattern matcher with zero dependencies

License

Notifications You must be signed in to change notification settings

alii/bwitch

Repository files navigation

bwitch

A tiny weeny pattern matcher with zero dependencies. Requires TypeScript 5.0+

Install

yarn add bwitch

Usage

import {bwitch} from 'bwitch';

const page = 'home';

const route = bwitch(page)
	.case('home', () => '/api/home/timeline')
	.case('weather', () => '/api/v1/weather/forecast')
	.orThrow();

route; // => `"/api/home/timeline" | "/api/v1/weather/forecast"`

Alternatively, you can finish a bwitch with a default case, or a helper to default to null

import {bwitch} from 'bwitch';

const page = 'home';

// Specify default
const route = bwitch(page)
	.case('home', () => '/api/home/timeline')
	// ...
	.or(value => '/api/home/timeline');

// Default to null
// This is the same as doing `.or(() => null)`
const route = bwitch(page)
	.case('home', () => '/api/home/timeline')
	// ...
	.orNull();

About

A tiny weeny pattern matcher with zero dependencies

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published