Skip to content

Commit

Permalink
Make getShow output more conformant
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
samhh committed Feb 16, 2024
1 parent ef884e9 commit 602b10e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/modules/index.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const showWeather = getShow<Weather>({
})

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
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export const getOrd = <A extends Sum.AnyMember>(ords: Ords<A>): Ord<A> =>
* })
*
* 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
*/
Expand All @@ -172,9 +172,9 @@ export const getShow = <A extends Sum.AnyMember>(shows: Shows<A>): Show<A> => ({
// 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<Value<A>>)
? `${k}(${(shows[k as keyof typeof shows] as unknown as Show<Value<A>>)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.show(v as any)}`
.show(v as any)})`
: k
},
})
4 changes: 2 additions & 2 deletions test/unit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
})
})
})

0 comments on commit 602b10e

Please sign in to comment.