You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import Switch from "@raini/switch";
const match = 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
import Switch from "@raini/switch";
const match = 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
import Switch from "@raini/switch";
type TBrowser = "firefox" | "edge" | "chrome" | "ie";
const isEdge = (x: Navigator): boolean => /Edge/.test(x.userAgent);
const isChrome = (x: Navigator): boolean => "vendor" in x && /Google Inc/.test(x.vendor);
const isIe = (x: Navigator): boolean => /Trident/.test(x.userAgent);
export const getCurrentBrowser = (navigator: Navigator): TBrowser =>
Switch(navigator)
.case(isEdge, "edge" as const)
.case(isChrome, "chrome" as const)
.case(isIe, "ie" as const)
.default("firefox" as const);
const browser = getCurrentBrowser(navigator);