Skip to content

Commit 104587c

Browse files
author
Sergei Orlov
authored
Merge pull request #5 from Raini-js/dev
v1.1.0
2 parents abfd9a9 + 021db11 commit 104587c

File tree

8 files changed

+32
-28
lines changed

8 files changed

+32
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ tsconfig.tsbuildinfo
99
*.d.ts
1010
*.map
1111
*.js
12+
!jest.config.js

index.ts

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

src/ISwitch.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TNonFunction, TPredicateFunction } from "./TPredicateFunction";
22
import { TKnown } from "./TKnown";
3+
import { Unpack } from "./Switch";
34

45
/**
56
* ISwitch for cases when K is defined.
@@ -14,16 +15,16 @@ export interface ISwitch<T, K> {
1415
/**
1516
* Define predicate to be matched against and the value to be returned in case of matching.
1617
*/
17-
case<N>(pred: TNonFunction<T>, value: TKnown<K, N>): TDefinedSwitch<T, K, N>;
18+
case<N>(pred: TNonFunction<T>, value: N): ISwitch<T, [Unpack<K>, N]>;
1819

1920
/**
2021
* Define predicate function to be executed against Switch state and the value to be
2122
* returned in case of matching.
2223
*/
23-
case<N>(pred: TPredicateFunction<T>, value: TKnown<K, N>): TDefinedSwitch<T, K, N>;
24+
case<N>(pred: TPredicateFunction<T>, value: N): ISwitch<T, [Unpack<K>, N]>;
2425

2526
/**
2627
* Define the value to be returned in case none of the cases was matched.
2728
*/
28-
default<N>(defaultValue: N): K | N;
29+
default<N>(defaultValue: N): Unpack<K> | N;
2930
}

src/Switch.ts

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

56
class SwitchMatched<T, K> implements ISwitch<T, K> {
67
public static for<T>(x: T): any {
@@ -26,11 +27,11 @@ class SwitchMatched<T, K> implements ISwitch<T, K> {
2627
* Switch becomes a kind of Left and holds the value until it reaches the .default call. If
2728
* matching didn't happen for all cases, the value of the .default argument is returned instead.
2829
*/
29-
export class Switch<T, K> implements ISwitch<T, K> {
30+
export class Switch<T, K extends any[]> implements ISwitch<T, K> {
3031
/**
3132
* Pointer interface for lifting a value into Switch.
3233
*/
33-
public static for<T, K>(x: T): ISwitch<T, K> {
34+
public static for<T, K extends any[] = []>(x: T): ISwitch<T, K> {
3435
return new Switch<T, K>(x);
3536
}
3637

@@ -42,14 +43,14 @@ export class Switch<T, K> implements ISwitch<T, K> {
4243
/**
4344
* Define predicate to be matched against and the value to be returned in case of matching.
4445
*/
45-
public case<N>(pred: TNonFunction<T>, value: TKnown<K, N>): TDefinedSwitch<T, K, N>;
46+
public case<N>(pred: TNonFunction<T>, value: N): ISwitch<T, [Unpack<K>, N]>;
4647

4748
/**
4849
* Define predicate function to be executed against Switch state and the value to be
4950
* returned in case of matching.
5051
*/
51-
public case<N>(pred: TPredicateFunction<T>, res: TKnown<K, N>): TDefinedSwitch<T, K, N>;
52-
public case<N>(pred: any, res: TKnown<K, N>): TDefinedSwitch<T, K, N> {
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]> {
5354
const check = typeof pred == "function" ? pred(this.x) : pred === this.x;
5455

5556
return check ? SwitchMatched.for(res) : Switch.for(this.x);
@@ -58,7 +59,7 @@ export class Switch<T, K> implements ISwitch<T, K> {
5859
/**
5960
* Define the value to be returned in case none of the cases was matched.
6061
*/
61-
public default<V>(defaultValue: V): K | V {
62+
public default<V>(defaultValue: V): Unpack<K> | V {
6263
return defaultValue;
6364
}
6465
}

src/TKnown.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
type TIsKnown<X, Y> = X extends unknown ? Y : X;
2-
31
/**
42
* Returns the second type if the first type extends unknown.
5-
* @type TIsKnown
63
*/
7-
export type TKnown<T, K> = T extends TIsKnown<T, K> ? T : K;
4+
export type TKnown<T, K> = never extends T ? K : T;

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1+
import { Switch } from "./Switch";
2+
13
export * from "./ISwitch";
24
export * from "./Switch";
35
export * from "./TKnown";
46
export * from "./TPredicateFunction";
7+
8+
/**
9+
* Pointer interface for lifting value provided as an argument to Switch.
10+
*/
11+
export default function<T, K extends any[]>(x: T) {
12+
return Switch.for<T, K>(x);
13+
}

tests/Switch.spec.ts

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

44
describe("Switch", () => {
55
it("should exist", () => {

tsconfig.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
55
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
66
"lib": ["es2018"] /* Specify library files to be included in the compilation. */,
7+
"baseUrl": "./src",
78
"sourceMap": true,
89
"declaration": true,
9-
10+
"outDir": "./",
1011
/* Strict Type-Checking Options */
1112
"strict": true /* Enable all strict type-checking options. */,
1213
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
@@ -34,5 +35,9 @@
3435

3536
/* Advanced Options */
3637
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
37-
}
38+
},
39+
"exclude": [
40+
"node_modules",
41+
"tests"
42+
]
3843
}

0 commit comments

Comments
 (0)