|
29 | 29 | var unallowedCharsInValue = extend(unallowedChars, { |
30 | 30 | ' ': '%20' |
31 | 31 | }); |
32 | | - function encode(value, charmap) { |
| 32 | + |
| 33 | + function encode (value, charmap) { |
33 | 34 | for (var character in charmap) { |
34 | 35 | value = value |
35 | 36 | .replace(new RegExp(character, 'g'), charmap[character]); |
36 | 37 | } |
37 | 38 | return value; |
38 | 39 | } |
39 | 40 |
|
40 | | - function decode(value, charmap) { |
| 41 | + function decode (value, charmap) { |
41 | 42 | for (var character in charmap) { |
42 | 43 | value = value |
43 | 44 | .replace(new RegExp(charmap[character], 'g'), character); |
44 | 45 | } |
45 | 46 | return value; |
46 | 47 | } |
47 | 48 |
|
48 | | - function parseCookieValue(value) { |
| 49 | + function processWrite (value) { |
| 50 | + var stringified; |
| 51 | + try { |
| 52 | + stringified = JSON.stringify(value); |
| 53 | + if (/^(?:\{[\w\W]*\}|\[[\w\W]*\])$/.test(stringified)) { |
| 54 | + value = stringified; |
| 55 | + } |
| 56 | + } catch(e) {} |
| 57 | + return encode(String(value), unallowedCharsInValue); |
| 58 | + } |
| 59 | + |
| 60 | + function processRead (value, converter, json) { |
49 | 61 | if (value.indexOf('"') === 0) { |
50 | 62 | // This is a quoted cookie as according to RFC2068, unescape... |
51 | 63 | value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); |
52 | 64 | } |
53 | 65 |
|
54 | 66 | value = decode(value, unallowedCharsInValue); |
55 | 67 |
|
56 | | - try { |
57 | | - return api.json ? JSON.parse(value) : value; |
58 | | - } catch(e) {} |
59 | | - } |
| 68 | + if (json) { |
| 69 | + try { |
| 70 | + value = JSON.parse(value); |
| 71 | + } catch(e) {} |
| 72 | + } |
60 | 73 |
|
61 | | - function read(s, converter) { |
62 | | - var value = parseCookieValue(s); |
63 | 74 | return isFunction(converter) ? converter(value) : value; |
64 | 75 | } |
65 | 76 |
|
66 | | - function extend() { |
| 77 | + function extend () { |
67 | 78 | var key, options; |
68 | 79 | var i = 0; |
69 | 80 | var result = {}; |
|
76 | 87 | return result; |
77 | 88 | } |
78 | 89 |
|
79 | | - function isFunction(obj) { |
| 90 | + function isFunction (obj) { |
80 | 91 | return Object.prototype.toString.call(obj) === '[object Function]'; |
81 | 92 | } |
82 | 93 |
|
83 | 94 | var api = function (key, value, options) { |
| 95 | + var converter; |
| 96 | + |
| 97 | + if (isFunction(value)) { |
| 98 | + converter = value; |
| 99 | + value = undefined; |
| 100 | + } |
84 | 101 |
|
85 | 102 | // Write |
86 | 103 |
|
87 | | - if (arguments.length > 1 && !isFunction(value)) { |
| 104 | + if (arguments.length > 1 && !converter) { |
88 | 105 | options = extend(api.defaults, options); |
89 | 106 |
|
90 | 107 | if (typeof options.expires === 'number') { |
|
93 | 110 | } |
94 | 111 |
|
95 | 112 | return (document.cookie = [ |
96 | | - encode(key, unallowedCharsInName), '=', encode(api.json ? JSON.stringify(value) : String(value), unallowedCharsInValue), |
| 113 | + encode(key, unallowedCharsInName), '=', processWrite(value), |
97 | 114 | options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE |
98 | 115 | options.path ? '; path=' + options.path : '', |
99 | 116 | options.domain ? '; domain=' + options.domain : '', |
|
117 | 134 | cookie = parts.join('='); |
118 | 135 |
|
119 | 136 | if (key === name) { |
120 | | - // If second argument (value) is a function it's a converter... |
121 | | - result = read(cookie, value); |
| 137 | + result = processRead(cookie, converter, this.json); |
122 | 138 | break; |
123 | 139 | } |
124 | 140 |
|
125 | | - // Prevent storing a cookie that we couldn't decode. |
126 | | - if (!key && (cookie = read(cookie)) !== undefined) { |
127 | | - result[name] = cookie; |
| 141 | + if (!key) { |
| 142 | + result[name] = processRead(cookie, converter, this.json); |
128 | 143 | } |
129 | 144 | } |
130 | 145 |
|
131 | 146 | return result; |
132 | 147 | }; |
133 | 148 |
|
134 | 149 | api.get = api.set = api; |
| 150 | + api.getJSON = function() { |
| 151 | + return api.get.apply({ |
| 152 | + json: true |
| 153 | + }, [].slice.call(arguments)); |
| 154 | + }; |
135 | 155 | api.defaults = {}; |
136 | 156 |
|
137 | 157 | api.remove = function (key, options) { |
|
0 commit comments