@textback/notification-widget
Version:
TODO: Give a short introduction of your project. Let this section explain the objectives or the motivation behind this project.
68 lines (57 loc) • 1.8 kB
JavaScript
/**
* Created by martb on 28.06.2017.
* Module for work with cookies
*/
const cookies = {
/**
* возвращает cookie с именем name, если есть, если нет, то null
* @param name {String}
* @returns {String|null}
*/
getCookie: function (name) {
let nameEQ = name + "=";
let ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
return null;
},
/**
* устанавливает куку
* @param name - имя куки
* @param value - значение куки
* @param t - время в секундах
*/
setCookie: function (name, value, t) {
let expires = "";
if (t) {
let date = new Date();
t *= 1000;
date.setTime(date.getTime() + t);
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + encodeURIComponent(value) + expires + "; path=/";
},
/*
* удаляет куку по имени
* @param name {String}
*/
deleteCookie: function (name) {
setCookie(name, "", -1);
},
getCookieObject: function () {
const result = {};
document.cookie
.split(';')
.forEach((cookieItem) => {
const separatorPos = cookieItem.indexOf('=');
const key = cookieItem.slice(0, separatorPos).trim();
const value = cookieItem.slice(separatorPos + 1);
result[key] = value;
});
return result;
}
};
export default cookies;