Skip to content

Commit

Permalink
Merge branch 'IOPLT-301-creating-utility-methods-for-eventhub-reading…
Browse files Browse the repository at this point in the history
…' of https://github.com/pagopa/data-ti-ms into IOPLT-301-creating-utility-methods-for-eventhub-reading
  • Loading branch information
drmarro committed Dec 14, 2023
2 parents ac8abee + a5d599b commit d920d04
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/formatter/__test__/case.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { switchCaseFormat } from "../case";

describe("switchCaseFormat", () => {
const aFieldValue = "foo";
const switchCaseMapping = { bar: "foo", foo: "bar" };
const anotherSwitchCaseMapping = { 1: 2, 2: 1 };
it("should return correct mapping if fieldValue matches a case", () => {
const processor = switchCaseFormat(switchCaseMapping, "");
expect(processor(aFieldValue)).toEqual(switchCaseMapping[aFieldValue]);
});

it("should return correct mapping with fieldValue type narrowing matching a case", () => {
const processor = switchCaseFormat(anotherSwitchCaseMapping, 0);
expect(processor(1)).toEqual(anotherSwitchCaseMapping[1]);
});

it("should return default mapping if fieldValue deos not match a case", () => {
const processor = switchCaseFormat(switchCaseMapping, "");
expect(processor(1)).toEqual("");
});
});
12 changes: 12 additions & 0 deletions src/formatter/case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { pipe } from "fp-ts/lib/function";
import * as O from "fp-ts/lib/Option";

export const switchCaseFormat = <T>(
cases: { readonly [key: string]: T },
defaultValue: T
) => (field: unknown): T =>
pipe(
cases[`${field}`],
O.fromNullable,
O.getOrElse(() => defaultValue)
);

0 comments on commit d920d04

Please sign in to comment.