On Mon, Mar 20, 2017 at 4:59 PM, Michael J. Ryan <[email protected]> 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
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to