cookies_store.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS206: Consider reworking classes to avoid initClass
  8. * DS207: Consider shorter variations of null checks
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  10. */
  11. (function() {
  12. let INT = undefined;
  13. const Cls = (this.CookiesStore = class CookiesStore {
  14. static initClass() {
  15. // Intentionally called CookiesStore instead of CookieStore
  16. // Calling it CookieStore causes issues when the Experimental Web Platform features flag is enabled in Chrome
  17. // Related issue: https://github.com/freeCodeCamp/devdocs/issues/932
  18. INT = /^\d+$/;
  19. }
  20. static onBlocked() {}
  21. get(key) {
  22. let value = Cookies.get(key);
  23. if ((value != null) && INT.test(value)) { value = parseInt(value, 10); }
  24. return value;
  25. }
  26. set(key, value) {
  27. if (value === false) {
  28. this.del(key);
  29. return;
  30. }
  31. if (value === true) { value = 1; }
  32. if (value && (typeof INT.test === 'function' ? INT.test(value) : undefined)) { value = parseInt(value, 10); }
  33. Cookies.set(key, '' + value, {path: '/', expires: 1e8});
  34. if (this.get(key) !== value) { this.constructor.onBlocked(key, value, this.get(key)); }
  35. }
  36. del(key) {
  37. Cookies.expire(key);
  38. }
  39. reset() {
  40. try {
  41. for (var cookie of Array.from(document.cookie.split(/;\s?/))) {
  42. Cookies.expire(cookie.split('=')[0]);
  43. }
  44. return;
  45. } catch (error) {}
  46. }
  47. dump() {
  48. const result = {};
  49. for (var cookie of Array.from(document.cookie.split(/;\s?/))) {
  50. if (cookie[0] !== '_') {
  51. cookie = cookie.split('=');
  52. result[cookie[0]] = cookie[1];
  53. }
  54. }
  55. return result;
  56. }
  57. });
  58. Cls.initClass();
  59. return Cls;
  60. })();