js.cookie.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*! js-cookie v3.0.0 | 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. return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent)
  27. },
  28. write: function (value) {
  29. return encodeURIComponent(value).replace(
  30. /%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,
  31. decodeURIComponent
  32. )
  33. }
  34. };
  35. /* eslint-enable no-var */
  36. /* eslint-disable no-var */
  37. function init (converter, defaultAttributes) {
  38. function set (key, value, attributes) {
  39. if (typeof document === 'undefined') {
  40. return
  41. }
  42. attributes = assign({}, defaultAttributes, attributes);
  43. if (typeof attributes.expires === 'number') {
  44. attributes.expires = new Date(Date.now() + attributes.expires * 864e5);
  45. }
  46. if (attributes.expires) {
  47. attributes.expires = attributes.expires.toUTCString();
  48. }
  49. key = encodeURIComponent(key)
  50. .replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent)
  51. .replace(/[()]/g, escape);
  52. value = converter.write(value, key);
  53. var stringifiedAttributes = '';
  54. for (var attributeName in attributes) {
  55. if (!attributes[attributeName]) {
  56. continue
  57. }
  58. stringifiedAttributes += '; ' + attributeName;
  59. if (attributes[attributeName] === true) {
  60. continue
  61. }
  62. // Considers RFC 6265 section 5.2:
  63. // ...
  64. // 3. If the remaining unparsed-attributes contains a %x3B (";")
  65. // character:
  66. // Consume the characters of the unparsed-attributes up to,
  67. // not including, the first %x3B (";") character.
  68. // ...
  69. stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
  70. }
  71. return (document.cookie = key + '=' + value + stringifiedAttributes)
  72. }
  73. function get (key) {
  74. if (typeof document === 'undefined' || (arguments.length && !key)) {
  75. return
  76. }
  77. // To prevent the for loop in the first place assign an empty array
  78. // in case there are no cookies at all.
  79. var cookies = document.cookie ? document.cookie.split('; ') : [];
  80. var jar = {};
  81. for (var i = 0; i < cookies.length; i++) {
  82. var parts = cookies[i].split('=');
  83. var value = parts.slice(1).join('=');
  84. if (value[0] === '"') {
  85. value = value.slice(1, -1);
  86. }
  87. try {
  88. var foundKey = defaultConverter.read(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. })));