js.cookie.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*! js-cookie v3.0.1 | MIT */
  2. ;
  3. (function (global, factory) {
  4. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  5. typeof define === 'function' && define.amd ? define(factory) :
  6. (global = global || self, (function () {
  7. var current = global.Cookies;
  8. var exports = global.Cookies = factory();
  9. exports.noConflict = function () { global.Cookies = current; return exports; };
  10. }()));
  11. }(this, (function () { 'use strict';
  12. /* eslint-disable no-var */
  13. function assign (target) {
  14. for (var i = 1; i < arguments.length; i++) {
  15. var source = arguments[i];
  16. for (var key in source) {
  17. target[key] = source[key];
  18. }
  19. }
  20. return target
  21. }
  22. /* eslint-enable no-var */
  23. /* eslint-disable no-var */
  24. var defaultConverter = {
  25. read: function (value) {
  26. if (value[0] === '"') {
  27. value = value.slice(1, -1);
  28. }
  29. return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent)
  30. },
  31. write: function (value) {
  32. return encodeURIComponent(value).replace(
  33. /%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,
  34. decodeURIComponent
  35. )
  36. }
  37. };
  38. /* eslint-enable no-var */
  39. /* eslint-disable no-var */
  40. function init (converter, defaultAttributes) {
  41. function set (key, value, attributes) {
  42. if (typeof document === 'undefined') {
  43. return
  44. }
  45. attributes = assign({}, defaultAttributes, attributes);
  46. if (typeof attributes.expires === 'number') {
  47. attributes.expires = new Date(Date.now() + attributes.expires * 864e5);
  48. }
  49. if (attributes.expires) {
  50. attributes.expires = attributes.expires.toUTCString();
  51. }
  52. key = encodeURIComponent(key)
  53. .replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent)
  54. .replace(/[()]/g, escape);
  55. var stringifiedAttributes = '';
  56. for (var attributeName in attributes) {
  57. if (!attributes[attributeName]) {
  58. continue
  59. }
  60. stringifiedAttributes += '; ' + attributeName;
  61. if (attributes[attributeName] === true) {
  62. continue
  63. }
  64. // Considers RFC 6265 section 5.2:
  65. // ...
  66. // 3. If the remaining unparsed-attributes contains a %x3B (";")
  67. // character:
  68. // Consume the characters of the unparsed-attributes up to,
  69. // not including, the first %x3B (";") character.
  70. // ...
  71. stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
  72. }
  73. return (document.cookie =
  74. key + '=' + converter.write(value, key) + stringifiedAttributes)
  75. }
  76. function get (key) {
  77. if (typeof document === 'undefined' || (arguments.length && !key)) {
  78. return
  79. }
  80. // To prevent the for loop in the first place assign an empty array
  81. // in case there are no cookies at all.
  82. var cookies = document.cookie ? document.cookie.split('; ') : [];
  83. var jar = {};
  84. for (var i = 0; i < cookies.length; i++) {
  85. var parts = cookies[i].split('=');
  86. var value = parts.slice(1).join('=');
  87. try {
  88. var foundKey = decodeURIComponent(parts[0]);
  89. jar[foundKey] = converter.read(value, foundKey);
  90. if (key === foundKey) {
  91. break
  92. }
  93. } catch (e) {}
  94. }
  95. return key ? jar[key] : jar
  96. }
  97. return Object.create(
  98. {
  99. set: set,
  100. get: get,
  101. remove: function (key, attributes) {
  102. set(
  103. key,
  104. '',
  105. assign({}, attributes, {
  106. expires: -1
  107. })
  108. );
  109. },
  110. withAttributes: function (attributes) {
  111. return init(this.converter, assign({}, this.attributes, attributes))
  112. },
  113. withConverter: function (converter) {
  114. return init(assign({}, this.converter, converter), this.attributes)
  115. }
  116. },
  117. {
  118. attributes: { value: Object.freeze(defaultAttributes) },
  119. converter: { value: Object.freeze(converter) }
  120. }
  121. )
  122. }
  123. var api = init(defaultConverter, { path: '/' });
  124. /* eslint-enable no-var */
  125. return api;
  126. })));