Skip to content

Commit

Permalink
merge transformEither and transformEffect into transformResult (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Mar 24, 2023
1 parent f32e2e1 commit 5bd0a7e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-walls-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": minor
---

merge transformEither and transformEffect into transformResult
49 changes: 4 additions & 45 deletions packages/schema/src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ export function filter<A>(
@category combinators
@since 1.0.0
*/
export const transformEffect: {
export const transformResult: {
<I2, A2, A1>(
to: Schema<I2, A2>,
decode: (a1: A1, options?: ParseOptions) => ParseResult<I2>,
Expand All @@ -781,47 +781,6 @@ export const transformEffect: {
encode: (i2: I2, options?: ParseOptions) => ParseResult<A1>
): Schema<I1, A2> => make(AST.createTransform(from.ast, to.ast, decode, encode)))

/**
Create a new `Schema` by transforming the input and output of an existing `Schema`
using the provided decoding functions.
@category combinators
@since 1.0.0
*/
export const transformEither: {
<I2, A2, A1>(
to: Schema<I2, A2>,
decode: (
a1: A1,
options?: ParseOptions
) => E.Either<PR.ParseError, I2>,
encode: (
i2: I2,
options?: ParseOptions
) => E.Either<PR.ParseError, A1>
): <I1>(self: Schema<I1, A1>) => Schema<I1, A2>
<I1, A1, I2, A2>(
from: Schema<I1, A1>,
to: Schema<I2, A2>,
decode: (
a1: A1,
options?: ParseOptions
) => E.Either<PR.ParseError, I2>,
encode: (
i2: I2,
options?: ParseOptions
) => E.Either<PR.ParseError, A1>
): Schema<I1, A2>
} = dual(4, <I1, A1, I2, A2>(
from: Schema<I1, A1>,
to: Schema<I2, A2>,
decode: (
a1: A1,
options?: ParseOptions
) => E.Either<PR.ParseError, I2>,
encode: (i2: I2, options?: ParseOptions) => E.Either<PR.ParseError, A1>
): Schema<I1, A2> => make(AST.createTransform(from.ast, to.ast, decode, encode)))

/**
Create a new `Schema` by transforming the input and output of an existing `Schema`
using the provided mapping functions.
Expand Down Expand Up @@ -849,7 +808,7 @@ export const transform: {
decode: (a1: A1) => I2,
encode: (i2: I2) => A1
): Schema<I1, A2> =>
transformEither(from, to, (a) => E.right(decode(a)), (b) => E.right(encode(b)))
transformResult(from, to, (a) => E.right(decode(a)), (b) => E.right(encode(b)))
)

/**
Expand Down Expand Up @@ -1417,7 +1376,7 @@ export const date: Schema<Date> = declare(
@category parsers
@since 1.0.0
*/
export const dateFromString: Schema<string, Date> = transformEffect(
export const dateFromString: Schema<string, Date> = transformResult(
string,
date,
(s) => {
Expand Down Expand Up @@ -1856,7 +1815,7 @@ export const clamp = <A extends number>(min: number, max: number) =>
@since 1.0.0
*/
export const numberFromString = <I>(self: Schema<I, string>): Schema<I, number> => {
const schema: Schema<I, number> = transformEffect(
const schema: Schema<I, number> = transformResult(
self,
number,
(s) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/schema/test/Forbidden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ describe.concurrent("Forbidden", () => {

it("transform", () => {
const schema = pipe(
S.transformEither(
S.transformResult(
S.string,
S.transformEffect(
S.transformResult(
S.string,
S.string,
(s) => PR.flatMap(Util.sleep, () => PR.success(s)),
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/test/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ describe.concurrent("Schema", () => {
const To = S.struct({ radius: S.number, _isVisible: S.boolean })

const Circle = pipe(
S.transformEither(From, To, S.parseEither(To), ({ _isVisible, ...rest }) => E.right(rest)),
S.transformResult(From, To, S.parseEither(To), ({ _isVisible, ...rest }) => E.right(rest)),
S.attachPropertySignature("_tag", "Circle")
)
expect(S.decode(Circle)({ radius: 10, _isVisible: true })).toEqual({
Expand Down

0 comments on commit 5bd0a7e

Please sign in to comment.