Skip to content

Instantly share code, notes, and snippets.

@raindrop-ua
Created February 10, 2016 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raindrop-ua/b49b1510be23b85e8b0c to your computer and use it in GitHub Desktop.
Save raindrop-ua/b49b1510be23b85e8b0c to your computer and use it in GitHub Desktop.
Collect some IDs in cookies
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(elt) {
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0) ? Math.ceil(from) : Math.floor(from);
if (from < 0) from += len;
for (; from < len; from++) {
if (from in this && this[from] === elt) return from;
}
return -1;
};
}
function CookieIDsHistory(name, options) {
if ((typeof name == 'undefined') || (name.length < 1)) {
throw 'Please set cookie name.';
}
options = options || {};
this._cookieName = encodeURIComponent(name);
this._expiresDays = options.days || 0;
this._itemsLimit = options.limit || 15;
this._uniqueValues = !!options.unique;
this._setCookie = function(name, value, days) {
var expires, host;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = '; expires=' + date.toGMTString();
} else {
expires = '';
}
host = location.hostname.replace(/^www\./i, '');
document.cookie = name + '=' + value + expires + '; path=/' + '; domain=.' + host;
};
this._getCookie = function(name) {
var cookieName = name + '=';
var cookieArray = document.cookie.split(';');
for (var i = 0; i < cookieArray.length; i++) {
var cookieItem = cookieArray[i];
while (cookieItem.charAt(0) == ' ') cookieItem = cookieItem.substring(1, cookieItem.length);
if (cookieItem.indexOf(cookieName) == 0) return cookieItem.substring(cookieName.length, cookieItem.length);
}
return '';
};
this._delCookie = function(name) {
this._setCookie(name, '', -1);
};
this._getCookieData = function() {
var items;
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
try {
items = JSON.parse(this._getCookie(this._cookieName)) || [];
} catch (err) {
items = [];
}
if (!Array.isArray(items)) {
items = [];
}
return items;
};
this._setCookieData = function(items) {
this._setCookie(this._cookieName, JSON.stringify(items), this._expiresDays);
};
this.add = function(id) {
var items = this._getCookieData();
if (items.length > this._itemsLimit) {
items.shift();
}
if (this._uniqueValues) {
while (items.indexOf(id) > -1) {
items.splice(items.indexOf(id), 1);
}
}
items.push(id);
this._setCookieData(items);
return this;
};
this.remove = function(id) {
var items = this._getCookieData();
while (items.indexOf(id) > -1) {
items.splice(items.indexOf(id), 1);
}
this._setCookieData(items);
return this;
};
this.removeOne = function(id) {
var items = this._getCookieData();
if (items.indexOf(id) != -1) {
items.splice(items.indexOf(id), 1);
}
this._setCookieData(items);
return this;
};
this.count = function(id) {
var items = this._getCookieData(),
counter = 0;
for (var i = 0; i < items.length; i++) {
if (items[i] == id) {
counter++;
}
}
return counter;
};
this.length = function() {
var items = this._getCookieData();
return items.length;
};
this.is = function(id) {
return !!this.count(id);
};
this.unset = function () {
this._delCookie(this._cookieName);
return this;
};
}
/* Usage example: */
var ch = new CookieIDsHistory('_test', { days: 5, limit: 50, unique: true });
// days - How many days cookie will live
// limit - How many IDs will be collected before cutting of old IDs will start
// unique - Can IDs be repeated in the array or not
// Adding IDs to collect.
ch.add(103);
ch.add(345);
ch.add(101);
ch.add(235);
ch.add(101);
ch.add(867);
ch.add(124);
// or simply
ch.add(103).add(345).add(101).add(235).add(101).add(867).add(124);
// Check ID existing.
ch.is(101);
// Count IDs in collection. Note: it makes sense with unique parameter in False state.
ch.count(101);
// Remove only first ID in collection. Note: it makes sense with unique parameter in False state.
ch.removeOne(101);
// Remove all specified IDs in collection.
ch.remove(101);
// Remove cookie.
ch.unset();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment