Proposal: Boolean.parseBoolean

classic Classic list List threaded Threaded
26 messages Options
12
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Proposal: Boolean.parseBoolean

Dmitry Soshnikov
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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

Ben Newman
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 <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

Dmitry Soshnikov
On Thu, Mar 16, 2017 at 2:03 PM, Ben Newman <[hidden email]> 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?


Yep, this looks good.

Dmitry 

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

Qantas 94 Heavy
In reply to this post by Ben Newman
On 17 March 2017 at 08:03, Ben Newman <[hidden email]> 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;
};
```

--
- Karl Cheng (Qantas94Heavy)
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

段垚
In reply to this post by Ben Newman



在 2017/3/17 5:03, Ben Newman 写道:
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 implemention throws for invalid JSON syntax and is case-sensitive (Java's counterpart is case-insensitive). Is this better?
```
Boolean.parseBoolean = function (value) {
  return !(!value || /^false$/i.test('' + value));
};
```

However I'm not sure this is widely useful. If the value is not from JSON, there are a lot of alternatives for 'true|false', e.g. 'yes|no', 'on|off', or 'T|F', and different rules for casing/empty value/invalid syntax. So it's better to do this in users' code.


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 <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

Garrett Smith
The user's code… 

Trying to parse "" will also throw with JSON; !!"false " is true; host objects (or whatever they're called now) such as document.all.

Unsubscribing. Not sure why I didn't earlier… I need to stay a code-free user. :-) 

On Thu, Mar 16, 2017 at 7:08 PM, 段垚 <[hidden email]> wrote:



在 2017/3/17 5:03, Ben Newman 写道:
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 implemention throws for invalid JSON syntax and is case-sensitive (Java's counterpart is case-insensitive). Is this better?
```
Boolean.parseBoolean = function (value) {
  return !(!value || /^false$/i.test('' + value));
};
```

However I'm not sure this is widely useful. If the value is not from JSON, there are a lot of alternatives for 'true|false', e.g. 'yes|no', 'on|off', or 'T|F', and different rules for casing/empty value/invalid syntax. So it's better to do this in users' code.



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 <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss




--

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

Dmitry Soshnikov
In reply to this post by Qantas 94 Heavy
On Thu, Mar 16, 2017 at 7:04 PM, Karl Cheng <[hidden email]> wrote:
On 17 March 2017 at 08:03, Ben Newman <[hidden email]> 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

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

Augusto Moura
In reply to this post by Dmitry Soshnikov
What would be the result for `Boolean.parseBoolean('undefined')` and `Boolean.parseBoolean('null')`?
--
Augusto B. Moura

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

Steve Fink
In reply to this post by Dmitry Soshnikov
On 03/16/2017 09:40 PM, Dmitry Soshnikov wrote:
On Thu, Mar 16, 2017 at 7:04 PM, Karl Cheng <[hidden email]> wrote:
On 17 March 2017 at 08:03, Ben Newman <[hidden email]> 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.


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

Dmitry Soshnikov
In reply to this post by Augusto Moura


On Fri, Mar 17, 2017 at 7:42 AM Augusto Moura <[hidden email]> wrote:
What would be the result for `Boolean.parseBoolean('undefined')` and `Boolean.parseBoolean('null')`?

Perhaps this should be restricted to JSON-compatible "false" only. Other possible may include "", and "0". For stricter handling "null", "undefined", and similar strings may even throw as non-Boolean parseable.

Dmitry

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

Dmitry Soshnikov
In reply to this post by Dmitry Soshnikov
On Thu, Mar 16, 2017 at 1:55 PM, Dmitry Soshnikov <[hidden email]> 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
```



I made it plain simple (anything which is not case-insensitive `"true"` is `false`), and copy-pasted from Java.

Dmitry 

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

T.J. Crowder-2
Any reason for not just using `Boolean.parse`? (Rather than repeating `Boolean` in the function name?)

-- T.J. Crowder


On Mon, Mar 20, 2017 at 6:54 PM, Dmitry Soshnikov <[hidden email]> wrote:
On Thu, Mar 16, 2017 at 1:55 PM, Dmitry Soshnikov <[hidden email]> 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
```



I made it plain simple (anything which is not case-insensitive `"true"` is `false`), and copy-pasted from Java.

Dmitry 

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

Dmitry Soshnikov
On Mon, Mar 20, 2017 at 11:57 AM, T.J. Crowder <[hidden email]> wrote:
Any reason for not just using `Boolean.parse`? (Rather than repeating `Boolean` in the function name?)


Just a copy-paste from Java, similarly was done with `parseInt` taken from Java. But just `parse` probably would work too. For consistency and predictability `parseBoolean` is probably better now.

Dmitry

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

James Treworgy
I'd say there's no clear model for consistenty, e.g. JSON.parse. One could argue that the template is that anything which has only one way to parse is to use `parse`. Numbers have more than one way (`parseInt`, `parseFloat`)


On Mon, Mar 20, 2017 at 3:04 PM, Dmitry Soshnikov <[hidden email]> wrote:
On Mon, Mar 20, 2017 at 11:57 AM, T.J. Crowder <[hidden email]> wrote:
Any reason for not just using `Boolean.parse`? (Rather than repeating `Boolean` in the function name?)


Just a copy-paste from Java, similarly was done with `parseInt` taken from Java. But just `parse` probably would work too. For consistency and predictability `parseBoolean` is probably better now.

Dmitry

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

kdex
In reply to this post by Dmitry Soshnikov
One could argue that `parse` makes more sense, as there is really just that one way to parse a bool.

On Monday, March 20, 2017 8:04:53 PM CET Dmitry Soshnikov wrote:

> On Mon, Mar 20, 2017 at 11:57 AM, T.J. Crowder <
> [hidden email]> wrote:
>
> > Any reason for not just using `Boolean.parse`? (Rather than repeating
> > `Boolean` in the function name?)
> >
> >>
> >>
> Just a copy-paste from Java, similarly was done with `parseInt` taken from
> Java. But just `parse` probably would work too. For consistency and
> predictability `parseBoolean` is probably better now.
>
> Dmitry
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

T.J. Crowder-2
In reply to this post by James Treworgy
Funny, I'd go the other way. We have `parseInt` in part because we also have `parseFloat`. `Date` has `parse` (but it doesn't return a `Date`, sigh). `JSON` has `parse`.

For me, consistency with Java isn't important for this.

My $0.02 for what it's worth...

-- T.J. Crowder



On Mon, Mar 20, 2017 at 7:07 PM, James Treworgy <[hidden email]> wrote:
I'd say there's no clear model for consistenty, e.g. JSON.parse. One could argue that the template is that anything which has only one way to parse is to use `parse`. Numbers have more than one way (`parseInt`, `parseFloat`)


On Mon, Mar 20, 2017 at 3:04 PM, Dmitry Soshnikov <[hidden email]> wrote:
On Mon, Mar 20, 2017 at 11:57 AM, T.J. Crowder <[hidden email]> wrote:
Any reason for not just using `Boolean.parse`? (Rather than repeating `Boolean` in the function name?)


Just a copy-paste from Java, similarly was done with `parseInt` taken from Java. But just `parse` probably would work too. For consistency and predictability `parseBoolean` is probably better now.

Dmitry

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss




_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

Andrea Giammarchi-2
In reply to this post by James Treworgy
As mentioned in the gist, and FWIW, -1 here.

`/^true$/i.test(str)` works since ever for the specified use case

`Boolean.parseBoolean(1)` that returns `false` is a footgun.

Either we talk about a better definition of truthy-like values, or having a public spec about just string type and `true` as value looks like the solution for 1% of use cases that's also already covered by `JSON.parse`

Regards


On Mon, Mar 20, 2017 at 7:07 PM, James Treworgy <[hidden email]> wrote:
I'd say there's no clear model for consistenty, e.g. JSON.parse. One could argue that the template is that anything which has only one way to parse is to use `parse`. Numbers have more than one way (`parseInt`, `parseFloat`)


On Mon, Mar 20, 2017 at 3:04 PM, Dmitry Soshnikov <[hidden email]> wrote:
On Mon, Mar 20, 2017 at 11:57 AM, T.J. Crowder <[hidden email]> wrote:
Any reason for not just using `Boolean.parse`? (Rather than repeating `Boolean` in the function name?)


Just a copy-paste from Java, similarly was done with `parseInt` taken from Java. But just `parse` probably would work too. For consistency and predictability `parseBoolean` is probably better now.

Dmitry

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

Dmitry Soshnikov
In reply to this post by kdex
On Mon, Mar 20, 2017 at 12:09 PM, kdex <[hidden email]> wrote:
One could argue that `parse` makes more sense, as there is really just that one way to parse a bool.


I agree, just `parse` sounds good (similar to `Date.parse`, and `JSON.parse`). Since in `parseInt`, the `Int` is a quantifier.

Dmitry 

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

Dmitry Soshnikov
In reply to this post by Andrea Giammarchi-2
On Mon, Mar 20, 2017 at 12:12 PM, Andrea Giammarchi <[hidden email]> wrote:
As mentioned in the gist, and FWIW, -1 here.

`/^true$/i.test(str)` works since ever for the specified use case

`Boolean.parseBoolean(1)` that returns `false` is a footgun.

Either we talk about a better definition of truthy-like values, or having a public spec about just string type and `true` as value looks like the solution for 1% of use cases that's also already covered by `JSON.parse`


Still, semantics matter :) With a `JSON.parse` you may parse any JSON value, and then will have to do extra checks. RegExp test is also less semantic. Initially in the thread I considered truthy/falsey values, but then reduced to strings only, and took Java's implementation, but this can be discussed. The need for a semantic method from `Boolean` still exists, instead of using ad-hoc technics like JSON or regexp, which are just implementation details for the semantic method.

Dmitry

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Proposal: Boolean.parseBoolean

Andrea Giammarchi-2
I still think `Boolean.parse` makes sense only if gives us something more than `JSON.parse` like `/^true|yes|y|1$/i.test(value)` would do.

Otherwise I personally don't see any real-world use case for it, specially *not* those from bash or env variables.

On Mon, Mar 20, 2017 at 8:53 PM, Dmitry Soshnikov <[hidden email]> wrote:
On Mon, Mar 20, 2017 at 12:12 PM, Andrea Giammarchi <[hidden email]> wrote:
As mentioned in the gist, and FWIW, -1 here.

`/^true$/i.test(str)` works since ever for the specified use case

`Boolean.parseBoolean(1)` that returns `false` is a footgun.

Either we talk about a better definition of truthy-like values, or having a public spec about just string type and `true` as value looks like the solution for 1% of use cases that's also already covered by `JSON.parse`


Still, semantics matter :) With a `JSON.parse` you may parse any JSON value, and then will have to do extra checks. RegExp test is also less semantic. Initially in the thread I considered truthy/falsey values, but then reduced to strings only, and took Java's implementation, but this can be discussed. The need for a semantic method from `Boolean` still exists, instead of using ad-hoc technics like JSON or regexp, which are just implementation details for the semantic method.

Dmitry


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
12
Loading...