Skip to content

Commit 593636a

Browse files
author
carhartl
committed
refactoring: reduce amount of code remarkably
1 parent c0f510b commit 593636a

2 files changed

Lines changed: 40 additions & 49 deletions

File tree

‎jquery.cookie.js‎

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
/*jslint browser: true */ /*global jQuery: true */
2+
13
/**
24
* Cookie plugin
35
*
4-
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
6+
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
57
* Dual licensed under the MIT and GPL licenses:
68
* http://www.opensource.org/licenses/mit-license.php
79
* http://www.gnu.org/licenses/gpl.html
810
*
911
*/
1012

13+
// TODO JsDoc
14+
1115
/**
12-
* Create a cookie with the given name and value and other optional parameters.
16+
* Create a cookie with the given key and value and other optional parameters.
1317
*
1418
* @example $.cookie('the_cookie', 'the_value');
1519
* @desc Set the value of a cookie.
@@ -21,7 +25,7 @@
2125
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
2226
* used when the cookie was set.
2327
*
24-
* @param String name The name of the cookie.
28+
* @param String key The key of the cookie.
2529
* @param String value The value of the cookie.
2630
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
2731
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
@@ -40,58 +44,43 @@
4044
*/
4145

4246
/**
43-
* Get the value of a cookie with the given name.
47+
* Get the value of a cookie with the given key.
4448
*
4549
* @example $.cookie('the_cookie');
4650
* @desc Get the value of a cookie.
4751
*
48-
* @param String name The name of the cookie.
52+
* @param String key The key of the cookie.
4953
* @return The value of the cookie.
5054
* @type String
5155
*
5256
* @name $.cookie
5357
* @cat Plugins/Cookie
5458
* @author Klaus Hartl/klaus.hartl@stilbuero.de
5559
*/
56-
jQuery.cookie = function(name, value, options) {
57-
if (typeof value != 'undefined') { // name and value given, set cookie
58-
options = options || {};
60+
jQuery.cookie = function (key, value, options) {
61+
62+
// key and value given, set cookie...
63+
if (arguments.length > 1) {
64+
options = jQuery.extend({}, options);
65+
5966
if (value === null) {
60-
value = '';
61-
options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
6267
options.expires = -1;
6368
}
64-
var expires = '';
65-
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
66-
var date;
67-
if (typeof options.expires == 'number') {
68-
date = new Date();
69-
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
70-
} else {
71-
date = options.expires;
72-
}
73-
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
74-
}
75-
// NOTE Needed to parenthesize options.path and options.domain
76-
// in the following expressions, otherwise they evaluate to undefined
77-
// in the packed version for some reason...
78-
var path = options.path ? '; path=' + (options.path) : '';
79-
var domain = options.domain ? '; domain=' + (options.domain) : '';
80-
var secure = options.secure ? '; secure' : '';
81-
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
82-
} else { // only name given, get cookie
83-
var cookieValue = null;
84-
if (document.cookie && document.cookie != '') {
85-
var cookies = document.cookie.split(';');
86-
for (var i = 0; i < cookies.length; i++) {
87-
var cookie = jQuery.trim(cookies[i]);
88-
// Does this cookie string begin with the name we want?
89-
if (cookie.substring(0, name.length + 1) == (name + '=')) {
90-
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
91-
break;
92-
}
93-
}
69+
70+
if (typeof options.expires === 'number') {
71+
var days = options.expires, t = options.expires = new Date();
72+
t.setDate(t.getDate() + days);
9473
}
95-
return cookieValue;
74+
75+
return (document.cookie = [key, '=', encodeURIComponent(String(value)),
76+
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
77+
options.path ? '; path=' + options.path : '',
78+
options.domain ? '; domain=' + options.domain : '',
79+
options.secure ? '; secure' : ''
80+
].join(''));
9681
}
97-
};
82+
83+
// get cookie...
84+
var result;
85+
return (result = new RegExp('(?:^|; )' + key + '=([^;]*)').exec(document.cookie)) ? decodeURIComponent(result[1]) : null;
86+
};

‎test/test.js‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,31 @@ var before = {
77
module('read', before);
88

99
test('Read', function () {
10-
expect(2);
10+
expect(3);
1111

1212
equals($.cookie('c'), null);
1313
document.cookie = 'c=v';
14-
equals($.cookie('c'), 'v', 'Should read cookie value');
14+
equals($.cookie('c'), 'v', 'Read cookie value');
15+
equals($.cookie('whatever'), null, 'Give null if cookie does not exist');
1516
});
1617

1718
module('Write', before);
1819

1920
test('Write', function () {
20-
expect(2);
21+
expect(3);
2122

2223
equals($.cookie('c'), null);
23-
$.cookie('c', 'v');
24-
equals(document.cookie, 'c=v', 'Should write cookie value');
24+
var cookie = $.cookie('c', 'v');
25+
equals(document.cookie, 'c=v', 'Write cookie value');
26+
equals(cookie, 'c=v', 'Return cookie');
2527
});
2628

2729
module('Delete', before);
2830

2931
test('Delete', function () {
3032
expect(1);
3133

32-
document.cookie = "c=v";
34+
document.cookie = 'c=v';
3335
$.cookie('c', null);
34-
equals(document.cookie, '', 'Should delete cookie');
36+
equals(document.cookie, '', 'Delete cookie');
3537
});

0 commit comments

Comments
 (0)