Skip to content

Commit

Permalink
Merge pull request #81 from nobrayner/fix-80
Browse files Browse the repository at this point in the history
Fix: Result.fromJSON return type
  • Loading branch information
bloodyowl authored Sep 10, 2024
2 parents bd8584f + ac7c834 commit 3c53fb0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"format": "prettier '**/*' -u -w",
"prepack": "yarn test && yarn build",
"test": "vitest run",
"test:typecheck": "tsc --noEmit -p tsconfig.test.json",
"typecheck": "tsc --noEmit",
"benchmark": "node benchmark/src/option && node benchmark/src/result && node benchmark/src/future && node benchmark/src/future-result"
},
Expand Down
2 changes: 1 addition & 1 deletion src/OptionResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ class __Result<A, E> {
return false;
};

static fromJSON = <A, E>(value: JsonResult<A, E>) => {
static fromJSON = <A, E>(value: JsonResult<A, E>): Result<A, E> => {
return value.tag === "Ok"
? Result.Ok(value.value)
: Result.Error(value.error);
Expand Down
9 changes: 9 additions & 0 deletions test/AsyncData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,12 @@ test("AsyncData isAsyncData", async () => {
expect(AsyncData.isAsyncData([])).toEqual(false);
expect(AsyncData.isAsyncData({})).toEqual(false);
});

test("AsyncData JSON serialization", () => {
const data = AsyncData.Loading<number>();
expect(
AsyncData.fromJSON(data.toJSON()).tap(() => {
// use async data
}),
).toEqual(data);
});
2 changes: 1 addition & 1 deletion test/Future.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ test("Future concurrent", async () => {
--parallel;
}),
() =>
Future.make<4>((resolve) => {
Future.make((resolve) => {
expect(++parallel).toBeLessThanOrEqual(2);
setTimeout(() => resolve(undefined), 75);
})
Expand Down
9 changes: 9 additions & 0 deletions test/Option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,12 @@ test("Option.isOption", async () => {
expect(Option.isOption([])).toEqual(false);
expect(Option.isOption({})).toEqual(false);
});

test("Option JSON serialization", () => {
const option = Option.None();
expect(
Option.fromJSON(option.toJSON()).tap(() => {
// Use option
}),
).toEqual(option);
});
16 changes: 16 additions & 0 deletions test/Result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,19 @@ test("Result.isResult", async () => {
expect(Result.isResult([])).toEqual(false);
expect(Result.isResult({})).toEqual(false);
});

test("Result JSON serialization", async () => {
const resultOk = Result.Ok({ thing: "hello" });
expect(
Result.fromJSON(resultOk.toJSON()).tap(() => {
// Use result
}),
).toEqual(resultOk);

const resultError = Result.Error(new Error("Oops"));
expect(
Result.fromJSON(resultError.toJSON()).tap(() => {
// Use result
}),
).toEqual(resultError);
});
9 changes: 9 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"include": ["**/*.test.ts"],
"exclude": ["node_modules", "dist"],
"compilerOptions": {
"noEmit": true,
"skipLibCheck": true
},
}

0 comments on commit 3c53fb0

Please sign in to comment.