-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path1-enum.js
More file actions
48 lines (40 loc) · 978 Bytes
/
1-enum.js
File metadata and controls
48 lines (40 loc) · 978 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
// Enum factory
const Enum = (...values) => class {
constructor(arg) {
if (typeof arg === 'number') {
this.value = (arg > 0 && arg <= values.length) ? arg : undefined;
return;
}
const value = values.indexOf(arg);
this.value = value > 0 ? value + 1 : undefined;
}
[Symbol.toPrimitive](hint) {
if (hint === 'number') return this.value;
return values[this.value - 1];
}
};
// Example type
const Month = Enum(
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
);
// Usage
const neg = new Month(-1);
const zero = new Month(0);
const first = new Month(1);
const april = new Month(4);
const may = new Month('May');
const last = new Month(12);
const next = new Month(13);
const unknown = new Month('Hello');
console.log([
[-1, neg],
[0, zero],
[1, first],
[4, april],
['May', may],
[12, last],
[13, next],
['Hello', unknown]
]);