How to convert empty string into an Option?

I wonder if Belt or Js have a utility to convert an empty string into an option

something like

let textFromApiResponse = ""
let text : option<string> = textFromApiResponse->falsyToOption

Js.log(text->Belt.Option.isNone) // true

AFAIK there is nothing like that. I guess because you can just pattern-match on the empty string (and because there is no Belt.String yet).

let stringToOption = str => switch(str) {
  | "" => None
  | str => Some(str)
1 Like

thanks, that’s what I’m going to use as a fallback