From fa6e0007cf07d2c474cbd302892b5ed58e7906df Mon Sep 17 00:00:00 2001 From: Sergei Orlov Date: Wed, 15 Jan 2020 18:12:11 +0300 Subject: [PATCH 1/7] refactor(case): rename res to value for consistency --- src/Switch.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Switch.ts b/src/Switch.ts index 6f322bd..cde1e1f 100644 --- a/src/Switch.ts +++ b/src/Switch.ts @@ -49,11 +49,11 @@ export class Switch implements ISwitch { * Define predicate function to be executed against Switch state and the value to be * returned in case of matching. */ - public case(pred: TPredicateFunction, res: N): ISwitch, N]>; - public case(pred: any, res: N): ISwitch, N]> { + public case(pred: TPredicateFunction, value: N): ISwitch, N]>; + public case(pred: any, value: N): ISwitch, 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); } /** From ad59406a2084fbea5c1d3ca67bec073106538fc1 Mon Sep 17 00:00:00 2001 From: Sergei Orlov Date: Wed, 15 Jan 2020 18:12:50 +0300 Subject: [PATCH 2/7] docs(switch): fix annotation mistake --- src/Switch.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Switch.ts b/src/Switch.ts index cde1e1f..7ae4da1 100644 --- a/src/Switch.ts +++ b/src/Switch.ts @@ -20,7 +20,7 @@ class SwitchMatched implements ISwitch { } /** - * 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, From 3f11efe6bfa0c06e26fcbcb7532d2dc436bd172e Mon Sep 17 00:00:00 2001 From: Sergei Orlov Date: Wed, 15 Jan 2020 18:14:10 +0300 Subject: [PATCH 3/7] chore(types): clean up types --- src/ISwitch.ts | 9 +-------- src/Switch.ts | 3 +-- src/TKnown.ts | 4 ---- src/Unpack.ts | 1 + 4 files changed, 3 insertions(+), 14 deletions(-) delete mode 100644 src/TKnown.ts create mode 100644 src/Unpack.ts diff --git a/src/ISwitch.ts b/src/ISwitch.ts index 1e3fcad..b0b464c 100644 --- a/src/ISwitch.ts +++ b/src/ISwitch.ts @@ -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 = ISwitch>; +import { Unpack } from "./Unpack"; /** * @interface ISwitch diff --git a/src/Switch.ts b/src/Switch.ts index 7ae4da1..55ec262 100644 --- a/src/Switch.ts +++ b/src/Switch.ts @@ -1,7 +1,6 @@ import { TNonFunction, TPredicateFunction } from "./TPredicateFunction"; import { ISwitch } from "./ISwitch"; - -export type Unpack = T extends (infer U)[] ? U : T; +import { Unpack } from "./Unpack"; class SwitchMatched implements ISwitch { public static for(x: T): any { diff --git a/src/TKnown.ts b/src/TKnown.ts deleted file mode 100644 index 5394581..0000000 --- a/src/TKnown.ts +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Returns the second type if the first type extends unknown. - */ -export type TKnown = never extends T ? K : T; diff --git a/src/Unpack.ts b/src/Unpack.ts new file mode 100644 index 0000000..7d52a10 --- /dev/null +++ b/src/Unpack.ts @@ -0,0 +1 @@ +export type Unpack = T extends (infer U)[] ? U : T; From e84a8ad7ffa5fdac657835987cdab301b6b2b97d Mon Sep 17 00:00:00 2001 From: Sergei Orlov Date: Wed, 15 Jan 2020 18:15:25 +0300 Subject: [PATCH 4/7] chore(types): fix exporting non-existing type in index.ts --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 2eda963..b95d040 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import { Switch } from "./Switch"; export * from "./ISwitch"; export * from "./Switch"; -export * from "./TKnown"; +export * from "./Unpack"; export * from "./TPredicateFunction"; /** From a406395bfb34050c56cc05acc8445f8e419549ad Mon Sep 17 00:00:00 2001 From: Sergei Orlov Date: Wed, 15 Jan 2020 23:10:22 +0300 Subject: [PATCH 5/7] fix(switch): improve type inference --- src/Switch.ts | 4 ++-- src/index.ts | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Switch.ts b/src/Switch.ts index 55ec262..0e525d7 100644 --- a/src/Switch.ts +++ b/src/Switch.ts @@ -26,11 +26,11 @@ class SwitchMatched implements ISwitch { * 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 implements ISwitch { +export class Switch implements ISwitch { /** * Pointer interface for lifting a value into Switch. */ - public static for(x: T): ISwitch { + public static for(x: T): ISwitch { return new Switch(x); } diff --git a/src/index.ts b/src/index.ts index b95d040..7252cbc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import { Switch } from "./Switch"; +import { ISwitch } from "./ISwitch"; export * from "./ISwitch"; export * from "./Switch"; @@ -8,6 +9,6 @@ export * from "./TPredicateFunction"; /** * Pointer interface for lifting value provided as an argument to Switch. */ -export default function(x: T) { +export default function(x: T): ISwitch { return Switch.for(x); } From 3d860d3b6489182348c24ea5704f6b774974fd6d Mon Sep 17 00:00:00 2001 From: Sergei Orlov Date: Wed, 15 Jan 2020 23:11:41 +0300 Subject: [PATCH 6/7] docs(readme): update documentation --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4112166..1d20add 100644 --- a/README.md +++ b/README.md @@ -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) - .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); ``` From d09486234c28d03f74f9da957caa58ca25b3c17c Mon Sep 17 00:00:00 2001 From: Sergei Orlov Date: Wed, 15 Jan 2020 23:12:33 +0300 Subject: [PATCH 7/7] chore: optimize imports --- tests/Switch.spec.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Switch.spec.ts b/tests/Switch.spec.ts index 43e2afe..2e78510 100644 --- a/tests/Switch.spec.ts +++ b/tests/Switch.spec.ts @@ -1,5 +1,4 @@ -import { Switch } from "../src"; -import match from "../src"; +import match, { Switch } from "../src"; describe("Switch", () => { it("should exist", () => {