js.cookie.mjs 3.4 KB

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