Skip to content

Commit 0016877

Browse files
committed
allow passing String objects, allow undefined as argument when deleting
1 parent 26f9f67 commit 0016877

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

‎jquery.cookie.js‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,25 @@
5858
* @author Klaus Hartl/klaus.hartl@stilbuero.de
5959
*/
6060
jQuery.cookie = function (key, value, options) {
61-
62-
// key and value given, set cookie...
63-
if (arguments.length > 1 && (value === null || typeof value !== "object")) {
61+
62+
// key and at least value given, set cookie...
63+
if (arguments.length > 1 && String(value) !== "[object Object]") {
6464
options = jQuery.extend({}, options);
6565

66-
if (value === null) {
66+
if (value === null || value === undefined) {
6767
options.expires = -1;
6868
}
6969

7070
if (typeof options.expires === 'number') {
7171
var days = options.expires, t = options.expires = new Date();
7272
t.setDate(t.getDate() + days);
7373
}
74-
74+
75+
value = String(value);
76+
7577
return (document.cookie = [
7678
encodeURIComponent(key), '=',
77-
options.raw ? String(value) : encodeURIComponent(String(value)),
79+
options.raw ? value : encodeURIComponent(value),
7880
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
7981
options.path ? '; path=' + options.path : '',
8082
options.domain ? '; domain=' + options.domain : '',

‎test.js‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ test('raw: true', 1, function () {
3434

3535
module('write', before);
3636

37-
test('simple value', 2, function () {
38-
var cookie = $.cookie('c', 'v');
39-
equals(cookie, 'c=v', 'should return written cookie string');
40-
equals(document.cookie, 'c=v', 'should write String primitive');
37+
test('String primitive', 1, function () {
38+
$.cookie('c', 'v');
39+
equals(document.cookie, 'c=v', 'should write value');
40+
});
41+
42+
test('String object', 1, function () {
43+
$.cookie('c', new String('v'));
44+
equals(document.cookie, 'c=v', 'should write value');
45+
});
46+
47+
test('return', 1, function () {
48+
equals($.cookie('c', 'v'), 'c=v', 'should return written cookie string');
4149
});
4250

4351
test('raw: true', 1, function () {
@@ -48,8 +56,12 @@ test('raw: true', 1, function () {
4856

4957
module('delete', before);
5058

51-
test('delete', 1, function () {
59+
test('delete', 2, function () {
5260
document.cookie = 'c=v';
5361
$.cookie('c', null);
5462
equals(document.cookie, '', 'should delete with null as value');
63+
64+
document.cookie = 'c=v';
65+
$.cookie('c', undefined);
66+
equals(document.cookie, '', 'should delete with undefined as value');
5567
});

0 commit comments

Comments
 (0)