JSFiddle - React, Tailwind, and code Playground
by jasonwilczak
JavaScript
//Object for cacheing Javascript logs until there is a force dump or the max has been reached or exceeded
var JSLogStorage = (function ($) {
var _storageBin;
//Check if the storage is empty, null or undefined and return true/false
function isEmpty() {
if (_storageBin === null || _storageBin === undefined || _storageBin.length === 0)
{ return true; }
return false;
}
//Push a log object into the storage
function addLog(log) {
if (isEmpty()) {
_storageBin = [];
}
log.LogIndex = getCount();
_storageBin.push(log);
}
//Get a splice from the array from 0->n, where n = maxItemsToGet
function getLogs(maxItemsToGet) {
if (isEmpty()) {
_storageBin = [];
}
if (maxItemsToGet > _storageBin.length)
{ maxItemsToGet = _storageBin.length; }
return _storageBin.splice(0, maxItemsToGet);
}
//Check to see if the storage is at specified capacity
function isFull(maxItems) {
if (isEmpty()) {
_storageBin = [];
}
if (_storageBin.length >= maxItems) {
return true;
}
return false;
}
//Drop all the elements from storage up to the max number of items
function emptyStorage(maxItemsToRemove) {
if (isEmpty()) {
_storageBin = [];
}
if (_storageBin.length <= maxItemsToRemove) {
_storageBin.splice(0, maxItemsToRemove);
}
else {
_storageBin = [];
}
}
//Get the count of all the log records in storage
function getCount() {
if (isEmpty()) {
_storageBin = [];
}
return _storageBin.length;
}
return {
IsEmpty: isEmpty,
AddLog: addLog,
GetLogs: getLogs,
IsFull: isFull,
EmptyStorage: emptyStorage,
GetCount: getCount
};
} (jQuery));
//Object for cacheing Javascript logs...