-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
585 lines (449 loc) · 17.6 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
'use strict';
function Enum(source, nameOrIsWrapped1, nameOrIsWrapped2, compare) {
/////////////Initializations///////////////////////////////////////////////////////////////////////////////////////////
var enumTypeDefinition = {};
var typeName = "";
var isWrapped = true;
var labels = []; //a list of the property names in order
var values = []; //a list of the property values in order
var dictionary = {};
var testEquality = null;
////////////////Resolve 2nd, 3rd, and 4th optional parameters///////////////////////////////////////////////////////////
(function() {
var nameOrIsWrapped1_type = typeof nameOrIsWrapped1;
var nameOrIsWrapped2_type = typeof nameOrIsWrapped2;
var compare_type = typeof compare;
if (nameOrIsWrapped1_type === "boolean") {
isWrapped = nameOrIsWrapped1;
if (nameOrIsWrapped2_type === "string") {
typeName = nameOrIsWrapped2;
} else if (!nameOrIsWrapped2) {
//placeholder to prevent exception from throwing for the case of an omitted optional parameter or a null parameter
} else {
throw "A string was expected for your third parameter. Enum type was not constructed.";
}
} else if (nameOrIsWrapped1_type === "string") {
typeName = nameOrIsWrapped1;
if (nameOrIsWrapped2_type === "boolean") {
isWrapped = nameOrIsWrapped2;
} else if (!nameOrIsWrapped2) {
//placeholder to prevent exception from throwing for the case of an omitted optional parameter or a null parameter
} else {
throw "A boolean was expected for your third parameter. Enum type was not constructed.";
}
} else if (!nameOrIsWrapped1) {
if (nameOrIsWrapped2_type === "boolean") {
isWrapped = nameOrIsWrapped2;
} else if (nameOrIsWrapped2_type === "string") {
typeName = nameOrIsWrapped2;
} else if (!nameOrIsWrapped2) {
//placeholder to prevent exception from throwing for the case of an omitted optional parameter or a null parameter
} else {
throw "A string, boolean, or omission was expected for your third parameter. Enum type was not constructed.";
}
}
if (compare_type === "function") {
if (compare.length === 2) {
testEquality = compare;
} else {
throw "Your equality function must take two parameters. Enum type was not constructed.";
}
} else if (!compare) {
} else {
throw "The fourth parameter must be a function that accepts two objects and returns a boolean indicating if the objects are equal. Enum type was not constructed.";
}
})();
////////////////Construct the data structures enum relies on from source////////////////////////////////////////////////////////////////////////////
(function() {
if (source.constructor === Array) {
if (source.length === 0) {
throw "The first argument cannot have zero elements. Enum type was not successfully created.";
}
if (typeof source[0] === "object") { //if source is formatted: [{userlabel:"userValue"},{userlabel2:"userValue"},{userlabel3:"userValue"},...]
for (var i = 0; i < source.length; i++) {
var label = Object.getOwnPropertyNames(source[i])[0];
var value = source[i][label];
labels.push(label);
values.push(value);
dictionary[label] = value;
}
} else { //if source is formated: ["userValue", "userValue", "userValue",...]
for (var iter = 0; iter < source.length; iter++) {
labels.push(source[iter]);
values.push(source[iter]);
dictionary[source[iter]] = source[iter];
}
}
} else if (typeof source === "object" && source !== null) { //if source is formatted: {userlabel : "userValue", userlabel : "userValue", userLabel : "userValue"}
dictionary = source;
labels = Object.getOwnPropertyNames(source);
for (var lbl in source) {
values.push(source[lbl]);
}
} else {
throw "You must supply a first parameter that is either an array or a filled object. Enum type was not successfully created.";
}
})();
/*Enum Definition properties////////////////////////////////////////////////////////////////////
*
*
*/ //////////////////////////////////////////////////////////////////////////////////////////////
function eLabelOfValue(value) {
return labels[eIndexOfValue(value)];
}
function eValueOfLabel(label){
return dictionary[label];
}
function eLabelOfIndex(index) {
var returnValue = null;
if (isInteger(index)) {
if (isWrapped === false) {
if (index > 0 && index < labels.length) {
return labels[index];
} else {
throw "Enum index was not gotten in eGetValueByIndex() call. The index was outside the range of possible values. " +
"If you were expecting cyclical indexing, consider redefining your Enum definition setting the second or " +
"third parameter of the constructor to true. You can also omit the boolean parameter for the same effect.";
}
} else {
returnValue = labels[(labels.length + (index % labels.length)) % labels.length]; //wrap around
}
} else {
throw "Index parameter must be an integer. Index was not set.";
}
return returnValue;
}
function eValue(numberOrLabel) {
var returnValue = null;
if (isInteger(numberOrLabel)) {
returnValue = eValueOfIndex(numberOrLabel);
} else if (typeof numberOrLabel === "string") {
returnValue = eValueOfLabel(numberOrLabel);
} else {
throw "The parameter for eValue() must be a number or a string. Enum value was not returned.";
}
return returnValue;
}
function eValueOfIndex(index) {
if (isInteger(index)) {
if (isWrapped === false) {
if (index > 0 && index < labels.length) {
return values[index];
} else {
throw "Enum index was not gotten in eGetValueByIndex() call. The index was outside the range of possible values. " +
"If you were expecting cyclical indexing, consider redefining your Enum definition setting the second or " +
"third parameter of the constructor to true. You can also omit the boolean parameter for the same effect.";
}
} else {
return values[(labels.length + (index % labels.length)) % labels.length]; //wrap around
}
} else {
throw "Index parameter must be an integer. Index was not set.";
}
}
function eIndexOfLabel(label) {
if (typeof label !== "string") {
throw "The parameter of eIndexOfLabel() must be of type string. Index was not returned.";
}
var result = labels.indexOf(label);
if (result === -1) {
throw "Label was not found in Enum Definition in call to eIndexOfLabel().";
}
return result;
}
function eIndexOfValue(value) {
//YOU"RE WORKING HERE/////////////////////////////////////////////////////////
//return values.indexOf(value);
var returnValue = null;
if (testEquality !== null) { //if the equality parameter was passed to the constructor
var result = false;
for (var i = 0; i < values.length; i++) {
result = testEquality(value, values[i]);
if (result === true) {
returnValue = i;
}
}
if (result === false) {
throw "Could not get index of value in the call to eIndexOfValue(), value was not found. Ensure that an object meeting the criteria" +
" of your equality parameter exists in the Enum definition " +
"and that the criteria of your equality function is behaving as you intend it to.";
}
} else {
var index = values.indexOf(value);
if (index !== -1) {
returnValue = index;
} else {
throw "Could not get index of value in the call to eIndexOfValue(), value was not found. If your value is an object(non-primitive), " +
"please note that setByValue() determines value equivalence using === which compares objects by reference. " +
"If it was not your intention to test that your object was the same instance as the value for the Enum, please" +
"make sure that you include (or included) a compare function parameter (the 4th one) to test equality in your Enum definition.";
}
}
return returnValue;
}
function eFirst() {
return values[0];
}
function eLast() {
return values[values.length - 1];
}
function eInverse(numberOrLabel) {
if (isInteger(numberOrLabel)) {
return eValueOfIndex(-numberOrLabel);
} else if (typeof numberOrLabel === "string") {
return eValueOfIndex(-eIndexOfLabel(numberOrLabel));
} else {
throw "The parameter for eValue() must be a number or a string. Enum value was not returned.";
}
}
function eQuotient(divisor){
return Math.floor(labels.length / divisor);
}
function eModulo(divisor){
return labels.length % divisor;
}
/*//////This function generates an instance of the Enum definition////////////////////////
*
*
*/ ////////////////////////////////////////////////////////////////////////////////////////
function eCreate(initializationValue) {
var enumObject = {};
var iterator = 0;
//initialize object///////////////////////
(function() {
if (isInteger(initializationValue)) {
setByIndex(initializationValue);
} else if (typeof initializationValue === "string") {
setByLabel(initializationValue);
} else if (!initializationValue) {
iterator = 0;
} else {
throw "Enum instance not created using eCreate() due to an incorrectly-typed initialization parameter." +
" You can only assign new instances by enum index (Number) or enum label (String). " +
"If you need to assign the instance a particular value, please call the instance's setByValue() method.";
}
})();
function increment() {
iterator++;
if (iterator > labels.length - 1) {
if (isWrapped === true) {
iterator = 0;
} else {
throw "Enum cannot be incremented beyond the maximum index. Increment failed. Did you intend to construct an enum type with wrap around set to true?";
}
}
}
function decrement() {
iterator--;
if (iterator < 0) {
if (isWrapped === true) {
iterator = labels.length - 1;
} else {
throw "Enum cannot be decremented beyond the minimum index. Decrement failed. Did you intend to construct an enum type with wrap around set to true?";
}
}
}
function setToNext() {
increment();
}
function setToPrevious() {
decrement();
}
function getTypeName() {
return typeName;
}
function getDefinition() {
return enumTypeDefinition;
}
function getDictionary() {
return dictionary;
}
function getLabel() {
return labels[iterator];
}
function setByLabel(label) {
var index = labels.indexOf(label);
if (index !== -1) {
iterator = index;
} else {
throw "Could not setByLabel(), label was not found.";
}
}
function getValue() {
return values[iterator];
}
function setByValue(value) {
if (testEquality !== null) { //if the equality parameter was passed to the constructor
var result = false;
for (var i = 0; i < values.length; i++) {
result = testEquality(value, values[i]);
if (result === true) {
iterator = i;
break;
}
}
if (result === false) {
throw "Could not setByValue(), value was not found. Ensure that an object meeting the criteria" +
" of your equality parameter exists in the Enum definition " +
"and that the criteria of your equality function is behaving as you intend it to.";
}
} else {
var index = values.indexOf(value);
if (index !== -1) {
iterator = index;
} else {
throw "Could not setByValue(), value was not found. If your value is an object(non-primitive), " +
"please note that setByValue() determines value equivalence using === which compares objects by reference. " +
"If it was not your intention to test that your object was the same instance as the value for the Enum, please" +
"make sure that you include (or included) a compare function parameter (the 4th one) to test equality in your Enum definition.";
}
}
}
function getIndex() {
return iterator;
}
function setByIndex(index) {
if (isInteger(index)) {
if (isWrapped === false) {
if (index > 0 && index < labels.length) {
iterator = index;
} else {
throw "Enum index was not set in setByIndex() call. The index was outside the range of possible values. " +
"If you were expecting cyclical indexing, consider redefining your Enum definition setting the second or " +
"thrid parameter to true. You can also omit the boolean parameter for the same effect.";
}
} else {
iterator = (labels.length + (index % labels.length)) % labels.length; //wrap around
}
} else {
throw "Index parameter must be an integer. Index was not set.";
}
}
function add(index) {
if (isInteger(index)) {
setByIndex(iterator + index);
} else {
throw "The value to add must be an integer.";
}
}
function subtract(index) {
if (isInteger(index)) {
setByIndex(iterator - index);
} else {
throw "The value to subtract must be an integer.";
}
}
function multiply(index) {
if (isInteger(index)) {
setByIndex(index * iterator);
} else {
throw "The value to multiply must be an integer.";
}
}
function getInverse() {
return eInverse(iterator);
}
function invert() {
setByIndex(-iterator);
}
function getPrevious() {
var previous = iterator - 1;
if (previous < 0) {
if (isWrapped === true) {
previous = labels.length - 1;
} else {
throw "There is no previous. getPrevious() failed. Did you intend to construct an enum type with wrap around set to true?";
}
}
return values[previous];
}
function getNext() {
var next = iterator + 1;
if (next > labels.length - 1) {
if (isWrapped === true) {
next = 0;
} else {
throw "There is no next. getNext() failed. Did you intend to construct an enum type with wrap around set to true?";
}
}
return values[next]; //need to do wrap around
}
function setToFirst() {
iterator = 0;
}
function setToLast() {
iterator = values.length - 1;
}
enumObject.increment = increment;
enumObject.decrement = decrement;
enumObject.setToNext = setToNext;
enumObject.setToPrevious = setToPrevious;
enumObject.getTypeName = getTypeName;
enumObject.getDefinition = getDefinition;
enumObject.getDictionary = getDictionary;
enumObject.getLabel = getLabel;
enumObject.setByLabel = setByLabel;
enumObject.getValue = getValue;
enumObject.setByValue = setByValue;
enumObject.getIndex = getIndex;
enumObject.setByIndex = setByIndex;
enumObject.add = add;
enumObject.subtract = subtract;
enumObject.multiply = multiply;
enumObject.getInverse = getInverse;
enumObject.invert = invert;
enumObject.getPrevious = getPrevious;
enumObject.getNext = getNext;
enumObject.setToFirst = setToFirst;
enumObject.setToLast = setToLast;
Object.freeze(enumObject);
return enumObject;
}
//Attach Enum Definition Properties
enumTypeDefinition.eType = typeName;
enumTypeDefinition.eWrapped = isWrapped;
enumTypeDefinition.eLabels = labels;
enumTypeDefinition.eValues = values;
enumTypeDefinition.eDictionary = dictionary;
enumTypeDefinition.eLength = labels.length;
enumTypeDefinition.eLabelOfValue = eLabelOfValue;
enumTypeDefinition.eValueOfLabel = eValueOfLabel;
enumTypeDefinition.eLabelOfIndex = eLabelOfIndex;
enumTypeDefinition.eValue = eValue;
enumTypeDefinition.eValueOfIndex = eValueOfIndex;
enumTypeDefinition.eIndexOfLabel = eIndexOfLabel;
enumTypeDefinition.eIndexOfValue = eIndexOfValue;
enumTypeDefinition.eFirst = eFirst;
enumTypeDefinition.eLast = eLast;
enumTypeDefinition.eInverse = eInverse;
enumTypeDefinition.eCreate = eCreate;
//////////////////////////////Freeze all the data and generate enum properties from dictionary/////////////////////////////////////////////
(function() {
for (var key in dictionary) {
if (enumTypeDefinition.hasOwnProperty(key)) {
throw "Enum labels cannot have the same name as reserved Enum properties: " +
"eType, eWrapped, eLabels, eValues, eDictionary, eLength, eLabelByIndex, " +
"eLabelByValue, eValue, eIndexOfLabel, eIndexOfValue, eCreate, or inherited others. Enum type was not created.";
}
enumTypeDefinition[key] = dictionary[key];
}
deepFreeze(enumTypeDefinition);
})();
return enumTypeDefinition;
}
/*/////Utility Functions//////////////////////////////////////////////////////////////////////////////////////////
*
*
*/ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
function deepFreeze(obj) {
if (typeof obj === 'object') {
Object.freeze(obj);
for (var property in obj) {
deepFreeze(obj[property]);
}
}
}
function isInteger(x) {
return (typeof x === 'number') && (x % 1 === 0);
}
module.exports = Enum;