-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
132 lines (132 loc) · 3.91 KB
/
index.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
exports.EnumFlagsTest = {
has: function (val, flags) {
return ((+val & +flags) === +flags);
},
any: function (val, flags) {
return !!(+val & +flags);
},
eql: function (val, flags) {
return (+val === +flags);
}
};
exports.EnumFlagsTestAlt = function (val) {
this.val = val;
};
exports.EnumFlagsTestAlt.prototype.has = function (flags) {
return ((+this.val & +flags) === +flags);
};
function EnumFlagsType(enumeration, prop) {
var keys = {};
var hash = Object.keys(enumeration).reduce(function (obj, k) {
if (isNaN(k)) {
obj[k] = +enumeration[k];
}
keys[k] = k;
return obj;
}, {});
var State = function (methods) {
this.methods = methods;
};
Object.keys(hash).forEach(function (k) {
Object.defineProperty(State.prototype, k, { get: function () {
return !!hash[k] ? ((this.methods.val & hash[k]) === hash[k]) : !this.methods.val;
} });
});
var Methods = function (val) {
this.val = +val;
this.state = new State(this);
};
Methods.prototype.eql = function (flags) {
return (this.val === +flags);
};
Methods.prototype.has = function (flags) {
return (+(this.val & +flags) === +flags);
};
Methods.prototype.any = function (flags) {
return !!(this.val & +flags);
};
Methods.prototype.toArray = function () {
return Object.keys(hash).filter(function (k) {
return !!hash[k] && ((this.val & hash[k]) === hash[k]);
}.bind(this));
};
Methods.prototype.toString = function () {
return Object.keys(hash).filter(function (k) {
return !!hash[k] && ((this.val & hash[k]) === hash[k]);
}.bind(this)).join(' | ');
};
var BindValue = function BindValue(val) {
return new Methods((val === undefined) ? this : val);
};
BindValue.val = hash;
BindValue.key = keys;
BindValue.toArray = function () {
var arr = [];
Object.keys(hash).map(function (k) {
arr.push({ key: k, val: hash[k] });
});
};
if (prop) {
Object.defineProperty(Number.prototype, prop, { get: BindValue });
}
return BindValue;
}
exports.EnumFlagsType = EnumFlagsType;
function EnumStringsType(enumeration, prop, validKeysFilter) {
var hash = {};
var keys = {};
if (validKeysFilter) {
Object.keys(enumeration).forEach(function (k) {
if (validKeysFilter(k)) {
hash[k] = enumeration[k];
keys[k] = k;
}
});
}
else {
Object.keys(enumeration).forEach(function (k) {
hash[k] = enumeration[k];
keys[k] = k;
});
}
var State = function (methods) {
this.methods = methods;
};
Object.keys(hash).forEach(function (k) {
Object.defineProperty(State.prototype, k, { get: function () {
return (this.methods.str === hash[k]);
} });
});
var Methods = function (str) {
this.str = str.toString();
this.state = new State(this);
};
Methods.prototype.equals = function (str) {
return (this.str === str);
};
Methods.prototype.toStringKey = function () {
for (var k in hash) {
if (hash[k] === this.str) {
return k;
}
}
};
Methods.prototype.toStringVal = function () {
return this.str;
};
var BindString = function (str) {
return new Methods((str === undefined) ? this : str);
};
BindString.val = hash;
BindString.key = keys;
BindString.toArray = function () {
return Object.keys(hash).map(function (k) {
return { key: k, val: hash[k] };
});
};
if (prop) {
Object.defineProperty(String.prototype, prop, { get: BindString });
}
return BindString;
}
exports.EnumStringsType = EnumStringsType;