javascript-time-ago
Version:
Localized relative date/time formatting
62 lines (61 loc) • 2.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
* A basic in-memory cache.
*
* import Cache from 'javascript-time-ago/Cache'
* const cache = new Cache()
* const object = cache.get('key1', 'key2', ...) || cache.put('key1', 'key2', ..., createObject())
*/
var Cache = exports["default"] = /*#__PURE__*/function () {
function Cache() {
_classCallCheck(this, Cache);
this.cache = {};
}
_createClass(Cache, [{
key: "get",
value: function get() {
var cache = this.cache;
for (var _len = arguments.length, keys = new Array(_len), _key = 0; _key < _len; _key++) {
keys[_key] = arguments[_key];
}
for (var _i = 0, _keys = keys; _i < _keys.length; _i++) {
var key = _keys[_i];
if (_typeof(cache) !== 'object') {
return;
}
cache = cache[key];
}
return cache;
}
}, {
key: "put",
value: function put() {
for (var _len2 = arguments.length, keys = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
keys[_key2] = arguments[_key2];
}
var value = keys.pop();
var lastKey = keys.pop();
var cache = this.cache;
for (var _i2 = 0, _keys2 = keys; _i2 < _keys2.length; _i2++) {
var key = _keys2[_i2];
if (_typeof(cache[key]) !== 'object') {
cache[key] = {};
}
cache = cache[key];
}
return cache[lastKey] = value;
}
}]);
return Cache;
}();
//# sourceMappingURL=cache.js.map