Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Change the treatment of falsy values in _.toQuery and _.fromQuery?

Aperta
#234 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
javascript
Ambito
web-dev

Direzione di ricerca

Inizia dai punti di ingresso _.toQuery e _.fromQuery ed esamina la discussione collegata della pull request. Decidi come devono essere mantenuti nel round-trip null, undefined, le stringhe vuote e lo zero numerico, incluso se si applicano le convenzioni di jQuery. Il lavoro è completato quando un comportamento concordato è documentato ed entrambe le funzioni hanno una copertura per i casi selezionati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

after modules question

@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 toQuery and fromQuery.

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']) // undefined

This might lead to a change in the behaviour of fromQuery and 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, null and 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.

Lingua principale
JavaScript
Stelle
622
Fork
114
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di documentcloud/underscore-contrib

Tutte le issue di documentcloud/underscore-contrib

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.