Proposal: Boolean.parseBoolean

Steve Fink sphink at gmail.com
Sat Mar 18 16:35:33 UTC 2017


On 03/16/2017 09:40 PM, Dmitry Soshnikov wrote:
> On Thu, Mar 16, 2017 at 7:04 PM, Karl Cheng <qantas94heavy at gmail.com 
> <mailto:qantas94heavy at gmail.com>> wrote:
>
>     On 17 March 2017 at 08:03, Ben Newman <benjamin at cs.stanford.edu
>     <mailto: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).

There are many, many reasonable choices for a function that maps a 
string to a boolean. Even more for a function that maps an arbitrary 
value to a boolean. The choice of the function is highly context 
dependent. That context includes language/locale/whatever the right l10n 
term is. It's true that JS could arbitrarily pick one, but then it would 
implicitly be favoring one context over another. And not even Node and 
the Web would completely agree on the most appropriate definition. It 
makes sense for JSON to pick a single function, because it's a specified 
interchange format.

-1 from me.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170318/d57717ac/attachment.html>


More information about the es-discuss mailing list