|
6 | 6 | * Released under the MIT license |
7 | 7 | */ |
8 | 8 | ;(function (factory) { |
9 | | - var registeredInModuleLoader = false; |
| 9 | + var registeredInModuleLoader; |
10 | 10 | if (typeof define === 'function' && define.amd) { |
11 | 11 | define(factory); |
12 | 12 | registeredInModuleLoader = true; |
|
38 | 38 |
|
39 | 39 | function init (converter) { |
40 | 40 | function api (key, value, attributes) { |
41 | | - var result; |
42 | 41 | if (typeof document === 'undefined') { |
43 | 42 | return; |
44 | 43 | } |
|
51 | 50 | }, api.defaults, attributes); |
52 | 51 |
|
53 | 52 | 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); |
57 | 54 | } |
58 | 55 |
|
59 | 56 | // We're using "expires" because "max-age" is not supported by IE |
60 | 57 | attributes.expires = attributes.expires ? attributes.expires.toUTCString() : ''; |
61 | 58 |
|
62 | 59 | try { |
63 | | - result = JSON.stringify(value); |
| 60 | + var result = JSON.stringify(value); |
64 | 61 | if (/^[\{\[]/.test(result)) { |
65 | 62 | value = result; |
66 | 63 | } |
67 | 64 | } catch (e) {} |
68 | 65 |
|
69 | | - if (!converter.write) { |
70 | | - value = encodeURIComponent(String(value)) |
| 66 | + value = converter.write ? |
| 67 | + converter.write(value, key) : |
| 68 | + encodeURIComponent(String(value)) |
71 | 69 | .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 | | - } |
75 | 70 |
|
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); |
79 | 74 |
|
80 | 75 | var stringifiedAttributes = ''; |
81 | 76 | for (var attributeName in attributes) { |
|
102 | 97 |
|
103 | 98 | // Read |
104 | 99 |
|
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 | + }; |
109 | 104 | // 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. |
112 | 106 | var cookies = document.cookie ? document.cookie.split('; ') : []; |
113 | | - var rdecode = /(%[0-9A-Z]{2})+/g; |
114 | 107 | var i = 0; |
115 | 108 |
|
116 | 109 | for (; i < cookies.length; i++) { |
|
122 | 115 | } |
123 | 116 |
|
124 | 117 | 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); |
129 | 121 |
|
130 | 122 | if (this.json) { |
131 | 123 | try { |
132 | 124 | cookie = JSON.parse(cookie); |
133 | 125 | } catch (e) {} |
134 | 126 | } |
135 | 127 |
|
| 128 | + jar[name] = cookie; |
| 129 | + |
136 | 130 | if (key === name) { |
137 | | - result = cookie; |
138 | 131 | break; |
139 | 132 | } |
140 | | - |
141 | | - if (!key) { |
142 | | - result[name] = cookie; |
143 | | - } |
144 | 133 | } catch (e) {} |
145 | 134 | } |
146 | 135 |
|
147 | | - return result; |
| 136 | + return key ? jar[key] : jar; |
148 | 137 | } |
149 | 138 |
|
150 | 139 | api.set = api; |
|
154 | 143 | api.getJSON = function () { |
155 | 144 | return api.apply({ |
156 | 145 | json: true |
157 | | - }, [].slice.call(arguments)); |
| 146 | + }, arguments); |
158 | 147 | }; |
159 | 148 | api.remove = function (key, attributes) { |
160 | 149 | api(key, '', extend(attributes, { |
|
0 commit comments