-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenum.js
148 lines (138 loc) · 3.61 KB
/
enum.js
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
/*globals module */
!(function(){
var exports;
if( typeof window !== 'undefined'){
exports = window;
} else if( typeof module !== 'undefined' ){
exports = module.exports;
} else {
console.warn("I don't know where to export to....Enums.js");
exports = {};
}
exports.Enum = (function () {
"use strict";
var Enum = function ( enums ) {
var i,
keys = [],
values = [],
length = 0;
// if(!(enums instanceof Array)){
// throw new SyntaxError('Enum input must be an Array');
// }
if ( enums instanceof Array ) {
for ( i = 0 ; i < enums.length ; i++ ) {
try {
Object.defineProperty(
this,
enums[i],
{
configurable: false,
enumerable: true,
value: i,
writable: false
}
);
++length;
values.push(i);
keys.push(enums[i]);
} catch (e) {
throw new TypeError('Enum Constructor Failure, you must pass a set of unique keys.');
}
}
} else if ( enums instanceof Object ) {
for ( i in enums ) {
if ( enums.hasOwnProperty(i) ) {
if ( values.lastIndexOf(enums[i]) !== -1 ) {
throw new TypeError('Enum Constructor Failure, you must pass a unique value for each key');
}
try {
Object.defineProperty(
this,
i,
{
configurable: false,
enumerable: true,
value: enums[i],
writable: false
}
);
++length;
values.push(enums[i]);
keys.push(i);
} catch (e) {
throw new TypeError('Enum Constructor Failure.');
}
}
}
}
Object.defineProperty(this, 'length', {
configurable: false,
enumerable: false,
value: length,
writable: false
});
// Object.defineProperty(this, 'keys', {
// configurable: false,
// enumerable: false,
// value: keys,
// writable: false
// });
// Object.defineProperty(this, 'values', {
// configurable: false,
// enumerable: false,
// value: values,
// writable: false
// });
return Object.freeze(this);
};
// Prototype Functions
Object.defineProperties(Enum.prototype, {
/*'length':{
configurable: false,
enumerable: false,
get: function () {
var count = 0;
for ( var prop in this ){
++count;
}
return count;
//return this.keys.length;
}
},*/
'keys':{
configurable: false,
enumerable: false,
get: function () {
var keys = [];
for ( var prop in this ){
if ( this.hasOwnProperty(prop) ){
keys.push(prop);
}
}
return keys;
}
},
'values':{
configurable: false,
enumerable: false,
get: function () {
var values = [];
for ( var prop in this ){
if ( this.hasOwnProperty(prop) ){
values.push(this[prop]);
}
}
return values;
}
},
'toString':{
configurable: false,
enumerable: false,
writable: false,
value: function(){return '[object Enum]';}
}
});
// Export
return Enum;
})();
})();