Skip to content

Commit 82b9168

Browse files
Cut some bytes
Simplify the JSON check to verify only the first character. Remove some functions
1 parent dfd44aa commit 82b9168

1 file changed

Lines changed: 24 additions & 31 deletions

File tree

src/js.cookie.js

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@
1717
// Browser globals
1818
var _OldCookies = window.Cookies;
1919
var api = window.Cookies = factory(window.jQuery);
20-
api.noConflict = function() {
20+
api.noConflict = function () {
2121
window.Cookies = _OldCookies;
2222
return api;
2323
};
2424
}
2525
}(function () {
26-
function decode (value) {
27-
return value.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
28-
}
29-
3026
function extend () {
3127
var i = 0;
3228
var result = {};
@@ -39,29 +35,13 @@
3935
return result;
4036
}
4137

42-
function init(converter) {
43-
var processRead = function (value, name, json) {
44-
if (value.charAt(0) === '"') {
45-
value = value.slice(1, -1);
46-
}
47-
48-
value = converter && converter(value, name) || decode(value);
49-
50-
if (json) {
51-
try {
52-
value = JSON.parse(value);
53-
} catch(e) {}
54-
}
55-
56-
return value;
57-
};
58-
var api = function (key, value, options) {
38+
function init (converter) {
39+
function api (key, value, options) {
5940
var result;
60-
var args = [].slice.call(arguments);
6141

6242
// Write
6343

64-
if (args.length > 1) {
44+
if ([].slice.call(arguments).length > 1) {
6545
options = extend(api.defaults, options);
6646

6747
if (typeof options.expires === 'number') {
@@ -72,7 +52,7 @@
7252

7353
try {
7454
result = JSON.stringify(value);
75-
if (/^(?:\{[\w\W]*\}|\[[\w\W]*\])$/.test(result)) {
55+
if (/^[\{\[]/.test(result)) {
7656
value = result;
7757
}
7858
} catch(e) {}
@@ -103,25 +83,38 @@
10383
// in case there are no cookies at all. Also prevents odd result when
10484
// calling "get()"
10585
var cookies = document.cookie ? document.cookie.split('; ') : [];
86+
var rdecode = /(%[0-9A-Z]{2})+/g;
10687
var i = 0;
10788

10889
for (; i < cookies.length; i++) {
109-
var parts = cookies[i].split('='),
110-
name = decode(parts.shift()),
111-
cookie = parts.join('=');
90+
var parts = cookies[i].split('=');
91+
var name = parts[0].replace(rdecode, decodeURIComponent);
92+
var cookie = parts.slice(1).join('=');
93+
94+
if (cookie.charAt(0) === '"') {
95+
cookie = cookie.slice(1, -1);
96+
}
97+
98+
cookie = converter && converter(cookie, name) || cookie.replace(rdecode, decodeURIComponent);
99+
100+
if (this.json) {
101+
try {
102+
cookie = JSON.parse(cookie);
103+
} catch(e) {}
104+
}
112105

113106
if (key === name) {
114-
result = processRead(cookie, name, this.json);
107+
result = cookie;
115108
break;
116109
}
117110

118111
if (!key) {
119-
result[name] = processRead(cookie, name, this.json);
112+
result[name] = cookie;
120113
}
121114
}
122115

123116
return result;
124-
};
117+
}
125118

126119
api.get = api.set = api;
127120
api.getJSON = function () {

0 commit comments

Comments
 (0)