-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path4-convert.js
More file actions
156 lines (138 loc) · 3.02 KB
/
4-convert.js
File metadata and controls
156 lines (138 loc) · 3.02 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
// This examples converts array of string to Object and
// use EnumCollection for mentioned case
// Multiple Enum implementations
const EnumArray = (values) => class {
constructor(val) {
this.value = values.includes(val) ? val : undefined;
}
static get collection() {
return values;
}
[Symbol.toPrimitive]() {
return this.value;
}
};
const EnumCollection = (values) => {
const index = {};
for (const key in values) {
const value = values[key];
index[value] = key;
}
return class {
constructor(arg) {
const value = values[arg];
this.key = value ? arg : index[arg];
}
static get collection() {
return values;
}
[Symbol.toPrimitive](hint) {
const key = this.key;
if (hint === 'number') return parseInt(key, 10);
return values[key];
}
};
};
const Enum = (...args) => {
const item = args[0];
const itemType = typeof item;
if (itemType === 'object') return EnumCollection(item);
if (itemType !== 'string') return EnumArray(args);
return EnumCollection(Object.assign({}, args));
};
// 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]
]);
}