Proposal: Boolean.parseBoolean

Ben Newman benjamin at cs.stanford.edu
Thu Mar 16 21:03:01 UTC 2017


Just to check my understanding, would

  Boolean.parseBoolean = function (value) {
    return !! (value && JSON.parse(String(value)));
  };

be a reasonable polyfill for your proposed function?

This would definitely be useful for parsing `process.env.*` environment
variables in Node!

Ben

On Thu, Mar 16, 2017 at 4:55 PM Dmitry Soshnikov <dmitry.soshnikov at gmail.com>
wrote:

> Similar to `Number.parseInt`, the `Boolean.parseBooelan` might be useful
> for "boolean strings" retrieved from some string-based storages, which do
> not support other types at serialization.
>
> ```
> Boolean.parseBoolean('true'); // true
> Boolean.parseBoolean('false'); // false
> ```
>
> This is to contrast current:
>
> ```
> Boolean('false'); // true
> ```
>
> In JS people do today:
>
> ```
> let shouldRefresh = (data.shouldRefresh === 'true');
> ```
>
> Other parsing results:
>
> ```
> Boolean.parseBoolean('1'); // true
> Boolean.parseBoolean(1); // true
> Boolean.parseBoolean(<not-falsey>); // true
> Boolean.parseBoolean(true); // true
>
> // Falsey:
> Boolean.parseBoolean('0'); // false
> Boolean.parseBoolean(''); // false
> Boolean.parseBoolean(false); // false
> ```
>
> The API, and the results are from Java's corresponding
> `Boolean.parseBoolean` method:
> https://docs.oracle.com/javase/7/docs/api/java/lang/Boolean.html#parseBoolean(java.lang.String)
> .
>
> Dmitry
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170316/103c8b1d/attachment.html>


More information about the es-discuss mailing list