Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ const isChrome = (x: Navigator): boolean => "vendor" in x && /Google Inc/.test(x
const isIe = (x: Navigator): boolean => /Trident/.test(x.userAgent);

export const getCurrentBrowser = (navigator: Navigator): TBrowser =>
Switch<Navigator, TBrowser>(navigator)
.case(isEdge, "edge")
.case(isChrome, "chrome")
.case(isIe, "ie")
.default("firefox");
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);
```
Expand Down
9 changes: 1 addition & 8 deletions src/ISwitch.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { TNonFunction, TPredicateFunction } from "./TPredicateFunction";
import { TKnown } from "./TKnown";
import { Unpack } from "./Switch";

/**
* ISwitch for cases when K is defined.
* @type TDefinedSwitch
*/
export type TDefinedSwitch<T, K, N> = ISwitch<T, TKnown<K, N>>;
import { Unpack } from "./Unpack";

/**
* @interface ISwitch
Expand Down
15 changes: 7 additions & 8 deletions src/Switch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TNonFunction, TPredicateFunction } from "./TPredicateFunction";
import { ISwitch } from "./ISwitch";

export type Unpack<T> = T extends (infer U)[] ? U : T;
import { Unpack } from "./Unpack";

class SwitchMatched<T, K> implements ISwitch<T, K> {
public static for<T>(x: T): any {
Expand All @@ -20,18 +19,18 @@ class SwitchMatched<T, K> implements ISwitch<T, K> {
}

/**
* Switch resembles imperative switch statement using functions.
* Switch resembles imperative switch statement using chaining.
*
* Internally, Switch behaves like Either in the sense that it preserves the position of Right
* until it successfully matches case predicate (either function or value). If matching happens,
* Switch becomes a kind of Left and holds the value until it reaches the .default call. If
* matching didn't happen for all cases, the value of the .default argument is returned instead.
*/
export class Switch<T, K extends any[]> implements ISwitch<T, K> {
export class Switch<T, K extends []> implements ISwitch<T, K> {
/**
* Pointer interface for lifting a value into Switch.
*/
public static for<T, K extends any[] = []>(x: T): ISwitch<T, K> {
public static for<T, K extends [] = []>(x: T): ISwitch<T, K> {
return new Switch<T, K>(x);
}

Expand All @@ -49,11 +48,11 @@ export class Switch<T, K extends any[]> implements ISwitch<T, K> {
* Define predicate function to be executed against Switch state and the value to be
* returned in case of matching.
*/
public case<N>(pred: TPredicateFunction<T>, res: N): ISwitch<T, [Unpack<K>, N]>;
public case<N>(pred: any, res: N): ISwitch<T, [Unpack<K>, N]> {
public case<N>(pred: TPredicateFunction<T>, value: N): ISwitch<T, [Unpack<K>, N]>;
public case<N>(pred: any, value: N): ISwitch<T, [Unpack<K>, N]> {
const check = typeof pred == "function" ? pred(this.x) : pred === this.x;

return check ? SwitchMatched.for(res) : Switch.for(this.x);
return check ? SwitchMatched.for(value) : Switch.for(this.x);
}

/**
Expand Down
4 changes: 0 additions & 4 deletions src/TKnown.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/Unpack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Unpack<T> = T extends (infer U)[] ? U : T;
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Switch } from "./Switch";
import { ISwitch } from "./ISwitch";

export * from "./ISwitch";
export * from "./Switch";
export * from "./TKnown";
export * from "./Unpack";
export * from "./TPredicateFunction";

/**
* Pointer interface for lifting value provided as an argument to Switch.
*/
export default function<T, K extends any[]>(x: T) {
export default function<T, K extends []>(x: T): ISwitch<T, K> {
return Switch.for<T, K>(x);
}
3 changes: 1 addition & 2 deletions tests/Switch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Switch } from "../src";
import match from "../src";
import match, { Switch } from "../src";

describe("Switch", () => {
it("should exist", () => {
Expand Down