@code-o-mat/history-cache
Version:
A data object structure that logs all changes using the immutable library
136 lines (104 loc) • 2.76 kB
JavaScript
import Bunyan from 'bunyan';
const log = Bunyan.createLogger({name: "HistoryCacheLog"});
import { NONE, PROPS } from '@code-o-mat/globals/lib/constants/values';
import {
HISTORY_CACHE,
} from '@code-o-mat/globals/lib/constants/types';
import {
GET_LOG_ITEMS,
GET_LOG_ITEM_TEXT,
PROCESS_LOG_ITEM
} from './constants/methods';
import { LOG } from './constants/types';
import {
assertIsIntOrNONE,
} from '@code-o-mat/globals/lib/assertions/js-types';
import Meta from '@code-o-mat/globals/lib/abstracts/meta';
class Log extends Meta {
static FAMILY = HISTORY_CACHE;
static TYPE = LOG;
FAMILY = HISTORY_CACHE;
TYPE = LOG;
/**
*
* @param {List} history Immutable List
* @param {int|NONE} limit
* @return {void}
*/
constructor(history, limit = NONE) {
super();
this[PROPS] = {
log: history,
limit: limit,
source: NONE,
sourceName: NONE,
}
}
$(limit = NONE) {
return this[GET_LOG_ITEMS](limit);
}
limit(limit = NONE) {
assertIsIntOrNONE(limit);
this[PROPS].limit = limit;
return this;
}
sourceName(sourceName = NONE) {
if (soure === NONE) return this[PROPS].source;
assertIsString(source);
this[PROPS].sourceName = sourceName;
return this;
}
log(limit) {
return this[GET_LOG_ITEMS](limit);
}
list(limit) {
return this[GET_LOG_ITEMS](limit);
}
js(limit) {
return this[GET_LOG_ITEMS](limit).toJS();
}
json(limit) {
return JSON.stringify(this[GET_LOG_ITEMS](limit));
}
text(limit = NONE) {
const { _sourceName } = this;
const logText = this[GET_LOG_ITEMS](limit)
.map(this[GET_LOG_ITEM_TEXT])
.join('\t\r');
const sourceStr = !_sourceName || _sourceName === NONE ? 'anon' : _sourceName;
return `History Cache Log for ${sourceStr}:
showing ${limit === NONE ? ' ' : limit} items:
===============
${logText}`;
}
[GET_LOG_ITEM_TEXT](item) {
return `--------------
${item.date}
${item.tag && item.tag!=='' ? item.tag : 'No Tag'}
${item.UID}
${item.event}
${item.comment}
--------------`;
}
[PROCESS_LOG_ITEM](item) {
return {
date: item.date,
event: item.event,
comment: item.comment,
UID: item.UID,
tag: item.tag
}
}
[GET_LOG_ITEMS](limit = NONE) {
if (limit === NONE) limit = this[PROPS].limit;
assertIsIntOrNONE(limit);
if (limit === NONE) {
return this[PROPS].log
.map(this[PROCESS_LOG_ITEM]);
}
return this[PROPS].log
.slice(-limit)
.map(this[PROCESS_LOG_ITEM]);
}
}
export default Log;