cookies_store.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. // Intentionally called CookiesStore instead of CookieStore
  12. // Calling it CookieStore causes issues when the Experimental Web Platform features flag is enabled in Chrome
  13. // Related issue: https://github.com/freeCodeCamp/devdocs/issues/932
  14. class CookiesStore {
  15. static INT = /^\d+$/;
  16. static onBlocked() {}
  17. get(key) {
  18. let value = Cookies.get(key);
  19. if (value != null && CookiesStore.INT.test(value)) {
  20. value = parseInt(value, 10);
  21. }
  22. return value;
  23. }
  24. set(key, value) {
  25. if (value === false) {
  26. this.del(key);
  27. return;
  28. }
  29. if (value === true) {
  30. value = 1;
  31. }
  32. if (
  33. value &&
  34. (typeof CookiesStore.INT.test === "function"
  35. ? CookiesStore.INT.test(value)
  36. : undefined)
  37. ) {
  38. value = parseInt(value, 10);
  39. }
  40. Cookies.set(key, "" + value, { path: "/", expires: 1e8 });
  41. if (this.get(key) !== value) {
  42. CookiesStore.onBlocked(key, value, this.get(key));
  43. }
  44. }
  45. del(key) {
  46. Cookies.expire(key);
  47. }
  48. reset() {
  49. try {
  50. for (var cookie of Array.from(document.cookie.split(/;\s?/))) {
  51. Cookies.expire(cookie.split("=")[0]);
  52. }
  53. return;
  54. } catch (error) {}
  55. }
  56. dump() {
  57. const result = {};
  58. for (var cookie of Array.from(document.cookie.split(/;\s?/))) {
  59. if (cookie[0] !== "_") {
  60. cookie = cookie.split("=");
  61. result[cookie[0]] = cookie[1];
  62. }
  63. }
  64. return result;
  65. }
  66. }