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