You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/assets/javascripts/lib/cookies_store.js

74 lines
1.9 KiB

// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS206: Consider reworking classes to avoid initClass
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
// Intentionally called CookiesStore instead of CookieStore
// Calling it CookieStore causes issues when the Experimental Web Platform features flag is enabled in Chrome
// Related issue: https://github.com/freeCodeCamp/devdocs/issues/932
class CookiesStore {
static INT = /^\d+$/;
1 year ago
static onBlocked() {}
get(key) {
let value = Cookies.get(key);
if (value != null && CookiesStore.INT.test(value)) {
value = parseInt(value, 10);
}
return value;
}
set(key, value) {
if (value === false) {
this.del(key);
return;
}
if (value === true) {
value = 1;
}
if (
value &&
(typeof CookiesStore.INT.test === "function"
? CookiesStore.INT.test(value)
: undefined)
) {
value = parseInt(value, 10);
}
Cookies.set(key, "" + value, { path: "/", expires: 1e8 });
if (this.get(key) !== value) {
CookiesStore.onBlocked(key, value, this.get(key));
}
}
del(key) {
Cookies.expire(key);
}
reset() {
try {
for (var cookie of Array.from(document.cookie.split(/;\s?/))) {
Cookies.expire(cookie.split("=")[0]);
}
return;
} catch (error) {}
}
dump() {
const result = {};
for (var cookie of Array.from(document.cookie.split(/;\s?/))) {
if (cookie[0] !== "_") {
cookie = cookie.split("=");
result[cookie[0]] = cookie[1];
}
}
return result;
}
}