2 * JavaScript Cookie v2.2.1
3 * https://github.com/js-cookie/js-cookie
5 * Copyright 2006, 2015 Klaus Hartl & Fagner Brack
6 * Released under the MIT license
9 var registeredInModuleLoader;
10 if (typeof define === 'function' && define.amd) {
12 registeredInModuleLoader = true;
14 if (typeof exports === 'object') {
15 module.exports = factory();
16 registeredInModuleLoader = true;
18 if (!registeredInModuleLoader) {
19 var OldCookies = window.Cookies;
20 var api = window.Cookies = factory();
21 api.noConflict = function () {
22 window.Cookies = OldCookies;
30 for (; i < arguments.length; i++) {
31 var attributes = arguments[ i ];
32 for (var key in attributes) {
33 result[key] = attributes[key];
40 return s.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
43 function init (converter) {
46 function set (key, value, attributes) {
47 if (typeof document === 'undefined') {
53 }, api.defaults, attributes);
55 if (typeof attributes.expires === 'number') {
56 attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5);
59 // We're using "expires" because "max-age" is not supported by IE
60 attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
63 var result = JSON.stringify(value);
64 if (/^[\{\[]/.test(result)) {
69 value = converter.write ?
70 converter.write(value, key) :
71 encodeURIComponent(String(value))
72 .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
74 key = encodeURIComponent(String(key))
75 .replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)
76 .replace(/[\(\)]/g, escape);
78 var stringifiedAttributes = '';
79 for (var attributeName in attributes) {
80 if (!attributes[attributeName]) {
83 stringifiedAttributes += '; ' + attributeName;
84 if (attributes[attributeName] === true) {
88 // Considers RFC 6265 section 5.2:
90 // 3. If the remaining unparsed-attributes contains a %x3B (";")
92 // Consume the characters of the unparsed-attributes up to,
93 // not including, the first %x3B (";") character.
95 stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
98 return (document.cookie = key + '=' + value + stringifiedAttributes);
101 function get (key, json) {
102 if (typeof document === 'undefined') {
107 // To prevent the for loop in the first place assign an empty array
108 // in case there are no cookies at all.
109 var cookies = document.cookie ? document.cookie.split('; ') : [];
112 for (; i < cookies.length; i++) {
113 var parts = cookies[i].split('=');
114 var cookie = parts.slice(1).join('=');
116 if (!json && cookie.charAt(0) === '"') {
117 cookie = cookie.slice(1, -1);
121 var name = decode(parts[0]);
122 cookie = (converter.read || converter)(cookie, name) ||
127 cookie = JSON.parse(cookie);
139 return key ? jar[key] : jar;
143 api.get = function (key) {
144 return get(key, false /* read as raw */);
146 api.getJSON = function (key) {
147 return get(key, true /* read as json */);
149 api.remove = function (key, attributes) {
150 set(key, '', extend(attributes, {
157 api.withConverter = init;
162 return init(function () {});