-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenum.js
More file actions
25 lines (22 loc) · 697 Bytes
/
enum.js
File metadata and controls
25 lines (22 loc) · 697 Bytes
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
class Enum {
constructor(valueArr){
// You can give this an array of strings which are converted to objects
for (let val of valueArr){
if (typeof val === 'string'){
this[val] = {
accessor: valueArr.indexOf(val) + 1, // Don't use zero-based indices in enums; for truthiness we want the first index to be index 1
humanName: val
}
} else if (typeof val === 'object'){
this[val.accessor] = {
accessor: val.accessor,
humanName: val.humanName || val.accessor
}
} else {
throw new Error('Unknown type for Enum values: must be a string or object with properties "accessor" [and "humanName"]')
}
}
Object.freeze(this)
}
}
exports.Enum = Enum