Skip to content

Commit c3caaa5

Browse files
author
Sergei Orlov
authored
Merge pull request #6 from Raini-js/dev
v1.1.1
2 parents 104587c + d094862 commit c3caaa5

File tree

7 files changed

+18
-29
lines changed

7 files changed

+18
-29
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ const isChrome = (x: Navigator): boolean => "vendor" in x && /Google Inc/.test(x
6262
const isIe = (x: Navigator): boolean => /Trident/.test(x.userAgent);
6363

6464
export const getCurrentBrowser = (navigator: Navigator): TBrowser =>
65-
Switch<Navigator, TBrowser>(navigator)
66-
.case(isEdge, "edge")
67-
.case(isChrome, "chrome")
68-
.case(isIe, "ie")
69-
.default("firefox");
65+
Switch(navigator)
66+
.case(isEdge, "edge" as const)
67+
.case(isChrome, "chrome" as const)
68+
.case(isIe, "ie" as const)
69+
.default("firefox" as const);
7070

7171
const browser = getCurrentBrowser(navigator);
7272
```

src/ISwitch.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import { TNonFunction, TPredicateFunction } from "./TPredicateFunction";
2-
import { TKnown } from "./TKnown";
3-
import { Unpack } from "./Switch";
4-
5-
/**
6-
* ISwitch for cases when K is defined.
7-
* @type TDefinedSwitch
8-
*/
9-
export type TDefinedSwitch<T, K, N> = ISwitch<T, TKnown<K, N>>;
2+
import { Unpack } from "./Unpack";
103

114
/**
125
* @interface ISwitch

src/Switch.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { TNonFunction, TPredicateFunction } from "./TPredicateFunction";
22
import { ISwitch } from "./ISwitch";
3-
4-
export type Unpack<T> = T extends (infer U)[] ? U : T;
3+
import { Unpack } from "./Unpack";
54

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

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

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

56-
return check ? SwitchMatched.for(res) : Switch.for(this.x);
55+
return check ? SwitchMatched.for(value) : Switch.for(this.x);
5756
}
5857

5958
/**

src/TKnown.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/Unpack.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Unpack<T> = T extends (infer U)[] ? U : T;

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { Switch } from "./Switch";
2+
import { ISwitch } from "./ISwitch";
23

34
export * from "./ISwitch";
45
export * from "./Switch";
5-
export * from "./TKnown";
6+
export * from "./Unpack";
67
export * from "./TPredicateFunction";
78

89
/**
910
* Pointer interface for lifting value provided as an argument to Switch.
1011
*/
11-
export default function<T, K extends any[]>(x: T) {
12+
export default function<T, K extends []>(x: T): ISwitch<T, K> {
1213
return Switch.for<T, K>(x);
1314
}

tests/Switch.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Switch } from "../src";
2-
import match from "../src";
1+
import match, { Switch } from "../src";
32

43
describe("Switch", () => {
54
it("should exist", () => {

0 commit comments

Comments
 (0)