UNPKG

hubot-factoids-loche

Version:
179 lines (163 loc) 4.47 kB
// Generated by CoffeeScript 1.10.0 (function() { var Factoids; Factoids = (function() { function Factoids(robot) { var base, ref; this.robot = robot; if (((ref = this.robot.brain) != null ? ref.data : void 0) != null) { this.data = (base = this.robot.brain.data).factoids != null ? base.factoids : base.factoids = {}; } this.robot.brain.on('loaded', (function(_this) { return function() { var base1; return _this.data = (base1 = _this.robot.brain.data).factoids != null ? base1.factoids : base1.factoids = {}; }; })(this)); } Factoids.prototype.set = function(key, value, who, resolveAlias) { var fact, hist; key = key.trim(); value = value.trim(); fact = this.get(key, resolveAlias); if (typeof fact === 'object') { if (fact.history == null) { fact.history = []; } hist = { date: Date(), editor: who, oldValue: fact.value, newValue: value }; fact.history.push(hist); fact.value = value; if (fact.forgotten != null) { fact.forgotten = false; } } else { fact = { value: value, popularity: 0 }; } return this.data[key.toLowerCase()] = fact; }; Factoids.prototype.add = function(key, value, who, resolveAlias) { var fact; fact = this.get(key, resolveAlias); fact = fact.value + ", and is also " + value; return this.set(key, fact, who, resolveAlias); }; Factoids.prototype.get = function(key, resolveAlias) { var alias, fact, ref; if (resolveAlias == null) { resolveAlias = true; } fact = this.data[key.toLowerCase()]; alias = fact != null ? (ref = fact.value) != null ? ref.match(/^@([^@].+)$/i) : void 0 : void 0; if (resolveAlias && (alias != null)) { fact = this.get(alias[1]); } return fact; }; Factoids.prototype.has = function(key) { if (key in this.data) { return true; } else { return false; } }; Factoids.prototype.search = function(str) { var keys; keys = Object.keys(this.data); return keys.filter((function(_this) { return function(a) { var value; if (_this.data[a].forgotten) { return false; } value = _this.data[a].value; return value.indexOf(str) > -1 || a.indexOf(str) > -1; }; })(this)); }; Factoids.prototype.ref = function(key, fact) { var aliasKey; aliasKey = fact.value.match(/^@([^@].+)$/i); if (aliasKey) { key = aliasKey[1]; return this.ref(key, this.data[key]); } return { value: fact.value, key: key }; }; Factoids.prototype.list = function() { var data, fact, i, key, keys, map, name, okey, result, str; map = {}; keys = Object.keys(this.data); key = ''; i = 0; while (i < keys.length) { key = keys[i]; okey = key; fact = this.data[key]; if (fact.forgotten) { ++i; continue; } data = this.ref(key, fact, []); if (data.key !== key) { key = data.key; } if (!map[key]) { map[key] = { aliases: [], value: '' }; } if (key !== okey) { map[key].aliases.push(okey); } map[key].value = data.value; ++i; } result = []; for (name in map) { str = name; if (map[name].aliases.length) { str += ' (aliases: ' + map[name].aliases.join(', ') + ')'; } result.push(str); } return result; }; Factoids.prototype.forget = function(key) { var fact; fact = this.get(key); if (fact) { return fact.forgotten = true; } }; Factoids.prototype.remember = function(key) { var fact; fact = this.get(key); if (fact) { fact.forgotten = false; } return fact; }; Factoids.prototype.drop = function(key) { key = key.toLowerCase(); if (this.has(key)) { return delete this.data[key]; } else { return false; } }; return Factoids; })(); module.exports = Factoids; }).call(this);