Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add withEncode function #146

Merged
merged 1 commit into from
Nov 9, 2020
Merged

add withEncode function #146

merged 1 commit into from
Nov 9, 2020

Conversation

EricCrosson
Copy link
Contributor

withEncode is similar to withValidate:

withEncode clones an existing codec and overrides the existing
encode function with the given encode function. This can be useful
when you want to use t.type to combinatorially create codecs but
want to leverage the encode function to encode into a different type.

@gcanti
Copy link
Owner

gcanti commented Nov 9, 2020

Thanks @EricCrosson, I would add an optional name argument though:

export function withEncode<A, O, I, P>(
  codec: t.Type<A, O, I>,
  encode: (a: A) => P,
  name: string = codec.name
): t.Type<A, P, I> {
  return new t.Type(name, codec.is, codec.validate, encode)
}

`withEncode` is similar to `withValidate`:

`withEncode` clones an existing codec and overrides the existing
`encode` function with the given `encode` function. This can be useful
when you want to use `t.type` to combinatorially create codecs but
want to leverage the `encode` function to encode into a different type.
@EricCrosson
Copy link
Contributor Author

Excellent! That's a much better implementation too. Thanks for the suggestion @gcanti , I've pushed these updates to the PR

@gcanti gcanti merged commit c6beba5 into gcanti:master Nov 9, 2020
@EricCrosson EricCrosson deleted the add-with-encode branch November 11, 2020 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants