swan-io / boxed

Essential building-blocks for functional & safe TypeScript code

Home Page:https://swan-io.github.io/boxed

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Would it be weird to have a new constructor method on Option to create an option from truthy?

itstheandre opened this issue · comments

Don't know if this is something smart, but would it be interesting to have an API like this:

Option.fromTruthy('') // Option<string> // effectively would be a None
Option.fromTruthy(0) // Option<number> // effectively would be a None
Option.fromTruthy(-0) // Option<number> // effectively would be a None
Option.fromTruthy(undefined) // Option<undefined> // effectively would be a None
Option.fromTruthy(null) // Option<null> // effectively would be a None
Option.fromTruthy(NaN) // Option<number> // effectively would be a None

If theres interest I can make a PR and test it as well

I'd prefer to avoid that kind of implicit behavior, Option will for the foreseeable future only expose fromNullable, fromUndefined & fromNull