-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
205 lines (182 loc) · 5.53 KB
/
index.ts
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/** Interface describing the tools for flag enums */
export interface EnumFlagsTool<E, e> {
state: e, // Returns map of T/F values
eql: (a: E) => boolean;
has: (a: E) => boolean;
any: (a: E) => boolean;
toArray: () => string[];
toString: () => string;
}
/** Static properties on the function returned by EnumFlagsType<E,e> */
export interface EnumFlagsFunc<E, e> {
(val?): EnumFlagsTool<E, e>,
toArray: () => { key: string, val: number }[];
val: e,
key: e,
}
/** Interface describing the tools for string enums */
export interface EnumStringsTool<E, e> {
state: e, // Returns map of T/F values
equals: (a: E) => boolean;
toStringKey: () => string;
toStringVal: () => string;
}
/** Static properties on the function returned by EnumStringsType<E,e> */
export interface EnumStringsFunc<E, e> {
(str?): EnumStringsTool<E, e>,
toArray: () => { key: string, val: string }[];
val: e,
key: e,
}
/**
* Fastest methods are these simple no frills comparison tests.
*/
export var EnumFlagsTest = {
has: function(val, flags) {
return ((+val & +flags) === +flags);
},
any: function(val, flags) {
return !!(+val & +flags);
},
eql: function(val, flags) {
return (+val === +flags);
}
}
/**
* This alternative using prototype method may be faster - WTH ?
*/
export var EnumFlagsTestAlt = function(val) {
this.val = val;
};
EnumFlagsTestAlt.prototype.has = function(flags) {
return ((+this.val & +flags) === +flags)
};
/**
* Returns a tools assortment for flag enums, and optionally implements them as a property of
* Number.prototype (named getter). The tools automatically bind to each unique Number.
*/
export function EnumFlagsType<E, e>(enumeration, prop?: string): EnumFlagsFunc<E, e> {
var keys = {};
var hash = Object.keys(enumeration).reduce(function(obj, k) {
// Excludes bi-directional numeric keys
if (isNaN(<any>k)) {
obj[k] = +enumeration[k];
}
keys[k] = k;
return obj;
}, {});
// New instance created per each binding
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;
}});
});
// New instance created per each binding
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]); // all bits weed out combos
}.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(' | ');
};
// This either runs in the context of a primitive or a value must be provided
var BindValue: any = function BindValue(val?: number): EnumFlagsTool<E, e> {
return new Methods((val === undefined) ? this : val);
};
BindValue.val = hash;
BindValue.key = keys;
BindValue.toArray = function() {
let arr = [];
Object.keys(hash).map(function(k) {
arr.push({ key: k, val: hash[k] });
});
};
// If property name is given, a number prototype is set
if (prop) {
Object.defineProperty(Number.prototype, prop, { get: BindValue });
}
return BindValue;
}
/**
* Returns a tools assortment for string enums, and optionally implements them as a property of
* String.prototype (named getter). The tools automatically bind to each unique string.
*/
export function EnumStringsType<E, e>(enumeration, prop?: string, validKeysFilter?: Function): EnumStringsFunc<E, e> {
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;
});
}
// New instance created per each binding
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]);
}});
});
// New instance created per each binding
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;
};
// This either runs in the context of a primitive or a string must be provided
var BindString: any = function (str?: string): EnumStringsTool<E, e> {
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 property name is given, a string prototype is set
if (prop) {
Object.defineProperty(String.prototype, prop, { get: BindString });
}
return BindString;
}