This repository was archived by the owner on Nov 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum.js
More file actions
132 lines (110 loc) · 3.63 KB
/
enum.js
File metadata and controls
132 lines (110 loc) · 3.63 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
let exceptions = require('node-exceptions');
let InvalidArgumentException = exceptions.InvalidArgumentException;
/**
* @param {string} name
* @returns {string}
* @see See {@link http://ecma-international.org/ecma-262/5.1/#sec-7.6} for more info.
*/
let getValidPropertyName = (name) => {
name = name.replace(/^[^A-Za-z$_]+/, function(match) {
return new Array(match.length + 1).join('_');
});
return name.replace(/[^a-z0-9$_]/ig, '_');
};
class Enum {
/**
* @param {string} name
* @param {string|int} value
* @param {*} extra any extra parameter(s)
*/
constructor(name, value, extra) {
/**
* @var {string}
*/
this._name = name;
/**
* @var {string|int}
*/
this._value = value;
/**
* @var {*}
*/
this._extra = extra;
}
/**
* @return {int}
*/
get value() {
return this._value;
}
/**
* @return {string}
*/
get name() {
return this._name;
}
/**
* @return {*}
*/
get extra() {
return this._extra;
}
/**
* @param {string|int} property
* @return {Enum}
* @throws {InvalidArgumentException}
*/
static valueOf(property) {
let propertyType = typeof property;
let propertyToEnumMap = null;
switch (propertyType) {
case 'string':
let upperCasedProperty = property.toUpperCase();
if (this.nameToEnumMap[upperCasedProperty]) {
property = upperCasedProperty;
propertyToEnumMap = this.nameToEnumMap;
} else {
propertyToEnumMap = this.valueToEnumMap;
}
break;
case 'number':
propertyToEnumMap = this.valueToEnumMap;
}
if (propertyToEnumMap && propertyToEnumMap[property]) {
return propertyToEnumMap[property];
}
throw new InvalidArgumentException('No enum with specified name');
}
/**
* @param {Object.<string, string|int>} items
* @param {*[]|null} [extra] extra values stored in enums
* @returns {{}}
*/
static create(items, extra = null) {
if (extra && !Array.isArray(extra)) {
throw new InvalidArgumentException('Extra params should be an array or null');
}
if (Array.isArray(extra) && extra.length !== Object.keys(items).length) {
throw new InvalidArgumentException('Extra params should be an array of the same length as enum has or null');
}
let newEnum = class extends this {};
let valueToEnumMap = {};
let nameToEnumMap = {};
Object.keys(items).map((/** string */name, index) => {
let propertyName = getValidPropertyName(name.toUpperCase());
if (newEnum.hasOwnProperty(propertyName)) {
throw new InvalidArgumentException('Some names turn to be the same after making them valid JS IdentifierName');
}
let value = items[name];
let enumInstance = new newEnum(name, value, Array.isArray(extra) ? extra[index] : null);
newEnum[propertyName] = enumInstance;
valueToEnumMap[value] = enumInstance;
nameToEnumMap[name.toUpperCase()] = enumInstance;
});
// Define non-writable, non-enumerable properties
Object.defineProperty(newEnum, 'valueToEnumMap', {value: valueToEnumMap});
Object.defineProperty(newEnum, 'nameToEnumMap', {value: nameToEnumMap});
return Object.freeze(newEnum);
};
}
module.exports = Enum;