summaryrefslogtreecommitdiffhomepage
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/lib/enum.js54
1 files changed, 45 insertions, 9 deletions
diff --git a/app/lib/enum.js b/app/lib/enum.js
index a27bd37439..ac5df30d06 100644
--- a/app/lib/enum.js
+++ b/app/lib/enum.js
@@ -8,23 +8,47 @@ export default class Enum {
/**
* Creates an instance of EnumBase.
*
- * @param {...string} ... - enum keys
+ * @param {...string|object} ... - enum keys
* @memberOf Enum
*/
constructor() {
- const keys = [...arguments];
+ const items = [...arguments];
+ let allKeys = [];
+ let reverseMap = new Map();
- for(const key of keys) {
- Object.defineProperty(this, key, {
- enumerable: true,
- value: key,
- writable: false
- });
+ for(const item of items) {
+ if(typeof(item) === 'string') {
+ Object.defineProperty(this, item, {
+ enumerable: true,
+ value: item,
+ writable: false
+ });
+ allKeys.push(item);
+ reverseMap.set(item, item);
+ } else if(typeof(item) === 'object') {
+ for(const key of Object.keys(item)) {
+ Object.defineProperty(this, key, {
+ enumerable: true,
+ value: item[key],
+ writable: false
+ });
+ allKeys.push(key);
+ reverseMap.set(item[key], key);
+ }
+ } else {
+ throw new Error('Unsupported argument type: ' + typeof(item));
+ }
}
Object.defineProperty(this, 'allKeys', {
enumerable: false,
- value: keys,
+ value: allKeys,
+ writable: false
+ });
+
+ Object.defineProperty(this, 'reverseMap', {
+ enumerable: false,
+ value: reverseMap,
writable: false
});
@@ -42,5 +66,17 @@ export default class Enum {
isValid(key) {
return this.allKeys.includes(key);
}
+
+ /**
+ * Return key for value
+ *
+ * @param {any} value
+ * @returns {any|undefined} returns undefined if key is not found
+ *
+ * @memberOf Enum
+ */
+ reverse(value) {
+ return this.reverseMap.get(value);
+ }
}