jshttp / negotiator

An HTTP content negotiator for Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] More preferredCharset() stuff

AnyhowStep opened this issue · comments

preferredCharsets("first,second,third")
//Actual Output: ["first", "second", "third"]

preferredCharsets("first,second,third", [])
//Actual Output: []
//Should the output, instead be, ["first", "second", "third"] ?

I feel like passing undefined and passing an empty array should mean the same thing. But, I'm not sure how charset negotiation works.

if (!provided) {


This other question is more contrived,

preferredCharsets("SECOND,first,second,third", ["SeCoNd", "first"])
//Actual Output: ["first", "SeCoNd"]
//Should the output, instead be, ["SeCoNd", "first"] ?

I'm looking at this line, in particular,

if (spec && (priority.s - spec.s || priority.q - spec.q || priority.o - spec.o) < 0) {

It says, priority.o - spec.o. So, getCharsetPriority() will set the o value of "SeCoNd" to 2, the index of "second", instead of 0, the index of "SECOND".

If we change that line to spec.o - priority.o, the o value of "SeCoNd" becomes 0, and will come before "first".

But, of course, realistically, who's going to send such a header where there are duplicate charsets, and they're not consecutive, with lower-priority available charsets requested in between?


You shouldn't have to do anything about the second question. I just like reading random pieces of code and poking at them :x

I feel like passing undefined and passing an empty array should mean the same thing. But, I'm not sure how charset negotiation works.

So it's not that you're passing undefined in this API: it's that those are two different function signatures (https://github.com/jshttp/negotiator#methods-2):

charset()
Returns the most preferred charset from the client.

and

charset(availableCharsets)
Returns the most preferred charset from a list of available charsets.

Oh. I should pay more attention to the documentation than the internals...
Thank you