|
17 | 17 | // Browser globals |
18 | 18 | var _OldCookies = window.Cookies; |
19 | 19 | var api = window.Cookies = factory(window.jQuery); |
20 | | - api.noConflict = function() { |
| 20 | + api.noConflict = function () { |
21 | 21 | window.Cookies = _OldCookies; |
22 | 22 | return api; |
23 | 23 | }; |
24 | 24 | } |
25 | 25 | }(function () { |
26 | | - function decode (value) { |
27 | | - return value.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent); |
28 | | - } |
29 | | - |
30 | 26 | function extend () { |
31 | 27 | var i = 0; |
32 | 28 | var result = {}; |
|
39 | 35 | return result; |
40 | 36 | } |
41 | 37 |
|
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) { |
59 | 40 | var result; |
60 | | - var args = [].slice.call(arguments); |
61 | 41 |
|
62 | 42 | // Write |
63 | 43 |
|
64 | | - if (args.length > 1) { |
| 44 | + if ([].slice.call(arguments).length > 1) { |
65 | 45 | options = extend(api.defaults, options); |
66 | 46 |
|
67 | 47 | if (typeof options.expires === 'number') { |
|
72 | 52 |
|
73 | 53 | try { |
74 | 54 | result = JSON.stringify(value); |
75 | | - if (/^(?:\{[\w\W]*\}|\[[\w\W]*\])$/.test(result)) { |
| 55 | + if (/^[\{\[]/.test(result)) { |
76 | 56 | value = result; |
77 | 57 | } |
78 | 58 | } catch(e) {} |
|
103 | 83 | // in case there are no cookies at all. Also prevents odd result when |
104 | 84 | // calling "get()" |
105 | 85 | var cookies = document.cookie ? document.cookie.split('; ') : []; |
| 86 | + var rdecode = /(%[0-9A-Z]{2})+/g; |
106 | 87 | var i = 0; |
107 | 88 |
|
108 | 89 | 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 | + } |
112 | 105 |
|
113 | 106 | if (key === name) { |
114 | | - result = processRead(cookie, name, this.json); |
| 107 | + result = cookie; |
115 | 108 | break; |
116 | 109 | } |
117 | 110 |
|
118 | 111 | if (!key) { |
119 | | - result[name] = processRead(cookie, name, this.json); |
| 112 | + result[name] = cookie; |
120 | 113 | } |
121 | 114 | } |
122 | 115 |
|
123 | 116 | return result; |
124 | | - }; |
| 117 | + } |
125 | 118 |
|
126 | 119 | api.get = api.set = api; |
127 | 120 | api.getJSON = function () { |
|
0 commit comments