/**
* NOOP Cookie module.
* @memberof noop
* @namespace cookie
* @author cisco211
* @copyright © 2019 by "cisco211"
* @version 0.100
*/
(function(noop, undefined)
{
'use strict';
// #region Private
/**
* @summary Default cookie parameters.
* @typedef {object.<string, any>} noop.cookie.Defaults
* @memberof noop.cookie
* @property {string|null} [comment=null] - A comment explaining this cookie.
* @property {string|null} [domain=null] - The domain this cookie belongs to.
* @property {string|null} [expires=null] - The expiration date.
* @property {string|null} [maxAge=null] - The maximum allowed cookie age.
* @property {string|null} [path=null] - The URL path this cookie belongs to.
* @property {boolean} [secure=false] - Flag that this cookie is only available on HTTPS.
* @property {string|null} [version=null] - Cookie version number.
*/
class Defaults
{
constructor()
{
this.comment = null;
this.domain = null;
this.expires = null;
this.maxAge = null;
this.path = null;
this.secure = false;
this.version = null;
} // constructor()
}; // class Defaults
// #endregion Private
// #region Public
noop.cookie = new class Cookie
{
constructor()
{
/**
* @summary Get an object of all available cookies.
* @function all
* @memberof noop.cookie
* @return {object.<string, string>} - An object of all available cookies.
* @example var all = noop.cookie.all();
*/
this.all = function()
{
var l;
var o = new class Cookies {};
var p;
var s = document.cookie.split('; ');
l = s.length;
if (l > 0)
{
for (var i = 0; i < l; ++i)
{
p = s[i].split('=');
o[p[0]] = decodeURIComponent(p[1]);
}
}
return o;
}; // all = function()
/**
* @summary Return default cookie parameters.
* @function def
* @memberof noop.cookie
* @return {noop.cookie.Defaults} - An object containing the default cookie parameters.
* @example var def = noop.cookie.def();
*/
this.def = function()
{
return new Defaults();
}; // def = function()
/**
* @summary Delete a cookie by given name and cookie parameters.
* @function del
* @memberof noop.cookie
* @param {string} key - The cookie name.
* @param {noop.cookie.Defaults} [opts=noop.cookie.def()] - An object containing cookie parameters.
* @example noop.cookie.del('foo');
*/
this.del = function(key, opts = noop.cookie.def())
{
opts.expires = null;
opts.maxAge = -1;
noop.cookie.set(key, 'nullptr', opts);
}; // del = function(key, opts = noop.cookie.def())
/**
* @summary Get a cookie by given name.
* @function get
* @memberof noop.cookie
* @param {string} key - The cookie name.
* @param {any} [fallback=null] - The value, that will return when the given cookie name does not exist.
* @param {boolean} [raw=false] - Directly return the string, instead converting the type.
* @example var get = noop.cookie.get('foo');
*/
this.get = function(key, fallback = null, raw = false)
{
var c = document.cookie.match('(^|;)\\s*' + key + '\\s*=\\s*([^;]+)');
if (c)
{
var v = decodeURIComponent(c.pop());
if (raw)
return v;
if
(
(v.startsWith('[') && v.endsWith(']')) ||
(v.startsWith('{') && v.endsWith('}')) ||
noop.isInteger(v) ||
noop.isFloat(v) ||
v.toLowerCase().match(/^(true|false|null)$/)
)
return JSON.parse(v);
return v;
}
return fallback;
}; // get = function(key, fallback = null, raw = false)
/**
* @summary Check that cookie exists.
* @function has
* @memberof noop.cookie
* @param {string} key - The cookie name.
* @return {boolean} - A boolean indicating if the given cookie exists or not.
* @example var has = noop.cookie.has('foo');
*/
this.has = function(key)
{
return !noop.isNull(document.cookie.match(';?\\s*' + key + '\\s*='));
}; // has = function(key)
/**
* @summary Get a cookie by given name and create the cookie if not exists using given value.
* @function rw
* @memberof noop.cookie
* @param {string} key - The cookie name.
* @param {any} [value=null] - The cookie value.
* @param {noop.cookie.Defaults} [opts=noop.cookie.def()] - An object containing cookie parameters.
* @param {boolean} [raw=false] - Directly return the string, instead converting the type.
* @example var rw = noop.cookie.rw('foo', 'bar');
*/
this.rw = function(key, value = null, opts = noop.cookie.def(), raw = false)
{
if (!noop.cookie.has(key))
noop.cookie.set(key, value, opts);
return noop.cookie.get(key, value, raw);
}; // rw = function(key, value = null, opts = noop.cookie.def())
/**
* @summary Set a cookie by given name, value and cookie parameters.
* @function set
* @memberof noop.cookie
* @param {string} key - The cookie name.
* @param {any} [value=null] - The cookie value.
* @param {noop.cookie.Defaults} [opts=noop.cookie.def()] - An object containing cookie parameters.
* @example noop.cookie.set('foo', true);
*/
this.set = function(key, value = null, opts = noop.cookie.def())
{
if (noop.isSet(opts.expires) && noop.isSet(opts.maxAge))
return;
if (!noop.isString(value))
value = JSON.stringify(value);
var c = '';
c = key;
c += '=';
c += encodeURIComponent(value);
c += '; ';
if (noop.isSet(opts.expires))
{
var d = new Date();
d.setTime(d.getTime() + (opts.expires * 1000));
c += 'expires';
c += '=';
c += d.toUTCString();
c += '; ';
}
if (noop.isSet(opts.maxAge))
{
c += 'max-age';
c += '=';
c += opts.maxAge;
c += '; ';
}
if (noop.isSet(opts.path))
{
c += 'path';
c += '=';
c += opts.path;
c+= '; ';
}
if (noop.isSet(opts.domain))
{
c += 'domain';
c += '=';
c += opts.domain;
c+= '; ';
}
if (noop.isSet(opts.secure) && opts.secure)
{
c += 'secure';
c+= '; ';
}
if (noop.isSet(opts.version))
{
c += 'version';
c += '=';
c += opts.version;
c+= '; ';
}
if (noop.isSet(opts.comment))
{
c += 'comment';
c += '=';
c += opts.comment;
c+= '; ';
}
c = c.slice(0, c.length - 1);
// console.log(c);
document.cookie = c;
}; // set = function(key, value = null, opts = noop.cookie.def())
} // constructor()
}; // noop.cookie = new class Cookie
// #endregion Public
} // (function(noop, undefined)
(window.noop = window.noop || new class NOOP{}));
// EOF