Skip to content
This repository has been archived by the owner on Mar 23, 2020. It is now read-only.

Commit

Permalink
Allow mixing .is() with .true()
Browse files Browse the repository at this point in the history
  • Loading branch information
nawordar committed Sep 5, 2019
1 parent f0f1508 commit 9d1678d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ module.exports = {
transform: { "\\.ts$": "ts-jest" },
moduleFileExtensions: ["ts", "js"],
globals: {
"ts-jest": { enableTsDiagnostics: true }
"ts-jest": { diagnostics: true }
}
};
35 changes: 35 additions & 0 deletions src/__tests__/true.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,38 @@ describe("'when.true' syntax with assertion wrapped in thunk", () => {
expect(isBuffer({ isBuffer: () => false })).toBe(2);
});
});

describe("'when.true' mixed with when(subject) syntax", () => {

it("should allow to call method is() after method true()", () => {
const whenTrue = (subject: any, bool: boolean) =>
when(subject)
.true(bool, true);

expect(whenTrue({}, true)).toHaveProperty("is");
});

it("should allow to call method true() after method true()", () => {
const whenTrue = (subject: any, bool: boolean) =>
when(subject)
.true(bool, true);

expect(whenTrue({}, true)).toHaveProperty("true");
});

it("should allow to call method true() after method is()", () => {
const whenIs = (subject: string) =>
when(subject)
.is("funny", 420);

expect(whenIs("whatever")).toHaveProperty("true");
});

it("should allow to call method is() after method is()", () => {
const whenIs = (subject: string) =>
when(subject)
.is("funny", 420);

expect(whenIs("whatever")).toHaveProperty("is");
});
});
19 changes: 16 additions & 3 deletions src/when.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ export interface Matcher<T, R extends T> {
export interface When<T, V> {
is: <U extends T, W>(
matcher: U,
returnValue: ((inputValue: U) => W) | W,
value: ((inputValue: U) => W) | W,
) => When<T, V | W>;

match: <U extends T, W>(
matcher: Matcher<T, U>,
returnValue: ((inputValue: U) => W) | W,
value: ((inputValue: U) => W) | W,
) => When<T, V | W>;

else: <W>(returnValue: ((inputValue: T) => W) | W) => V | W;
true: <W>(
assertion: (() => boolean) | boolean,
value: (() => W) | W,
) => When<T, V | W>;

else: <W>(
returnValue: ((inputValue: T) => W) | W,
) => V | W;
}

export interface WhenSwitch {
Expand All @@ -36,6 +43,7 @@ export interface True<T> {
const resolve = (resolvedValue: any): When<any, any> => ({
is: () => resolve(resolvedValue),
match: () => resolve(resolvedValue),
true: () => resolve(resolvedValue),
else: () => resolvedValue,
});

Expand All @@ -53,6 +61,11 @@ export const when = (<T>(expr: T): When<T, never> => ({
? resolve(typeof value === "function" ? (value as (x: any) => any)(expr) : value)
: when(expr),

true: (assertion, value) =>
(typeof assertion === "function" ? assertion() : assertion)
? resolve(typeof value === "function" ? (value as (() => any))() : value)
: when(expr),

else: (defaultValue) =>
typeof defaultValue === "function" ? (defaultValue as (x: any) => any)(expr) : defaultValue,
})) as WhenSwitch;
Expand Down
7 changes: 6 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
"rules": {
"interface-name": false,
"object-literal-sort-keys": false,
"ordered-imports": false
"ordered-imports": false,
"indent": [
true,
"spaces",
2
]
},
"rulesDirectory": []
}

0 comments on commit 9d1678d

Please sign in to comment.