-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialize objects of key/value pairs #47
base: master
Are you sure you want to change the base?
Changes from 3 commits
4d40c32
591675e
7df89fa
09ffac2
93b7c83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,12 +82,23 @@ function parse(str, options) { | |
/** | ||
* Serialize data into a cookie header. | ||
* | ||
* Serialize the a name value pair into a cookie string suitable for | ||
* http headers. An optional options object specified cookie parameters. | ||
* If the first parameter is an object, serialize the key-value pairs | ||
* in the object into a cookie string suitable for http headers. An | ||
* optional options object can be used to specify the encoding. If only | ||
* one key-value pair is in the first parmater, then the options object can also | ||
* specify cookie parameters. If more than one key-value pairs are in the | ||
* first parameter, then the options object may only specify an encoding. | ||
* | ||
* If the first parameter is a string, serialize the name value pair | ||
* into a cookie string suitable for http headers. An optional options | ||
* object specifies cookie parameters and encoding. | ||
* | ||
* serialize('foo', 'bar', { httpOnly: true }) | ||
* => "foo=bar; httpOnly" | ||
* | ||
* serialize({ foo: 'bar', cat: 'meow' }) | ||
* => "foo=bar; cat=meow" | ||
* | ||
* @param {string} name | ||
* @param {string} val | ||
* @param {object} [options] | ||
|
@@ -96,6 +107,50 @@ function parse(str, options) { | |
*/ | ||
|
||
function serialize(name, val, options) { | ||
if( typeof name === 'object') { | ||
var cookies = name; | ||
var serializeOptions = val; | ||
|
||
var cookieNames = Object.keys(cookies); | ||
if(0 === cookieNames.length) { | ||
return ''; | ||
} else if(cookieNames.length > 1) { | ||
// If there are more than one cookies to serialize, only allow | ||
// an encoding option to be set | ||
var opt = serializeOptions || {}; | ||
serializeOptions = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't really make any sense. People supplying the object as a variable would have no way to understand if their options are going to be honored. Besides, it seems perfectly valid to want to set all the cookies to have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could be wrong about this, but my thinking was that if we are given several cookies to serialize, they want those cookies in the One possible option is: given an object with multiple cookies and an options object with cookie parameters we can return an array of serialized cookie strings, one for each cookie, with all the parameters set as could be used in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, so if we are given multiple cookies to serialize, would it be best to return an array of serialized cookie strings rather than a single string with all cookies? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, that makes sense :) |
||
encode: opt.encode || encode | ||
}; | ||
} | ||
|
||
var serializedCookies = []; | ||
cookieNames.forEach(function(cookieName) { | ||
serializedCookies.push(serializeNameValue(cookieName, cookies[cookieName], serializeOptions)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already know the number of cookies ahead time time; forcing the runtime to keep expanding the array is not very efficient. |
||
}); | ||
|
||
return serializedCookies.join('; '); | ||
} else { | ||
return serializeNameValue(name, val, options); | ||
} | ||
} | ||
|
||
/** | ||
* Serialize name value pair into a cookie header. | ||
* | ||
* Serialize the a name value pair into a cookie string suitable for | ||
* http headers. An optional options object specified cookie parameters. | ||
* | ||
* serialize('foo', 'bar', { httpOnly: true }) | ||
* => "foo=bar; httpOnly" | ||
* | ||
* @param {string} name | ||
* @param {string} val | ||
* @param {object} [options] | ||
* @return {string} | ||
* @private | ||
*/ | ||
|
||
function serializeNameValue(name, val, options) { | ||
var opt = options || {}; | ||
var enc = opt.encode || encode; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps return
undefined
for this case? Not too clear, but just feel like setting theSet-Cookie
header to the result would make more sense ifundefined
was returned, since that would just not send the header instead of send an empty header.