Change the treatment of falsy values in _.toQuery and _.fromQuery?
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- javascript
- Domain
- web-dev
Research direction
Start with the _.toQuery and _.fromQuery entry points and review the linked pull-request discussion. Decide how null, undefined, empty strings, and numeric zero should round-trip, including whether jQuery conventions apply. Done means an agreed behavior is documented and both functions have coverage for the selected cases.
Written by the indexing model from the issue text.
Description
@yashshah1 pointed out a problem with _.toQuery and _.fromQuery: through stringification, falsy values are being "upgraded" to truthy if you encode them and then decode them again. For example, _.fromQuery(_.toQuery({a: null})) returns {a: 'null'}.
@yashshah1 proposed the following solution in https://github.com/documentcloud/underscore-contrib/pull/229#issuecomment-683548408:
What I am suggesting is a change in both
toQueryandfromQuery.Proposed behaviour:
_.toQuery('{ a: '', b: null, c: undefined, d: '10' }') // a=&b=&d=10 _.fromQuery('a=&b=&d=10') // { a: '', b: '', d: '10' }Reasoning
The idea is that toQuery and fromQuery need to be as complementary to each other as possible, one drawback that's seen already is that numbers need to be re-parsed. Wrt falsy values, however, I think there has to be a special provision as described above.What we gain by this is that null values are re-encoded as empty strings, which is falsy, and undefined isn't included in the object, which leads to almost the same usage in a few cases.
var obj = { a: '', b: null, c: undefined, d: '10' } console.log(obj['c']) // undefined obj = _.fromQuery(_.toQuery(obj)) console.log(obj['c']) // undefinedThis might lead to a change in the behaviour of
fromQueryand I am happy to raise a PR should this be acceptable.
I'm not sure whether this approach would be the right solution, for a couple of reasons:
- Information loss:
undefined,nulland the empty string''could no longer be distinguished. - What to do with numeric zero?
- How does this align with jQuery's encoding conventions?
However, this is something we can discuss. Let's do that here.
- Dominant language
- JavaScript
- Stars
- 622
- Forks
- 114
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from documentcloud/underscore-contrib
-
Boolean helperOpen
Difficulty 3/5 1-2 days Newbie friendliness 38/100
documentcloud/underscore-contrib#254 · 2 comments ·
-
breaking change bug
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
documentcloud/underscore-contrib#252 · 7 comments ·
-
after modules enhancement
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
after modules docs enhancement
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
after modules enhancement
Difficulty 5/5 Over a week Newbie friendliness 32/100
documentcloud/underscore-contrib#225 · 8 comments ·
All issues in documentcloud/underscore-contrib
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
type/bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Edit: RTE News LogoOpencheck:failed logos:edit
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
iptv-org/database#36354 · 1 comment ·
Maintainers usually reply within 4 days
-
status:untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
midnightntwrk/midnight-wallet#791 ·
Maintainers usually reply within 1 day