Repository navigation
Replies: 2 comments
|
I updated the proposal. The main changes:
|
|
Two points that came up about the encoding rules. 1. Why does
|
| Written as | Read with decodeURIComponent |
Read with URLSearchParams |
|---|---|---|
%20 |
space | space |
%2B |
+ |
+ |
+ |
+ |
space |
2. Why does stringifySearch use encodeURIComponent?
Because it cannot know which rule the reader will use. encodeURIComponent writes spaces as %20 and + as %2B. It never produces a bare +, whose meaning depends on the reader, so either rule reads the same value. The URL Standard also notes that its component percent-encode set, which other standards can use to encode data embedded in a URL's path, query, or fragment, gives identical results to encodeURIComponent (URL Standard).
stringifySearch({ q: 'a b+c' }); // '?q=a%20b%2Bc'
new URLSearchParams('q=a%20b%2Bc').get('q'); // 'a b+c'
decodeURIComponent('a%20b%2Bc'); // 'a b+c'
new URLSearchParams({ q: 'a b+c' }).toString(); // 'q=a+b%2Bc'
decodeURIComponent('a+b%2Bc'); // 'a+b+c'With form encoding, as in the last two lines, a space becomes +, and a reader that uses decodeURIComponent cannot tell spaces and plus signs apart.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Summary
Add
stringifySearchtoes-toolkit/util. It converts a flat object into a string to append to a URL, such as?a=1&b=2. When there is nothing to send, it returns an empty string. It takes no options.Motivation
Passing an object directly to
URLSearchParamsproduces results that callers usually do not want.undefinedandnullare sent as the strings'undefined'and'null'.a=1%2C2.+. A receiver that decodes withdecodeURIComponentsees a literal+.?, as in'/items?', so callers have to branch on it every time.As a result, every call site repeats the same handling.
Proposed API
Detailed design
?a=1or''.new URL(url).search(URL Standard). Callers do not need to branch on an empty query.stringifySearch.?is not part of the query (RFC 3986 §3.4). Because the result includes?, the name followsurl.search.URLSearchParamscan read. Variants such as bracket notation, dot notation, and JSON are not offered as options.nullandundefined.a=means an empty string. The same applies inside arrays.String().a=1&a=2.URLSearchParams.getAll()reads this form as an array (URL Standard).JSON.stringify().encodeURIComponent.%20and+becomes%2B, sodecodeURIComponentandURLSearchParamsread the same value. The URL Standard also notes that its component percent-encode set, which other standards can use to encode data embedded in a URL's path, query, or fragment, gives identical results toencodeURIComponent(URL Standard).'2'and'10'come first in ascending order, following JavaScript's property order.interfaceand readonly arrays, and rejects nested objects,Date,null, and strings.Record<string, …>would reject objects declared withinterface.URIErrorfor unpaired surrogates.encodeURIComponentthrows (ECMA-262). It cannot be fixed without changing the value, so the error is surfaced as is.Use
.slice(1)when a string without?is needed. It returns an empty string for an empty result as well.Drawbacks
?themselves need.slice(1).a[]=1.anyand code without type checks can pass objects, which become[object Object]. There is no runtime validation.Rationale and alternatives
?.?takes one.slice(1), while adding it requires a branch.URLSearchParams.get()andgetAll()already read query strings. The only gap is collecting repeated keys into one object, so a parser can be added later if there is demand.TypeErrorfor objects.anyand untyped code.+.decodeURIComponentsees a literal+.Implementation
src/util/stringifySearch.ts,src/util/stringifySearch.spec.ts, and an export fromsrc/util/index.tsbenchmarks/performance/stringifySearch.bench.tsdocs/reference/util/,docs/ko/reference/util/,docs/ja/reference/util/, anddocs/zh_hans/reference/util/Future possibilities
parseSearch: a reading counterpart with the same rules, if there is demand.?,&, and#, so it would be a separate proposal.All reactions