js.cookie.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*!
  2. * JavaScript Cookie v2.2.1
  3. * https://github.com/js-cookie/js-cookie
  4. *
  5. * Copyright 2006, 2015 Klaus Hartl & Fagner Brack
  6. * Released under the MIT license
  7. */
  8. ;(function (factory) {
  9. var registeredInModuleLoader;
  10. if (typeof define === 'function' && define.amd) {
  11. define(factory);
  12. registeredInModuleLoader = true;
  13. }
  14. if (typeof exports === 'object') {
  15. module.exports = factory();
  16. registeredInModuleLoader = true;
  17. }
  18. if (!registeredInModuleLoader) {
  19. var OldCookies = window.Cookies;
  20. var api = window.Cookies = factory();
  21. api.noConflict = function () {
  22. window.Cookies = OldCookies;
  23. return api;
  24. };
  25. }
  26. }(function () {
  27. function extend () {
  28. var i = 0;
  29. var result = {};
  30. for (; i < arguments.length; i++) {
  31. var attributes = arguments[ i ];
  32. for (var key in attributes) {
  33. result[key] = attributes[key];
  34. }
  35. }
  36. return result;
  37. }
  38. function decode (s) {
  39. return s.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
  40. }
  41. function init (converter) {
  42. function api() {}
  43. function set (key, value, attributes) {
  44. if (typeof document === 'undefined') {
  45. return;
  46. }
  47. attributes = extend({
  48. path: '/'
  49. }, api.defaults, attributes);
  50. if (typeof attributes.expires === 'number') {
  51. attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5);
  52. }
  53. // We're using "expires" because "max-age" is not supported by IE
  54. attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
  55. try {
  56. var result = JSON.stringify(value);
  57. if (/^[\{\[]/.test(result)) {
  58. value = result;
  59. }
  60. } catch (e) {}
  61. value = converter.write ?
  62. converter.write(value, key) :
  63. encodeURIComponent(String(value))
  64. .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
  65. key = encodeURIComponent(String(key))
  66. .replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)
  67. .replace(/[\(\)]/g, escape);
  68. var stringifiedAttributes = '';
  69. for (var attributeName in attributes) {
  70. if (!attributes[attributeName]) {
  71. continue;
  72. }
  73. stringifiedAttributes += '; ' + attributeName;
  74. if (attributes[attributeName] === true) {
  75. continue;
  76. }
  77. // Considers RFC 6265 section 5.2:
  78. // ...
  79. // 3. If the remaining unparsed-attributes contains a %x3B (";")
  80. // character:
  81. // Consume the characters of the unparsed-attributes up to,
  82. // not including, the first %x3B (";") character.
  83. // ...
  84. stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
  85. }
  86. return (document.cookie = key + '=' + value + stringifiedAttributes);
  87. }
  88. function get (key, json) {
  89. if (typeof document === 'undefined') {
  90. return;
  91. }
  92. var jar = {};
  93. // To prevent the for loop in the first place assign an empty array
  94. // in case there are no cookies at all.
  95. var cookies = document.cookie ? document.cookie.split('; ') : [];
  96. var i = 0;
  97. for (; i < cookies.length; i++) {
  98. var parts = cookies[i].split('=');
  99. var cookie = parts.slice(1).join('=');
  100. if (!json && cookie.charAt(0) === '"') {
  101. cookie = cookie.slice(1, -1);
  102. }
  103. try {
  104. var name = decode(parts[0]);
  105. cookie = (converter.read || converter)(cookie, name) ||
  106. decode(cookie);
  107. if (json) {
  108. try {
  109. cookie = JSON.parse(cookie);
  110. } catch (e) {}
  111. }
  112. jar[name] = cookie;
  113. if (key === name) {
  114. break;
  115. }
  116. } catch (e) {}
  117. }
  118. return key ? jar[key] : jar;
  119. }
  120. api.set = set;
  121. api.get = function (key) {
  122. return get(key, false /* read as raw */);
  123. };
  124. api.getJSON = function (key) {
  125. return get(key, true /* read as json */);
  126. };
  127. api.remove = function (key, attributes) {
  128. set(key, '', extend(attributes, {
  129. expires: -1
  130. }));
  131. };
  132. api.defaults = {};
  133. api.withConverter = init;
  134. return api;
  135. }
  136. return init(function () {});
  137. }));