|
8 | 8 | (function (factory) { |
9 | 9 | if (typeof define === 'function' && define.amd) { |
10 | 10 | // AMD (Register as an anonymous module) |
11 | | - define(['jquery'], factory); |
| 11 | + define(factory); |
12 | 12 | } else if (typeof exports === 'object') { |
13 | 13 | // Node/CommonJS |
14 | | - module.exports = factory(require('jquery')); |
| 14 | + module.exports = factory(); |
15 | 15 | } else { |
16 | 16 | // Browser globals |
17 | | - window.Cookies = factory(jQuery); |
| 17 | + window.Cookies = factory(); |
18 | 18 | } |
19 | | -}(function ($) { |
| 19 | +}(function () { |
20 | 20 |
|
21 | 21 | var pluses = /\+/g; |
22 | 22 |
|
23 | 23 | function encode(s) { |
24 | | - return config.raw ? s : encodeURIComponent(s); |
| 24 | + return api.raw ? s : encodeURIComponent(s); |
25 | 25 | } |
26 | 26 |
|
27 | 27 | function decode(s) { |
28 | | - return config.raw ? s : decodeURIComponent(s); |
| 28 | + return api.raw ? s : decodeURIComponent(s); |
29 | 29 | } |
30 | 30 |
|
31 | 31 | function stringifyCookieValue(value) { |
32 | | - return encode(config.json ? JSON.stringify(value) : String(value)); |
| 32 | + return encode(api.json ? JSON.stringify(value) : String(value)); |
33 | 33 | } |
34 | 34 |
|
35 | 35 | function parseCookieValue(s) { |
|
43 | 43 | // If we can't decode the cookie, ignore it, it's unusable. |
44 | 44 | // If we can't parse the cookie, ignore it, it's unusable. |
45 | 45 | s = decodeURIComponent(s.replace(pluses, ' ')); |
46 | | - return config.json ? JSON.parse(s) : s; |
| 46 | + return api.json ? JSON.parse(s) : s; |
47 | 47 | } catch(e) {} |
48 | 48 | } |
49 | 49 |
|
50 | 50 | function read(s, converter) { |
51 | | - var value = config.raw ? s : parseCookieValue(s); |
| 51 | + var value = api.raw ? s : parseCookieValue(s); |
52 | 52 | return isFunction(converter) ? converter(value) : value; |
53 | 53 | } |
54 | 54 |
|
|
69 | 69 | return Object.prototype.toString.call(obj) === '[object Function]'; |
70 | 70 | } |
71 | 71 |
|
72 | | - var config = $.cookie = function (key, value, options) { |
| 72 | + var api = function (key, value, options) { |
73 | 73 |
|
74 | 74 | // Write |
75 | 75 |
|
76 | 76 | if (arguments.length > 1 && !isFunction(value)) { |
77 | | - options = extend(config.defaults, options); |
| 77 | + options = extend(api.defaults, options); |
78 | 78 |
|
79 | 79 | if (typeof options.expires === 'number') { |
80 | 80 | var days = options.expires, t = options.expires = new Date(); |
|
95 | 95 | var result = key ? undefined : {}, |
96 | 96 | // To prevent the for loop in the first place assign an empty array |
97 | 97 | // in case there are no cookies at all. Also prevents odd result when |
98 | | - // calling $.cookie(). |
| 98 | + // calling "get()". |
99 | 99 | cookies = document.cookie ? document.cookie.split('; ') : [], |
100 | 100 | i = 0, |
101 | 101 | l = cookies.length; |
|
120 | 120 | return result; |
121 | 121 | }; |
122 | 122 |
|
123 | | - config.defaults = {}; |
| 123 | + api.get = api.set = api; |
| 124 | + api.defaults = {}; |
124 | 125 |
|
125 | | - $.removeCookie = function (key, options) { |
| 126 | + api.remove = function (key, options) { |
126 | 127 | // Must not alter options, thus extending a fresh object... |
127 | | - $.cookie(key, '', extend(options, { expires: -1 })); |
128 | | - return !$.cookie(key); |
| 128 | + api(key, '', extend(options, { expires: -1 })); |
| 129 | + return !api(key); |
129 | 130 | }; |
130 | 131 |
|
131 | | - $.cookie.get = $.cookie; |
132 | | - $.cookie.set = $.cookie; |
133 | | - $.cookie.remove = $.removeCookie; |
134 | | - $.cookie._extend = extend; |
| 132 | + api._extend = extend; |
135 | 133 |
|
136 | | - return $.cookie; |
| 134 | + return api; |
137 | 135 | })); |
0 commit comments