-
Notifications
You must be signed in to change notification settings - Fork 4k
Handle batch object cookie read/write operations #123
Changes from all commits
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 |
|---|---|---|
|
|
@@ -21,6 +21,14 @@ | |
|
|
||
| var config = $.cookie = function (key, value, options) { | ||
|
|
||
| if (typeof key === 'object') { | ||
| for (var name in key) { | ||
| if (!key.hasOwnProperty(name)) continue; | ||
| $.cookie(name, key[name], value); // "options" is optional 2nd arg here | ||
| } | ||
| return key; | ||
|
Contributor
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. Why return
Author
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. If you followed the flow of the code, key here is the object of key: value pairs you just set, so you can use it just as any other assignment in javascript, e g: a = b = { x: 1, y: 2 }; sets both a and b to the given object. If you insert a $.cookie call anywhere in that assignment chain, this makes it still work, which is expected, intuitive and useful behaviour.
Contributor
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 know what the value of $.cookie('a', 'aaa', options); // returns undefined
$.cookie({a: 'aaa', b: 'bbb', options); // returns pairs object by referenceAside from it making the return value inconsistent based on invocation signature, it is somewhat counter-intuitive. Why would it return the pairs object? I'd expect maybe a boolean return on whether the action was successful, or maybe the expiration date of the set cookies, or perhaps the newly formed cookie-syntax string. But not the pairs object. All that just to save a literal assignment? var values = $.cookie({a: 'aaa', b: 'bbb'}, options);
// Instead of:
var values = {a: 'aaa', b: 'bbb'};
$.cookie(values, options);
Collaborator
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. Since the issue here is the batch cookie operation I think this return change should be threated in another PR.
Author
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. It is documented higher up in the pull request. You are all welcome to make your own pull requests based on this one changing as much as you like to whatever you personally deem more useful or intuitive. Personally, my opinion stands as is, and also, I don't plan to have computerized net access for most of the month of December anyway, so little will change here. That said, I don't believe the repository maintainer has the time or interest in accepting pull requests; this and other good patches have been sitting untouched rather long. (Yes. The whole pull request is to save myself and others writing this assignment loop and storing away the key:val pairs object outside of $.cookie.)
Owner
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. Interest yes, time hardly.
Owner
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. And regarding this PR in particular, I am still reluctant to add it, since I'd like to see whether there is greater popular demand, otherwise I'd like to keep the codebase as light as possible. |
||
| } | ||
|
|
||
| // write | ||
| if (value !== undefined) { | ||
| options = $.extend({}, config.defaults, options); | ||
|
|
@@ -46,17 +54,26 @@ | |
| } | ||
|
|
||
| // read | ||
| var all = arguments.length ? null : {}; | ||
| var decode = config.raw ? raw : decoded; | ||
| var cookies = document.cookie.split('; '); | ||
| for (var i = 0, l = cookies.length; i < l; i++) { | ||
| var parts = cookies[i].split('='); | ||
| if (decode(parts.shift()) === key) { | ||
| if ((name = decode(parts.shift())) === key || all) { | ||
| var cookie = decode(parts.join('=')); | ||
| return config.json ? JSON.parse(cookie) : cookie; | ||
| if (config.json) { | ||
| cookie = JSON.parse(cookie); | ||
| } | ||
| if (all) { | ||
| all[name] = cookie; | ||
| } | ||
| else { | ||
| return cookie; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| return all; | ||
| }; | ||
|
|
||
| config.defaults = {}; | ||
|
|
||
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.
I think
$.isPlainObjectwould fit better here...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.
Unless there's a supported, intuitive use case for passing a null key, that just makes the code longer and slower.
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.
I believe performance is not an issue here since the check is not in a loop.
And
typeof a==='object'has one more char than$.isPlainObject(a)oO