-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (35 loc) · 1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
const assert = require('assert');
const _ = require('lodash');
function PocketRegistry() {
const register = {};
this.set = function (key, value) {
assert(!this.has(key), `Registry already has a '${key}'.`);
assert(value !== undefined, `'${key}' cannot be set to undefined.`);
_.set(register, key, value);
return;
};
this.get = function (key, defaultValue) {
if (defaultValue === undefined) {
assert(this.has(key), `Registry key '${key}' not found, default not provided!`);
}
return _.get(register, key, defaultValue);
};
this.has = function (key) {
return _.has(register, key);
};
this.remove = function (key) {
_.unset(register, key);
};
// Create a read-only 'keys' property, using top-level objects only.
Object.defineProperties(this, {
keys: {
get: function () {
return Object.keys(register);
}
}
});
};
// Don't export a new instance of the registry, we want
// to be able to have more than one within our application.
module.exports = PocketRegistry;