Skip to content

Commit

Permalink
misc refactor of parseCookie()
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 29, 2011
1 parent 929c31c commit 8abc31c
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/utils.js
Expand Up @@ -136,13 +136,11 @@ exports.parseCookie = function(str){
, key = pair.substr(0, eqlIndex).trim().toLowerCase()
, val = pair.substr(++eqlIndex, pair.length).trim();

// Quoted values
if (val[0] === '"') {
val = val.slice(1, -1);
}
// quoted values
if ('"' == val[0]) val = val.slice(1, -1);

// Only assign once
if (obj[key] === undefined) {
// only assign once
if (undefined == obj[key]) {

This comment has been minimized.

Copy link
@frank06

frank06 Jun 29, 2011

i've seen this style elsewhere too (undefined == variable). wondering what the motivation is... why not variable == undefined?

This comment has been minimized.

Copy link
@tj

tj Jun 29, 2011

Author Member

just a preference. i prefer to read the expectation first, though in the case of undefined specifically it could be very bad haha if you forget a =. Typically it's done this way so that for example 1 == foo will fail on 1 = foo, whereas foo = 1 will not fail but lead to issues if not intended

This comment has been minimized.

Copy link
@frank06

frank06 Jun 29, 2011

cool, makes sense

val = val.replace(/\+/g, ' ');

This comment has been minimized.

Copy link
@Halicea

Halicea Jun 18, 2012

Can you explain me the reason why is + replaced with whitespace? it is causing trouble in my app(when there is a + sign in the sessionID in the cookie it cant find the appropriate session due to this line. i have changed it in my code , but i'm sure i'm doing something wrong.

try {
obj[key] = decodeURIComponent(val);
Expand Down

0 comments on commit 8abc31c

Please sign in to comment.