-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path2-indexed.js
More file actions
140 lines (127 loc) · 2.73 KB
/
2-indexed.js
File metadata and controls
140 lines (127 loc) · 2.73 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
133
134
135
136
137
138
139
140
'use strict';
// Enum factory
const Enum = (...args) => {
const objectMode = typeof args[0] === 'object';
const collection = objectMode ? args[0] : args;
const values = {};
const index = {};
let numKeys = false;
for (const name in collection) {
const value = collection[name];
numKeys = numKeys || typeof value === 'number';
const key = numKeys ? value : name;
values[key] = value;
index[value] = key;
}
return class {
constructor(arg) {
const value = values[arg];
this.key = value ? arg : index[arg];
}
static get collection() {
return collection;
}
[Symbol.toPrimitive](hint) {
const key = this.key;
if (hint === 'number') {
return numKeys ? key : parseInt(key, 10);
}
return values[key];
}
};
};
// Test
const testEnum = (Month) => {
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 aug = new Month('Aug');
const august = new Month('August');
const m11 = new Month(11);
const m12 = new Month(12);
const m13 = new Month(13);
const unknown = new Month('Hello');
console.log([
['-1', neg],
['0', zero],
['1', first],
['4', april],
['May', may],
['Aug', aug],
['August', august],
['11', m11],
['12', m12],
['13', m13],
['Hello', unknown]
]);
};
// Example 1
{
const Month = Enum(
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
);
console.dir(Month.collection);
testEnum(Month);
}
// Example 2
{
const Month = Enum({
1: 'January',
2: 'February',
3: 'March',
4: 'April',
5: 'May',
6: 'June',
7: 'July',
8: 'August',
9: 'September',
10: 'October',
11: 'November',
12: 'December'
});
console.dir(Month.collection);
testEnum(Month);
}
// Example 3
{
const Month = Enum({
Jan: 'January',
Feb: 'February',
Mar: 'March',
Apr: 'April',
May: 'May',
Jun: 'June',
Jul: 'July',
Aug: 'August',
Sep: 'September',
Oct: 'October',
Nov: 'November',
Dec: 'December'
});
console.dir(Month.collection);
testEnum(Month);
}
// Example 4
{
const Hundreds = Enum(100, 200, 300, 400, 500);
console.dir(Hundreds.collection);
const neg = new Hundreds(-1);
const zero = new Hundreds(0);
const h100 = new Hundreds(100);
const h300 = new Hundreds(200);
const h500 = new Hundreds(500);
const h600 = new Hundreds(600);
const unknown = new Hundreds('Hello');
console.log([
['-1', neg],
['0', zero],
['100', h100],
['200', h300],
['500', h500],
['600', h600],
['Hello', unknown]
]);
}