Guest User

Untitled

a guest
Jan 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. /**
  2. * CookieStorage
  3. * Pollyfill for Web Storage Store interface
  4. *
  5. * Ashen Gunaratne
  6. * mail@ashenm.ml
  7. *
  8. */
  9.  
  10. (function () {
  11.  
  12. var CookieStorage = function (path, maxAge, samesite) {
  13.  
  14. return Object.defineProperties({}, {
  15.  
  16. '__path': {
  17. value: path || '/'
  18. },
  19.  
  20. '__samesite': {
  21. value: samesite || 'strict'
  22. },
  23.  
  24. '__maxAge': {
  25. // default one hour
  26. value: maxAge || 3600
  27. },
  28.  
  29. 'getItem': {
  30. value: function (key) {
  31. return this.toObject()[key];
  32. }
  33. },
  34.  
  35. 'removeItem': {
  36. value: function (key) {
  37. document.cookie = key + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT ;path=' + this.__path;
  38. return this;
  39. }
  40. },
  41.  
  42. 'setItem': {
  43. value: function (key, value) {
  44. document.cookie = key + '=' + (value || '') + '; max-age=' + this.__maxAge + '; path' + this.__path;
  45. return this;
  46. }
  47. },
  48.  
  49. 'toString': {
  50. value: function (map) {
  51.  
  52. var key;
  53. var flatten = '';
  54.  
  55. if (typeof map === 'undefined') {
  56. return document.cookie;
  57. }
  58.  
  59. for (key in map) {
  60. flatten += key + '=' + map[key] + '; ';
  61. }
  62.  
  63. return flatten.replace(/;\s$|\s$|;$/, '');
  64.  
  65. }
  66. },
  67.  
  68. 'toObject': {
  69. value: function () {
  70.  
  71. var i;
  72. var map;
  73. var pair;
  74. var pairs;
  75. var value;
  76.  
  77. if (!document.cookie.length) {
  78. return {};
  79. }
  80.  
  81. map = {};
  82. pairs = document.cookie.split(';');
  83.  
  84. for (i = 0; i < pairs.length; i++) {
  85. pair = pairs[i].split('=');
  86. value = pair[1] || '';
  87. map[pair[0].trim()] = value.trim();
  88. }
  89.  
  90. return map;
  91.  
  92. }
  93. },
  94.  
  95. 'clear': {
  96. value: function () {
  97.  
  98. var key;
  99.  
  100. for (key in this.toObject()) {
  101. this.removeItem(key);
  102. }
  103.  
  104. return this;
  105.  
  106. }
  107. }
  108.  
  109. });
  110.  
  111. };
  112.  
  113. })();
Add Comment
Please, Sign In to add comment