@code-o-mat/history-cache
Version:
A data object structure that logs all changes using the immutable library
191 lines (158 loc) • 7.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _bunyan = _interopRequireDefault(require("bunyan"));
var _values = require("@code-o-mat/globals/lib/constants/values");
var _types = require("@code-o-mat/globals/lib/constants/types");
var _methods = require("./constants/methods");
var _types2 = require("./constants/types");
var _jsTypes = require("@code-o-mat/globals/lib/assertions/js-types");
var _meta = _interopRequireDefault(require("@code-o-mat/globals/lib/abstracts/meta"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return right[Symbol.hasInstance](left); } else { return left instanceof right; } }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!_instanceof(instance, Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
var log = _bunyan.default.createLogger({
name: "HistoryCacheLog"
});
var _GET_LOG_ITEM_TEXT = _methods.GET_LOG_ITEM_TEXT;
var _PROCESS_LOG_ITEM = _methods.PROCESS_LOG_ITEM;
var _GET_LOG_ITEMS = _methods.GET_LOG_ITEMS;
var Log =
/*#__PURE__*/
function (_Meta) {
_inherits(Log, _Meta);
/**
*
* @param {List} history Immutable List
* @param {int|NONE} limit
* @return {void}
*/
function Log(history) {
var _this;
var limit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _values.NONE;
_classCallCheck(this, Log);
_this = _possibleConstructorReturn(this, (Log.__proto__ || Object.getPrototypeOf(Log)).call(this));
Object.defineProperty(_assertThisInitialized(_this), "FAMILY", {
configurable: true,
enumerable: true,
writable: true,
value: _types.HISTORY_CACHE
});
Object.defineProperty(_assertThisInitialized(_this), "TYPE", {
configurable: true,
enumerable: true,
writable: true,
value: _types2.LOG
});
_this[_values.PROPS] = {
log: history,
limit: limit,
source: _values.NONE,
sourceName: _values.NONE
};
return _this;
}
_createClass(Log, [{
key: "$",
value: function $() {
var limit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _values.NONE;
return this[_methods.GET_LOG_ITEMS](limit);
}
}, {
key: "limit",
value: function limit() {
var _limit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _values.NONE;
(0, _jsTypes.assertIsIntOrNONE)(_limit);
this[_values.PROPS].limit = _limit;
return this;
}
}, {
key: "sourceName",
value: function sourceName() {
var _sourceName2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _values.NONE;
if (soure === _values.NONE) return this[_values.PROPS].source;
assertIsString(source);
this[_values.PROPS].sourceName = _sourceName2;
return this;
}
}, {
key: "log",
value: function log(limit) {
return this[_methods.GET_LOG_ITEMS](limit);
}
}, {
key: "list",
value: function list(limit) {
return this[_methods.GET_LOG_ITEMS](limit);
}
}, {
key: "js",
value: function js(limit) {
return this[_methods.GET_LOG_ITEMS](limit).toJS();
}
}, {
key: "json",
value: function json(limit) {
return JSON.stringify(this[_methods.GET_LOG_ITEMS](limit));
}
}, {
key: "text",
value: function text() {
var limit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _values.NONE;
var _sourceName = this._sourceName;
var logText = this[_methods.GET_LOG_ITEMS](limit).map(this[_methods.GET_LOG_ITEM_TEXT]).join('\t\r');
var sourceStr = !_sourceName || _sourceName === _values.NONE ? 'anon' : _sourceName;
return "History Cache Log for ".concat(sourceStr, ":\n showing ").concat(limit === _values.NONE ? ' ' : limit, " items:\n ===============\n ").concat(logText);
}
}, {
key: _GET_LOG_ITEM_TEXT,
value: function value(item) {
return "--------------\n ".concat(item.date, "\n ").concat(item.tag && item.tag !== '' ? item.tag : 'No Tag', "\n ").concat(item.UID, "\n ").concat(item.event, "\n ").concat(item.comment, "\n --------------");
}
}, {
key: _PROCESS_LOG_ITEM,
value: function value(item) {
return {
date: item.date,
event: item.event,
comment: item.comment,
UID: item.UID,
tag: item.tag
};
}
}, {
key: _GET_LOG_ITEMS,
value: function value() {
var limit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _values.NONE;
if (limit === _values.NONE) limit = this[_values.PROPS].limit;
(0, _jsTypes.assertIsIntOrNONE)(limit);
if (limit === _values.NONE) {
return this[_values.PROPS].log.map(this[_methods.PROCESS_LOG_ITEM]);
}
return this[_values.PROPS].log.slice(-limit).map(this[_methods.PROCESS_LOG_ITEM]);
}
}]);
return Log;
}(_meta.default);
Object.defineProperty(Log, "FAMILY", {
configurable: true,
enumerable: true,
writable: true,
value: _types.HISTORY_CACHE
});
Object.defineProperty(Log, "TYPE", {
configurable: true,
enumerable: true,
writable: true,
value: _types2.LOG
});
var _default = Log;
exports.default = _default;