Proposal: Boolean.parseBoolean

Dmitry Soshnikov dmitry.soshnikov at gmail.com
Fri Mar 17 04:40:05 UTC 2017


On Thu, Mar 16, 2017 at 7:04 PM, Karl Cheng <qantas94heavy at gmail.com> wrote:

> On 17 March 2017 at 08:03, Ben Newman <benjamin at cs.stanford.edu> wrote:
> > Just to check my understanding, would
> >
> >   Boolean.parseBoolean = function (value) {
> >     return !! (value && JSON.parse(String(value)));
> >   };
> >
> > be a reasonable polyfill for your proposed function?
>
> Not quite -- that would throw for strings that are not valid JSON, e.g.:
>
> ```
> Boolean.parseBoolean('{dddddd]');
> ```
>
> It'd probably be more like:
>
> ```
> Boolean.parseBoolean = function (val) {
>   if (val === 'false') return false;
>   return !!val;
> };
> ```
>

Looks good either (probably worth making case-insensitive).

Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170316/9c29dce3/attachment.html>


More information about the es-discuss mailing list