Skip to content

Commit

Permalink
new: helper to hush result decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
mlms13 committed Mar 24, 2023
1 parent ba6224c commit b6eae00
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Decode_AsResult_OfParseError.rei
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ let oneOf:
) =>
result('a, ParseError.failure);

let hush:
(Js.Json.t => result('a, ParseError.failure), Js.Json.t) => option('a);

[@deprecated "Will be removed in favor up the upcoming addition of letops"]
module Pipeline: {
let succeed: ('a, Js.Json.t) => result('a, ParseError.failure);
Expand Down
2 changes: 2 additions & 0 deletions src/Decode_Base.re
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ module Make =
let tupleFromFields = ((fieldA, decodeA), (fieldB, decodeB)) =>
map2(Tuple.make, field(fieldA, decodeA), field(fieldB, decodeB));

let hush = (decode, json) => decode(json) |> Result.toOption;

module Pipeline = {
let succeed = pure;

Expand Down
12 changes: 12 additions & 0 deletions test/Decode_AsResult_OfParseError_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ let objErr = (first, rest) =>

let objErrSingle = (field, err) => objErr((field, err), []);

describe("Decode utils", () => {
test("hush (success)", () => {
let decodeBooleanOpt = Decode.(boolean |> hush);
expect(decodeBooleanOpt(Sample.jsonBool)) |> toEqual(Some(true));
});

test("hush (failure)", () => {
let decodeStringOpt = Decode.(string |> hush);
expect(decodeStringOpt(Sample.jsonNull)) |> toEqual(None);
});
});

describe("Simple decoders", () => {
test("boolean", () =>
expect(Decode.boolean(Sample.jsonNull))
Expand Down

0 comments on commit b6eae00

Please sign in to comment.