Skip to content

Commit cb3925e

Browse files
author
Julien Maurel
committed
Migrate to js-cookie
1 parent 319d527 commit cb3925e

File tree

9 files changed

+201
-523
lines changed

9 files changed

+201
-523
lines changed

CHANGELOG.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
1.9.0
2+
Replace jquery.cookie (no longer maintained) by js-cookie
3+
Add gulp task to generate minified version
4+
15
1.8.1
26
Fix issue on FF with keys method
37

48
1.8.0
59
Add $.alwaysUseJsonInStorage method to always use Json to store values, even to store strings, int... at the first level
6-
10+
Broke compatibility with IE<9
11+
712
1.7.5
813
Add plugin to npm
914

Jasmine/SpecRunner.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
<!--<script type="text/javascript" src="lib/jquery-2.0.0.js"></script>-->
1212
<script type="text/javascript" src="lib/jquery-1.9.1.js"></script>
1313
<script type="text/javascript" src="lib/jasmine-jquery.js"></script>
14-
<script type="text/javascript" src="lib/json2.js"></script>
15-
<script type="text/javascript" src="../jquery.cookie.js"></script>
16-
<script type="text/javascript" src="../js.cookie.js"></script>
14+
<script type="text/javascript" src="lib/js.cookie.js"></script>
1715

1816
<!-- include source files here... -->
1917
<script type="text/javascript" src="../jquery.storageapi.js"></script>

Jasmine/lib/js.cookie.js

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*!
2+
* JavaScript Cookie v2.1.1
3+
* https://github.com/js-cookie/js-cookie
4+
*
5+
* Copyright 2006, 2015 Klaus Hartl & Fagner Brack
6+
* Released under the MIT license
7+
*/
8+
;(function (factory) {
9+
if (typeof define === 'function' && define.amd) {
10+
define(factory);
11+
} else if (typeof exports === 'object') {
12+
module.exports = factory();
13+
} else {
14+
var OldCookies = window.Cookies;
15+
var api = window.Cookies = factory();
16+
api.noConflict = function () {
17+
window.Cookies = OldCookies;
18+
return api;
19+
};
20+
}
21+
}(function () {
22+
function extend() {
23+
var i = 0;
24+
var result = {};
25+
for (; i < arguments.length; i++) {
26+
var attributes = arguments[i];
27+
for (var key in attributes) {
28+
result[key] = attributes[key];
29+
}
30+
}
31+
return result;
32+
}
33+
34+
function init(converter) {
35+
function api(key, value, attributes) {
36+
var result;
37+
if (typeof document === 'undefined') {
38+
return;
39+
}
40+
41+
// Write
42+
43+
if (arguments.length > 1) {
44+
attributes = extend({
45+
path: '/'
46+
}, api.defaults, attributes);
47+
48+
if (typeof attributes.expires === 'number') {
49+
var expires = new Date();
50+
expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
51+
attributes.expires = expires;
52+
}
53+
54+
try {
55+
result = JSON.stringify(value);
56+
if (/^[\{\[]/.test(result)) {
57+
value = result;
58+
}
59+
} catch (e) {
60+
}
61+
62+
if (!converter.write) {
63+
value = encodeURIComponent(String(value))
64+
.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
65+
} else {
66+
value = converter.write(value, key);
67+
}
68+
69+
key = encodeURIComponent(String(key));
70+
key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
71+
key = key.replace(/[\(\)]/g, escape);
72+
73+
return (document.cookie = [
74+
key, '=', value,
75+
attributes.expires && '; expires=' + attributes.expires.toUTCString(), // use expires attribute, max-age is not supported by IE
76+
attributes.path && '; path=' + attributes.path,
77+
attributes.domain && '; domain=' + attributes.domain,
78+
attributes.secure ? '; secure' : ''
79+
].join(''));
80+
}
81+
82+
// Read
83+
84+
if (!key) {
85+
result = {};
86+
}
87+
88+
// To prevent the for loop in the first place assign an empty array
89+
// in case there are no cookies at all. Also prevents odd result when
90+
// calling "get()"
91+
var cookies = document.cookie ? document.cookie.split('; ') : [];
92+
var rdecode = /(%[0-9A-Z]{2})+/g;
93+
var i = 0;
94+
95+
for (; i < cookies.length; i++) {
96+
var parts = cookies[i].split('=');
97+
var name = parts[0].replace(rdecode, decodeURIComponent);
98+
var cookie = parts.slice(1).join('=');
99+
100+
if (cookie.charAt(0) === '"') {
101+
cookie = cookie.slice(1, -1);
102+
}
103+
104+
try {
105+
cookie = converter.read ?
106+
converter.read(cookie, name) : converter(cookie, name) ||
107+
cookie.replace(rdecode, decodeURIComponent);
108+
109+
if (this.json) {
110+
try {
111+
cookie = JSON.parse(cookie);
112+
} catch (e) {
113+
}
114+
}
115+
116+
if (key === name) {
117+
result = cookie;
118+
break;
119+
}
120+
121+
if (!key) {
122+
result[name] = cookie;
123+
}
124+
} catch (e) {
125+
}
126+
}
127+
128+
return result;
129+
}
130+
131+
api.set = api;
132+
api.get = function (key) {
133+
return api(key);
134+
};
135+
api.getJSON = function () {
136+
return api.apply({
137+
json: true
138+
}, [].slice.call(arguments));
139+
};
140+
api.defaults = {};
141+
142+
api.remove = function (key, attributes) {
143+
api(key, '', extend(attributes, {
144+
expires: -1
145+
}));
146+
};
147+
148+
api.withConverter = init;
149+
150+
return api;
151+
}
152+
153+
return init(function () {
154+
});
155+
}));

0 commit comments

Comments
 (0)