js.cookie.mjs 3.4 KB

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