|
46 | 46 | return value; |
47 | 47 | } |
48 | 48 |
|
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 | 49 | function processRead (value, converter, json) { |
61 | | - if (value.indexOf('"') === 0) { |
62 | | - // This is a quoted cookie as according to RFC2068, unescape... |
63 | | - value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); |
| 50 | + if (value.charAt(0) === '"') { |
| 51 | + value = value.slice(1, -1); |
64 | 52 | } |
65 | 53 |
|
66 | 54 | value = decode(value, unallowedCharsInValue); |
|
71 | 59 | } catch(e) {} |
72 | 60 | } |
73 | 61 |
|
74 | | - return isFunction(converter) ? converter(value) : value; |
| 62 | + return converter ? converter(value) : value; |
75 | 63 | } |
76 | 64 |
|
77 | 65 | function extend () { |
78 | | - var key, options; |
79 | 66 | var i = 0; |
80 | 67 | var result = {}; |
81 | 68 | for (; i < arguments.length; i++) { |
82 | | - options = arguments[ i ]; |
83 | | - for (key in options) { |
| 69 | + var options = arguments[ i ]; |
| 70 | + for (var key in options) { |
84 | 71 | result[key] = options[key]; |
85 | 72 | } |
86 | 73 | } |
87 | 74 | return result; |
88 | 75 | } |
89 | 76 |
|
90 | | - function isFunction (obj) { |
91 | | - return Object.prototype.toString.call(obj) === '[object Function]'; |
92 | | - } |
93 | | - |
94 | 77 | var api = function (key, value, options) { |
95 | | - var converter; |
| 78 | + var converter, result; |
| 79 | + var args = [].slice.call(arguments); |
96 | 80 |
|
97 | | - if (isFunction(value)) { |
| 81 | + if (typeof value === 'function') { |
98 | 82 | converter = value; |
99 | | - value = undefined; |
| 83 | + args.length = 1; |
100 | 84 | } |
101 | 85 |
|
102 | 86 | // Write |
103 | 87 |
|
104 | | - if (arguments.length > 1 && !converter) { |
| 88 | + if (args.length > 1) { |
105 | 89 | options = extend(api.defaults, options); |
106 | 90 |
|
107 | 91 | if (typeof options.expires === 'number') { |
108 | | - var days = options.expires, t = options.expires = new Date(); |
109 | | - t.setMilliseconds(t.getMilliseconds() + days * 864e+5); |
| 92 | + var expires = new Date(); |
| 93 | + expires.setMilliseconds(expires.getMilliseconds() + options.expires * 864e+5); |
| 94 | + options.expires = expires; |
110 | 95 | } |
111 | 96 |
|
| 97 | + try { |
| 98 | + result = JSON.stringify(value); |
| 99 | + if (/^(?:\{[\w\W]*\}|\[[\w\W]*\])$/.test(result)) { |
| 100 | + value = result; |
| 101 | + } |
| 102 | + } catch(e) {} |
| 103 | + |
| 104 | + value = encode(String(value), unallowedCharsInValue); |
| 105 | + |
112 | 106 | return (document.cookie = [ |
113 | | - encode(key, unallowedCharsInName), '=', processWrite(value), |
114 | | - options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE |
115 | | - options.path ? '; path=' + options.path : '', |
116 | | - options.domain ? '; domain=' + options.domain : '', |
117 | | - options.secure ? '; secure' : '' |
| 107 | + encode(key, unallowedCharsInName), '=', value, |
| 108 | + options.expires && '; expires=' + options.expires.toUTCString(), // use expires attribute, max-age is not supported by IE |
| 109 | + options.path && '; path=' + options.path, |
| 110 | + options.domain && '; domain=' + options.domain, |
| 111 | + options.secure && '; secure' |
118 | 112 | ].join('')); |
119 | 113 | } |
120 | 114 |
|
121 | 115 | // Read |
122 | 116 |
|
123 | | - var result = key ? undefined : {}, |
124 | | - // To prevent the for loop in the first place assign an empty array |
125 | | - // in case there are no cookies at all. Also prevents odd result when |
126 | | - // calling "get()". |
127 | | - cookies = document.cookie ? document.cookie.split('; ') : [], |
128 | | - i = 0, |
129 | | - l = cookies.length; |
| 117 | + if (!key) { |
| 118 | + result = {}; |
| 119 | + } |
| 120 | + |
| 121 | + // To prevent the for loop in the first place assign an empty array |
| 122 | + // in case there are no cookies at all. Also prevents odd result when |
| 123 | + // calling "get()" |
| 124 | + var cookies = document.cookie ? document.cookie.split('; ') : []; |
| 125 | + var i = 0; |
130 | 126 |
|
131 | | - for (; i < l; i++) { |
| 127 | + for (; i < cookies.length; i++) { |
132 | 128 | var parts = cookies[i].split('='), |
133 | 129 | name = decode(parts.shift(), unallowedCharsInName), |
134 | 130 | cookie = parts.join('='); |
|
148 | 144 |
|
149 | 145 | api.get = api.set = api; |
150 | 146 | api.getJSON = function() { |
151 | | - return api.get.apply({ |
| 147 | + return api.apply({ |
152 | 148 | json: true |
153 | 149 | }, [].slice.call(arguments)); |
154 | 150 | }; |
|
0 commit comments