URI.js
URI.js copied to clipboard
Parameter check does not work as expected
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 !!!
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".
Understood ...
The falsy string appear to be correct. What you want to do? leave it as it is now? or treat as a bug?
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.
Great, thks !!!
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)