Skip to content

Commit 7389702

Browse files
Merge pull request #3 from Lukasz-pluszczewski/v2.0
Improved types, added types exports
2 parents 58f6fc7 + bd22ac6 commit 7389702

File tree

7 files changed

+116
-93
lines changed

7 files changed

+116
-93
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ swich function accepts two parameters: array of *Pattern*, *Result* tuples and *
496496
497497
## Changelog
498498
499+
### 1.2.0
500+
- Improved types
501+
- Added types exports
502+
499503
### 1.1.0
500504
- Added fallThrough functionality
501505

build/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/types/index.d.ts

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { Object } from "ts-toolbelt";
2-
declare type Pattern<TValue> = RegExp | ((value: TValue) => boolean) | ((value: TValue) => any) | any;
3-
declare type Result<TValue, TOutput> = TOutput | ((param: TValue) => TOutput);
4-
declare type CaseElement<TValue, TOutput> = [
2+
export declare type Pattern<TValue> = RegExp | ((value: TValue) => boolean) | ((value: TValue) => any) | any;
3+
export declare type Result<TValue, TOutput> = TOutput | ((param: TValue) => TOutput);
4+
export declare type CaseElement<TValue, TOutput> = [
55
Pattern<TValue>,
66
Result<TValue, TOutput>,
77
true?
88
];
9-
declare type DefaultCaseElement<TValue, TOutput> = [Result<TValue, TOutput>];
10-
declare type CaseElements<TValue, TOutput> = (CaseElement<TValue, TOutput> | DefaultCaseElement<TValue, TOutput>)[];
11-
declare type Matcher<TValue, TOutput> = (config: Object.Omit<Config<TValue, TOutput>, "matcher" | "resultGetter">) => (valueToMatch: TValue, pattern: Pattern<TValue>) => boolean;
12-
declare type ResultGetter<TValue, TOutput> = (config: Object.Omit<Config<TValue, TOutput>, "matcher" | "resultGetter">) => (valueToMatch: TValue, pattern: Pattern<TValue>, result: any) => TOutput;
13-
declare type Config<TValue, TOutput> = {
9+
export declare type DefaultCaseElement<TValue, TOutput> = [Result<TValue, TOutput>];
10+
export declare type CaseElements<TValue, TOutput> = (CaseElement<TValue, TOutput> | DefaultCaseElement<TValue, TOutput>)[];
11+
export declare type Matcher<TValue, TOutput> = (config: Object.Omit<Config<TValue, TOutput>, "matcher" | "resultGetter">) => (valueToMatch: TValue, pattern: Pattern<TValue>) => boolean;
12+
export declare type ResultGetter<TValue, TOutput> = (config: Object.Omit<Config<TValue, TOutput>, "matcher" | "resultGetter">) => (valueToMatch: TValue, pattern: Pattern<TValue>, result: any) => TOutput;
13+
export declare type Config<TValue, TOutput> = {
1414
returnMany: boolean;
1515
strict: boolean;
1616
acceptTruthyFunctionReturn: boolean;
@@ -21,43 +21,27 @@ declare type Config<TValue, TOutput> = {
2121
matcher: Matcher<TValue, TOutput>;
2222
resultGetter: ResultGetter<TValue, TOutput>;
2323
};
24-
declare type BasicConfig<TValue, TOutput> = Object.Omit<Config<TValue, TOutput>, "matcher" | "resultGetter">;
24+
export declare type BasicConfig<TValue, TOutput> = Object.Omit<Config<TValue, TOutput>, "matcher" | "resultGetter">;
2525
export declare const defaultMatcher: <TValue, TOutput>(config: import("ts-toolbelt/out/Object/Omit")._Omit<Config<TValue, TOutput>, "matcher" | "resultGetter">) => (valueToMatch: TValue, pattern: any) => boolean;
26-
export declare const defaultResultGetter: <TValue, TOutput>(config: import("ts-toolbelt/out/Object/Omit")._Omit<Config<TValue, TOutput>, "matcher" | "resultGetter">) => (valueToMatch: TValue, pattern: any, result: any) => TOutput;
27-
export declare const createSwich: <TValue, TOutput>({ returnMany: defaultReturnMany, strict: defaultStrict, acceptTruthyFunctionReturn: defaultAcceptTruthyFunctionReturn, catchFunctionErrors: defaultCatchFunctionErrors, performReplaceOnRegex: defaultPerformReplaceOnRegex, runResultFunction: defaultRunResultFunction, stopFallThrough: defaultStopFallThrough, matcher: defaultMatcherValue, resultGetter: defaultResultGetterValue, }?: {
28-
returnMany?: boolean;
29-
strict?: boolean;
30-
acceptTruthyFunctionReturn?: boolean;
31-
catchFunctionErrors?: boolean;
32-
performReplaceOnRegex?: boolean;
33-
runResultFunction?: boolean;
34-
stopFallThrough?: boolean;
35-
matcher?: Matcher<TValue, TOutput>;
36-
resultGetter?: ResultGetter<TValue, TOutput>;
37-
}) => (patterns: CaseElements<TValue, TOutput>, { returnMany, strict, acceptTruthyFunctionReturn, catchFunctionErrors, performReplaceOnRegex, runResultFunction, stopFallThrough, matcher, resultGetter, }?: {
38-
returnMany?: boolean;
39-
strict?: boolean;
40-
acceptTruthyFunctionReturn?: boolean;
41-
catchFunctionErrors?: boolean;
42-
performReplaceOnRegex?: boolean;
43-
runResultFunction?: boolean;
44-
stopFallThrough?: boolean;
45-
matcher?: Matcher<TValue, TOutput>;
46-
resultGetter?: ResultGetter<TValue, TOutput>;
47-
}) => (valueToMatch?: boolean | TValue) => TOutput[];
26+
export declare const defaultResultGetter: <TValue, TOutput>(config: import("ts-toolbelt/out/Object/Omit")._Omit<Config<TValue, TOutput>, "matcher" | "resultGetter">) => (valueToMatch: TValue, pattern: any, result: TOutput) => TOutput;
27+
declare type SwichReturnMany<TValue, TOutput> = (valueToMatch?: TValue | true) => TOutput[];
28+
declare type SwichReturnOne<TValue, TOutput> = (valueToMatch?: TValue | true) => TOutput;
29+
declare function swich<TValue, TOutput>(patterns: CaseElements<TValue, TOutput>, config: Object.Optional<Config<TValue, TOutput>> & {
30+
returnMany: true;
31+
}): SwichReturnMany<TValue, TOutput>;
32+
declare function swich<TValue, TOutput>(patterns: CaseElements<TValue, TOutput>, config?: Object.Optional<Config<TValue, TOutput>> & {
33+
returnMany?: false;
34+
}): SwichReturnOne<TValue, TOutput>;
4835
export declare const gt: (compareValue: number) => (value: number) => boolean;
4936
export declare const gte: (compareValue: number) => (value: number) => boolean;
5037
export declare const lt: (compareValue: number) => (value: number) => boolean;
5138
export declare const lte: (compareValue: number) => (value: number) => boolean;
52-
declare const defaultSwich: <TValue, TOutput>(patterns: CaseElements<TValue, TOutput>, config?: {
53-
returnMany?: boolean;
54-
strict?: boolean;
55-
acceptTruthyFunctionReturn?: boolean;
56-
catchFunctionErrors?: boolean;
57-
performReplaceOnRegex?: boolean;
58-
runResultFunction?: boolean;
59-
stopFallThrough?: boolean;
60-
matcher?: Matcher<TValue, TOutput>;
61-
resultGetter?: ResultGetter<TValue, TOutput>;
62-
}) => (valueToMatch?: boolean | TValue) => TOutput[];
63-
export default defaultSwich;
39+
declare type CreateSwichReturnMany<TValue, TOutput> = <TValue, TOutput>(patterns: CaseElements<TValue, TOutput>, config?: Object.Optional<Config<TValue, TOutput>>) => SwichReturnMany<TValue, TOutput>;
40+
declare type CreateSwichReturnOne<TValue, TOutput> = <TValue, TOutput>(patterns: CaseElements<TValue, TOutput>, config?: Object.Optional<Config<TValue, TOutput>>) => SwichReturnOne<TValue, TOutput>;
41+
export declare function createSwich<TValue, TOutput>(defaultConfig: Object.Optional<Config<TValue, TOutput>> & {
42+
returnMany: true;
43+
}): CreateSwichReturnMany<TValue, TOutput>;
44+
export declare function createSwich<TValue, TOutput>(defaultConfig: Object.Optional<Config<TValue, TOutput>> & {
45+
returnMany?: false;
46+
}): CreateSwichReturnOne<TValue, TOutput>;
47+
export default swich;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swich",
3-
"version": "1.1.2",
3+
"version": "1.2.0",
44
"description": "Switch, but better",
55
"main": "build/index.js",
66
"types": "build/types/index.d.ts",

0 commit comments

Comments
 (0)