feat(result): add unwrapOr, unwrapOrElse, map, and mapFailure#39
Merged
electric-gizzard merged 1 commit intoJun 28, 2026
Merged
Conversation
Adds Rust-inspired combinators to complement unwrap/expect: - unwrapOr / unwrapOrElse: total, non-throwing extractors that return a fallback value (eager) or compute one from the failure (lazy). - map / mapFailure: transform the success data or the failure while passing the other state through untouched.
🦋 Changeset detectedLatest commit: dc78c5e The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
electric-gizzard
approved these changes
Jun 28, 2026
electric-gizzard
left a comment
Contributor
There was a problem hiding this comment.
@coderdan your "Rust zealot[ry]" is showing 🦀
Thank you for contributing!
Contributor
Author
It's a sickness, what can I say! Thanks for the quick approval! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #37 (
unwrap/expect). Adds four more Rust-inspired combinators to@byteslice/result, all as free functions consistent with the package's existing functional surface:unwrapOr(result, fallback)— returns the successdata, or an eager fallback value. Never throws.unwrapOrElse(result, fn)— returns the successdata, or computes a fallback from the failure (fnis only invoked on failure). Never throws.map(result, fn)— transforms the successdata(Result<S, F>→Result<U, F>), passing a failure through untouched.mapFailure(result, fn)— transforms thefailure(Result<S, F>→Result<S, G>), passing a success through untouched.Design notes
unwrap/expect, theunwrapOr*pair always returns a value — fitting TS's preference for not throwing on expected paths. They correctly return falsy success data (e.g.0,'') rather than the fallback, since they branch on the success/failure discriminant, not truthiness.unwrapOrElseandmapFailurereceive the fullfailure(F), not an extractedError— mirroring Rust'sEand giving callers access to custom-failure fields. (unwrap/expectextract anErroronly because they throw.)mapis synchronous and non-catching, matching Rust's infallible closure. If a transform can fail,withResultis the right tool — called out in the docs.mapFailure's mapped failure is constrainedG extends FailureOption, so you can't map to an invalid failure shape.Testing
bun test,biome ci,cspell, andtsupbuild all pass.Changeset
Included a
minorchangeset. Also addedcombinator/combinatorsto the cspell word list.