0.0.3 • Published 5 years ago

xwitch v0.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

xwitch

xwitch replaces switch statement with function call.

xwitch is short for expression switch.

Installation

yarn add xwitch

or

npm install xwitch

Usage

import xwitch from 'xwitch';
import {AdminIcon, UserIcon, AnonymousIcon} from './some-icon-path';

type UserType = 'admin' | 'normal' | 'anonymous';
const user: UserType = 'admin';

// icon is inferred as ReactElement | null
const icon = xwitch(user)
  .case('admin', () => <AdminIcon />)
  .case('normal', () => <UserIcon />)
  .case('anonymous', () => <AnonymousIcon />)
  .default(() => null);

API

TODO

Motivation

switch syntax in JavaScript has following disadvantage.

  1. switch is not expression but statement. So, Sometimes, You must declare variable as let even if the variable will never changed.
  2. case statement does not create a block scope. that specification makes it difficult to name a variable used in statements associated with that case.
  3. if you forget break statement, that causes unexpected behaviors.
0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago