Skip to content

Commit 78a3b44

Browse files
Few tweaks and tricks to reduce gzipped size while supporting IE8
1 parent 2847ee7 commit 78a3b44

1 file changed

Lines changed: 37 additions & 40 deletions

File tree

‎src/js.cookie.js‎

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,8 @@
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) {
50+
if (value.charAt(0) === '"') {
6251
// This is a quoted cookie as according to RFC2068, unescape...
6352
value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
6453
}
@@ -71,64 +60,72 @@
7160
} catch(e) {}
7261
}
7362

74-
return isFunction(converter) ? converter(value) : value;
63+
return converter ? converter(value) : value;
7564
}
7665

7766
function extend () {
78-
var key, options;
7967
var i = 0;
8068
var result = {};
8169
for (; i < arguments.length; i++) {
82-
options = arguments[ i ];
83-
for (key in options) {
70+
var options = arguments[ i ];
71+
for (var key in options) {
8472
result[key] = options[key];
8573
}
8674
}
8775
return result;
8876
}
8977

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

97-
if (isFunction(value)) {
82+
if (typeof value === 'function') {
9883
converter = value;
99-
value = undefined;
84+
args.length = 1;
10085
}
10186

10287
// Write
10388

104-
if (arguments.length > 1 && !converter) {
89+
if (args.length > 1) {
10590
options = extend(api.defaults, options);
10691

10792
if (typeof options.expires === 'number') {
108-
var days = options.expires, t = options.expires = new Date();
109-
t.setMilliseconds(t.getMilliseconds() + days * 864e+5);
93+
var expires = new Date();
94+
expires.setMilliseconds(expires.getMilliseconds() + options.expires * 864e+5);
95+
options.expires = expires;
11096
}
11197

98+
try {
99+
result = JSON.stringify(value);
100+
if (/^(?:\{[\w\W]*\}|\[[\w\W]*\])$/.test(result)) {
101+
value = result;
102+
}
103+
} catch(e) {}
104+
105+
value = encode(String(value), unallowedCharsInValue);
106+
112107
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' : ''
108+
encode(key, unallowedCharsInName), '=', value,
109+
options.expires && '; expires=' + options.expires.toUTCString(), // use expires attribute, max-age is not supported by IE
110+
options.path && '; path=' + options.path,
111+
options.domain && '; domain=' + options.domain,
112+
options.secure && '; secure'
118113
].join(''));
119114
}
120115

121116
// Read
122117

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;
118+
if (!key) {
119+
result = {};
120+
}
121+
122+
// To prevent the for loop in the first place assign an empty array
123+
// in case there are no cookies at all. Also prevents odd result when
124+
// calling "get()"
125+
var cookies = document.cookie ? document.cookie.split('; ') : [];
126+
var i = 0;
130127

131-
for (; i < l; i++) {
128+
for (; i < cookies.length; i++) {
132129
var parts = cookies[i].split('='),
133130
name = decode(parts.shift(), unallowedCharsInName),
134131
cookie = parts.join('=');
@@ -148,7 +145,7 @@
148145

149146
api.get = api.set = api;
150147
api.getJSON = function() {
151-
return api.get.apply({
148+
return api.apply({
152149
json: true
153150
}, [].slice.call(arguments));
154151
};

0 commit comments

Comments
 (0)