Skip to content

Repository files navigation

@raini/switch

Build Statuscodecovnpmnpmlicence: MITcode style: prettierversioning: semantics

Installation

npm i -S @raini/switch

Usage

Equation matching

importSwitchfrom"@raini/switch";constmatch=x=>Switch(x).case(1,"matches 1!").case(2,"matches 2!").default("does not match!");console.log(match(1));// matches 1!console.log(match(2));// matches 2!console.log(match(3));// does not match!

Predicate function matching

importSwitchfrom"@raini/switch";constmatch=x=>Switch(x).case(x=>x>10,"greater than 10!").case(x=>x<10,"less than 10!").default("equals 10!");console.log(match(1));// less than 10!console.log(match(11));// greater than 10!console.log(match(10));// equals 10!

Examples

Get Current Browser

importSwitchfrom"@raini/switch";typeTBrowser="firefox"|"edge"|"chrome"|"ie";constisEdge=(x: Navigator): boolean=>/Edge/.test(x.userAgent);constisChrome=(x: Navigator): boolean=>"vendor"inx&&/GoogleInc/.test(x.vendor);constisIe=(x: Navigator): boolean=>/Trident/.test(x.userAgent);exportconstgetCurrentBrowser=(navigator: Navigator): TBrowser=>Switch(navigator).case(isEdge,"edge"asconst).case(isChrome,"chrome"asconst).case(isIe,"ie"asconst).default("firefox"asconst);constbrowser=getCurrentBrowser(navigator);

Sample Routing

importSwitchfrom"@raini/switch";import{createServer}from"http";import{sendIndexPage,sendAboutPage,sendNotFoundPage}from"./";constserver=createServer();server.on("request",(req,res)=>{constcb=Switch(req).case(({ url, method })=>url=="/"&&method=="GET",sendIndexPage).case(({ url, method })=>url=="/about"&&method=="GET",sendAboutPage).default(sendNotFoundPage);cb(req,res);});server.listen(process.env.PORT||3000);

About

A functional way to switch.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages