Skip to content

Commit fbde23b

Browse files
Merge pull request #14 from js-cookie/few-tweaks
Few tweaks and tricks to reduce gzipped size while supporting IE8
2 parents 2847ee7 + d02fac4 commit fbde23b

2 files changed

Lines changed: 47 additions & 51 deletions

File tree

‎src/js.cookie.js‎

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,9 @@
4646
return value;
4747
}
4848

49-
function processWrite (value) {
50-
var stringified;
51-
try {
52-
stringified = JSON.stringify(value);
53-
if (/^(?:\{[\w\W]*\}|\[[\w\W]*\])$/.test(stringified)) {
54-
value = stringified;
55-
}
56-
} catch(e) {}
57-
return encode(String(value), unallowedCharsInValue);
58-
}
59-
6049
function processRead (value, converter, json) {
61-
if (value.indexOf('"') === 0) {
62-
// This is a quoted cookie as according to RFC2068, unescape...
63-
value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
50+
if (value.charAt(0) === '"') {
51+
value = value.slice(1, -1);
6452
}
6553

6654
value = decode(value, unallowedCharsInValue);
@@ -71,64 +59,72 @@
7159
} catch(e) {}
7260
}
7361

74-
return isFunction(converter) ? converter(value) : value;
62+
return converter ? converter(value) : value;
7563
}
7664

7765
function extend () {
78-
var key, options;
7966
var i = 0;
8067
var result = {};
8168
for (; i < arguments.length; i++) {
82-
options = arguments[ i ];
83-
for (key in options) {
69+
var options = arguments[ i ];
70+
for (var key in options) {
8471
result[key] = options[key];
8572
}
8673
}
8774
return result;
8875
}
8976

90-
function isFunction (obj) {
91-
return Object.prototype.toString.call(obj) === '[object Function]';
92-
}
93-
9477
var api = function (key, value, options) {
95-
var converter;
78+
var converter, result;
79+
var args = [].slice.call(arguments);
9680

97-
if (isFunction(value)) {
81+
if (typeof value === 'function') {
9882
converter = value;
99-
value = undefined;
83+
args.length = 1;
10084
}
10185

10286
// Write
10387

104-
if (arguments.length > 1 && !converter) {
88+
if (args.length > 1) {
10589
options = extend(api.defaults, options);
10690

10791
if (typeof options.expires === 'number') {
108-
var days = options.expires, t = options.expires = new Date();
109-
t.setMilliseconds(t.getMilliseconds() + days * 864e+5);
92+
var expires = new Date();
93+
expires.setMilliseconds(expires.getMilliseconds() + options.expires * 864e+5);
94+
options.expires = expires;
11095
}
11196

97+
try {
98+
result = JSON.stringify(value);
99+
if (/^(?:\{[\w\W]*\}|\[[\w\W]*\])$/.test(result)) {
100+
value = result;
101+
}
102+
} catch(e) {}
103+
104+
value = encode(String(value), unallowedCharsInValue);
105+
112106
return (document.cookie = [
113-
encode(key, unallowedCharsInName), '=', processWrite(value),
114-
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
115-
options.path ? '; path=' + options.path : '',
116-
options.domain ? '; domain=' + options.domain : '',
117-
options.secure ? '; secure' : ''
107+
encode(key, unallowedCharsInName), '=', value,
108+
options.expires && '; expires=' + options.expires.toUTCString(), // use expires attribute, max-age is not supported by IE
109+
options.path && '; path=' + options.path,
110+
options.domain && '; domain=' + options.domain,
111+
options.secure && '; secure'
118112
].join(''));
119113
}
120114

121115
// Read
122116

123-
var result = key ? undefined : {},
124-
// To prevent the for loop in the first place assign an empty array
125-
// in case there are no cookies at all. Also prevents odd result when
126-
// calling "get()".
127-
cookies = document.cookie ? document.cookie.split('; ') : [],
128-
i = 0,
129-
l = cookies.length;
117+
if (!key) {
118+
result = {};
119+
}
120+
121+
// To prevent the for loop in the first place assign an empty array
122+
// in case there are no cookies at all. Also prevents odd result when
123+
// calling "get()"
124+
var cookies = document.cookie ? document.cookie.split('; ') : [];
125+
var i = 0;
130126

131-
for (; i < l; i++) {
127+
for (; i < cookies.length; i++) {
132128
var parts = cookies[i].split('='),
133129
name = decode(parts.shift(), unallowedCharsInName),
134130
cookie = parts.join('=');
@@ -148,7 +144,7 @@
148144

149145
api.get = api.set = api;
150146
api.getJSON = function() {
151-
return api.get.apply({
147+
return api.apply({
152148
json: true
153149
}, [].slice.call(arguments));
154150
};

‎test/tests.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ test('not existing', function () {
6262
strictEqual(Cookies.get('whatever'), undefined, 'return undefined');
6363
});
6464

65-
test('RFC 2068 quoted string', function () {
66-
expect(1);
67-
document.cookie = 'c="v@address.com\\"\\\\\\""';
68-
strictEqual(Cookies.get('c'), 'v@address.com"\\"', 'should decode RFC 2068 quoted string');
69-
});
70-
7165
// github.com/carhartl/jquery-cookie/issues/50
7266
test('equality sign in cookie value', function () {
7367
expect(1);
@@ -216,7 +210,7 @@ test('defaults', function () {
216210
ok(Cookies.set('c', 'v', { path: '/bar' }).match(/path=\/bar/), 'options argument has precedence');
217211
});
218212

219-
test('Quote in the cookie value', function () {
213+
test('Handling quotes in the cookie value for read and write', function () {
220214
expect(3);
221215

222216
Cookies.set('quote', '"');
@@ -229,7 +223,13 @@ test('Quote in the cookie value', function () {
229223
strictEqual(Cookies.get('without-first'), 'content"', 'should print the quote character');
230224
});
231225

232-
test('RFC 6265 disallowed characters in cookie-octet', function () {
226+
test('RFC 6265 - cookie-octet enclosed in DQUOTE', function () {
227+
expect(1);
228+
document.cookie = 'c="v"';
229+
strictEqual(Cookies.get('c'), 'v', 'should decode the quotes');
230+
});
231+
232+
test('RFC 6265 - disallowed characters in cookie-octet', function () {
233233
expect(5);
234234

235235
Cookies.set('whitespace', ' ');
@@ -248,7 +248,7 @@ test('RFC 6265 disallowed characters in cookie-octet', function () {
248248
strictEqual(Cookies.get('multiple'), '" ,;\\" ,;\\', 'should handle multiple special characters');
249249
});
250250

251-
test('RFC 6265 disallowed characters in cookie-name', function () {
251+
test('RFC 6265 - disallowed characters in cookie-name', function () {
252252
expect(18);
253253

254254
Cookies.set('(', 'v');

0 commit comments

Comments
 (0)