From 602b10e4d1627453822668fb72672edd301f8b16 Mon Sep 17 00:00:00 2001 From: "Sam A. Horvath-Hunt" Date: Fri, 16 Feb 2024 11:09:34 +0000 Subject: [PATCH] Make getShow output more conformant Like this we better adhere to the idea that Show's output should ideally be a valid TypeScript expression that'd be reversible with something equivalent to Haskell's Read. --- docs/modules/index.ts.md | 2 +- src/index.ts | 6 +++--- test/unit/index.ts | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/modules/index.ts.md b/docs/modules/index.ts.md index b0b4b82..043b528 100644 --- a/docs/modules/index.ts.md +++ b/docs/modules/index.ts.md @@ -127,7 +127,7 @@ const showWeather = getShow({ }) assert.strictEqual(showWeather.show(Sun), 'Sun') -assert.strictEqual(showWeather.show(Rain(1)), 'Rain 1') +assert.strictEqual(showWeather.show(Rain(1)), 'Rain(1)') ``` Added in v0.1.0 diff --git a/src/index.ts b/src/index.ts index 7df365d..d6d00e8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -156,7 +156,7 @@ export const getOrd = (ords: Ords): Ord => * }) * * assert.strictEqual(showWeather.show(Sun), 'Sun') - * assert.strictEqual(showWeather.show(Rain(1)), 'Rain 1') + * assert.strictEqual(showWeather.show(Rain(1)), 'Rain(1)') * * @since 0.1.0 */ @@ -172,9 +172,9 @@ export const getShow = (shows: Shows): Show => ({ // defined a member for which the value actually can tangibly be `null` // e.g. `Member<'Rain', number | null>`. return k in shows - ? `${k} ${(shows[k as keyof typeof shows] as unknown as Show>) + ? `${k}(${(shows[k as keyof typeof shows] as unknown as Show>) // eslint-disable-next-line @typescript-eslint/no-explicit-any - .show(v as any)}` + .show(v as any)})` : k }, }) diff --git a/test/unit/index.ts b/test/unit/index.ts index 234c390..7bc42cc 100644 --- a/test/unit/index.ts +++ b/test/unit/index.ts @@ -103,8 +103,8 @@ describe("index", () => { }) it("outputs members with values using provived Show instance", () => { - expect(f(A(42))).toBe("A 42") - expect(f(C(42))).toBe("C 24") + expect(f(A(42))).toBe("A(42)") + expect(f(C(42))).toBe("C(24)") }) }) })