Proposal: Boolean.parseBoolean

Dmitry Soshnikov dmitry.soshnikov at gmail.com
Tue Mar 21 05:47:20 UTC 2017


On Mon, Mar 20, 2017 at 4:59 PM, Michael J. Ryan <tracker1 at gmail.com> wrote:

> My suggestion for a polyfill.
>
> if (!Boolean.parse) {
>   Boolean.parse = function(input) {
>     // return false if it is already falsy
>     if (!input) return false;
>
>     var expr = String(input);
>
>     // return false if it's reasonably too long of a string
>     if (expr.length > 10) return false;
>
>     // test trimmed input against truthy values
>     return (/^(-?1|y|t|yes|true)$/).test(expr.trim().toLowerCase());
>   }
> }
>
> -1/1 are common database boolean/bit fields
> y/yes also common inputs for truthiness
> t/true also common for truthiness
>

Yeah, these might be good in general, although I'd like to reduce the
proposal to minimum vital version, and JS related. The problem we are
trying to solve is to parse `"false"` as `false`. It's not possible with
`Boolean('false')` today. And all the JSON.parse, and regexp manipulations
are too low-lever implementation details; users want good semantic library!
:)

So:

```
Boolean.parse('true'); // true
Boolean.parse('false'); // false
```

That's all we need for now, and already will be much better semantic
alternative too all existing "non-semantic building material".

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


More information about the es-discuss mailing list