Skip to content

Commit 3ec5018

Browse files
authored
Merge pull request js-cookie#401 from carhartl/reducing-bytes
Reduce bytes
2 parents 6e42d24 + a7ab62c commit 3ec5018

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

src/js.cookie.js

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Released under the MIT license
77
*/
88
;(function (factory) {
9-
var registeredInModuleLoader = false;
9+
var registeredInModuleLoader;
1010
if (typeof define === 'function' && define.amd) {
1111
define(factory);
1212
registeredInModuleLoader = true;
@@ -38,7 +38,6 @@
3838

3939
function init (converter) {
4040
function api (key, value, attributes) {
41-
var result;
4241
if (typeof document === 'undefined') {
4342
return;
4443
}
@@ -51,31 +50,27 @@
5150
}, api.defaults, attributes);
5251

5352
if (typeof attributes.expires === 'number') {
54-
var expires = new Date();
55-
expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
56-
attributes.expires = expires;
53+
attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5);
5754
}
5855

5956
// We're using "expires" because "max-age" is not supported by IE
6057
attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
6158

6259
try {
63-
result = JSON.stringify(value);
60+
var result = JSON.stringify(value);
6461
if (/^[\{\[]/.test(result)) {
6562
value = result;
6663
}
6764
} catch (e) {}
6865

69-
if (!converter.write) {
70-
value = encodeURIComponent(String(value))
66+
value = converter.write ?
67+
converter.write(value, key) :
68+
encodeURIComponent(String(value))
7169
.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
72-
} else {
73-
value = converter.write(value, key);
74-
}
7570

76-
key = encodeURIComponent(String(key));
77-
key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
78-
key = key.replace(/[\(\)]/g, escape);
71+
key = encodeURIComponent(String(key))
72+
.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)
73+
.replace(/[\(\)]/g, escape);
7974

8075
var stringifiedAttributes = '';
8176
for (var attributeName in attributes) {
@@ -102,15 +97,13 @@
10297

10398
// Read
10499

105-
if (!key) {
106-
result = {};
107-
}
108-
100+
var jar = {};
101+
var decode = function (s) {
102+
return s.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
103+
};
109104
// To prevent the for loop in the first place assign an empty array
110-
// in case there are no cookies at all. Also prevents odd result when
111-
// calling "get()"
105+
// in case there are no cookies at all.
112106
var cookies = document.cookie ? document.cookie.split('; ') : [];
113-
var rdecode = /(%[0-9A-Z]{2})+/g;
114107
var i = 0;
115108

116109
for (; i < cookies.length; i++) {
@@ -122,29 +115,25 @@
122115
}
123116

124117
try {
125-
var name = parts[0].replace(rdecode, decodeURIComponent);
126-
cookie = converter.read ?
127-
converter.read(cookie, name) : converter(cookie, name) ||
128-
cookie.replace(rdecode, decodeURIComponent);
118+
var name = decode(parts[0]);
119+
cookie = (converter.read || converter)(cookie, name) ||
120+
decode(cookie);
129121

130122
if (this.json) {
131123
try {
132124
cookie = JSON.parse(cookie);
133125
} catch (e) {}
134126
}
135127

128+
jar[name] = cookie;
129+
136130
if (key === name) {
137-
result = cookie;
138131
break;
139132
}
140-
141-
if (!key) {
142-
result[name] = cookie;
143-
}
144133
} catch (e) {}
145134
}
146135

147-
return result;
136+
return key ? jar[key] : jar;
148137
}
149138

150139
api.set = api;
@@ -154,7 +143,7 @@
154143
api.getJSON = function () {
155144
return api.apply({
156145
json: true
157-
}, [].slice.call(arguments));
146+
}, arguments);
158147
};
159148
api.remove = function (key, attributes) {
160149
api(key, '', extend(attributes, {

0 commit comments

Comments
 (0)