diff options
author | marzavec <admin@marzavec.com> | 2018-03-10 08:47:00 +0100 |
---|---|---|
committer | marzavec <admin@marzavec.com> | 2018-03-10 08:47:00 +0100 |
commit | fde6895720a4f417283b9e375583967b504de2f3 (patch) | |
tree | f5c8d9a188572d759456831d574bef9881d5c0be /server/src/managers/stats.js | |
download | hackchat-fde6895720a4f417283b9e375583967b504de2f3.tar.gz hackchat-fde6895720a4f417283b9e375583967b504de2f3.zip |
initial commit
Diffstat (limited to '')
-rw-r--r-- | server/src/managers/stats.js | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/server/src/managers/stats.js b/server/src/managers/stats.js new file mode 100644 index 0000000..c3b6f9f --- /dev/null +++ b/server/src/managers/stats.js @@ -0,0 +1,61 @@ +/** + * Simple generic stats collection script for events occurances (etc) + * + * Version: v2.0.0 + * Developer: Marzavec ( https://github.com/marzavec ) + * License: WTFPL ( http://www.wtfpl.net/txt/copying/ ) + * + */ + +'use strict'; + +class Stats { + /** + * Create a stats instance. + * + */ + constructor () { + this._stats = {}; + } + + /** + * Retrieve value of arbitrary `key` reference + * + * @param {String} key Reference to the arbitrary store name + */ + get (key) { + return this._stats[key]; + } + + /** + * Set value of arbitrary `key` reference + * + * @param {String} key Reference to the arbitrary store name + * @param {Number} value New value for `key` + */ + set (key, value) { + this._stats[key] = value; + } + + /** + * Increase value of arbitrary `key` reference, by 1 or `amount` + * + * @param {String} key Reference to the arbitrary store name + * @param {Number} amount Value to increase `key` by, or 1 if omitted + */ + increment (key, amount) { + this.set(key, (this.get(key) || 0) + (amount || 1)); + } + + /** + * Reduce value of arbitrary `key` reference, by 1 or `amount` + * + * @param {String} key Reference to the arbitrary store name + * @param {Number} amount Value to decrease `key` by, or 1 if omitted + */ + decrement (key, amount) { + this.set(key, (this.get(key) || 0) - (amount || 1)); + } +} + +module.exports = Stats; |