URI.js icon indicating copy to clipboard operation
URI.js copied to clipboard

Parameter check does not work as expected

Open outaTiME opened this issue 9 years ago • 5 comments

Hi, pal ...

Im doing something wrong here ? why truthy / falsy value check does not work as expected here my test:

URI('http://test.com?boolean=true').hasQuery('boolean', true);
// true (right)

URI('http://test.com?boolean=false').hasQuery('boolean', true);
// true (it might be false or not?)

thks !!!

outaTiME avatar Sep 05 '16 19:09 outaTiME

I see how this can be misleading… URI.js simply converts the value from the query string to boolean via Boolean(value). And the problem here is Boolean('false') === true, because any non-empty string is truthy. If we were to treat this as a bug, we'd have to consider a few strings might be considered falsy: "0", "-0", "+0", "false", "null", "undefined".

rodneyrehm avatar Sep 05 '16 20:09 rodneyrehm

Understood ...

The falsy string appear to be correct. What you want to do? leave it as it is now? or treat as a bug?

outaTiME avatar Sep 05 '16 20:09 outaTiME

What you want to do? leave it as it is now? or treat as a bug?

If there's enough feedback to this issue - i.e. people considering this a problem - we can fix it. But for now I'd not consider this a bug, so I'd lean to leaving it as it is.

rodneyrehm avatar Sep 05 '16 20:09 rodneyrehm

Great, thks !!!

outaTiME avatar Sep 05 '16 20:09 outaTiME

I also found this confusing. Given the method's description in the docs, I expected that URI.js would parse the string and perform the truthy/falsy conversion itself (since "false"/"0"/"null" are all values I would want to consider as falsy in this context).

Since any value that isn't "" would evaluate as truthy; would it be possible to change the documentation description of the second .hasQuery('boolean', true) method from:

// check if parameter has a truthy / falsy value

to:

// check if parameter has a value (regardless of value)

BenJenkinson avatar Dec 05 '17 15:12 BenJenkinson