Coverage

62%
123
77
46

enum.js

62%
123
77
46
LineHitsSource
11function Enum(next, count, clone) {
221 this.next = next;
321 this.count = count;
421 this.clone = clone;
5}
61module.exports = Enum;
7
8/**
9 * Enum#iter(function)
10 *
11 * e.iter(f) calls the function f with each element of e in turn. This
12 * consumes the enumeration.
13 **/
141Enum.prototype.iter = function(fn) {
152 var curr;
162 while ((curr = this.next()) !== null)
1711 fn(curr);
18};
19
20/**
21 * Enum#find(function) -> Element
22 *
23 * e.find(f) returns the first element x of e such that f(x) returns true,
24 * consuming the enumeration up to and including the found element, or,
25 * returns null if no such element exists in the enumeration, consuming the
26 * whole enumeration in the search. Since find consumes a prefix of the
27 * enumeration, it can be used several times on the same enumeration to find
28 * the next element.
29 **/
301Enum.prototype.find = function(fn) {
314 var curr, found;
324 while ((curr = this.next()) !== null) {
3316 if (fn(curr) === true) {
341 found = curr;
351 break;
36 }
37 }
38
394 return found || null;
40};
41
421Enum.prototype.isEmpty = function() {
432 if (this.fast)
440 return this.count() === 0;
45 else
462 return this.peek() === null;
47};
48
49// peek e returns None if e is empty or Some x where x is the next element of e.
50// The element is not removed from the enumeration.
511Enum.prototype.peek = function() {
527 var next = this.get();
537 if (next !== null)
545 this.push(next);
55
567 return next;
57};
58
591Enum.prototype.get = function() {
6025 return this.next();
61};
62
631Enum.prototype.junk = function() {
640 this.next();
65};
66
671Enum.prototype.push = function(e) {
686 var make = function(self) {
696 var fNext = self.next;
706 var fCount = self.count;
716 var fClone = self.clone;
726 var nextCalled = false;
73
746 self.next = function() {
754 nextCalled = true;
764 self.next = fNext;
774 self.count = fCount;
784 self.clone = fClone;
79
804 return e;
81 };
82
836 self.count = function() {
841 var n = fCount();
851 return nextCalled === true ? n : n + 1;
86 };
87
886 self.clone = function() {
890 var c = fClone();
900 if (nextCalled === false)
910 make(c);
92
930 return c;
94 };
95 };
96
976 make(this);
98};
99
1001Enum.make = function(next, count, clone) {
1010 var e = new Enum();
1020 e.next = next;
1030 e.count = count;
1040 e.clone = clone;
105
1060 return e;
107};
108
1091Enum.empty = function() {
1100 var e = new Enum();
1110 e.next = function() { return null; };
1120 e.count = function() { return 0; };
1130 e.clone = function() { return e.empty(); };
1140 e.fast = true;
115
1160 return e;
117};
118
1191Enum.init = function(n, fn) {
1200 var e = new Enum();
121
1220 var count = n;
1230 e.count = function() {
1240 return count;
125 };
126
1270 e.next = function() {
1280 if (count === 0) {
1290 throw "No more elements";
130 }
131 else {
1320 count -= 1;
1330 return fn(n - 1 - count);
134 }
135 };
136
1370 e.clone = function() {
1380 return e.init(count, fn);
139 };
140
1410 e.fast = true;
142};
143
1441Enum.prototype.map = function(fn) {
1452 var self = this;
1462 return (function() {
1472 this.next = function() {
1484 return fn(self.next());
149 };
150
1512 this.count = self.count;
152
1532 this.clone = function() {
1541 return self.clone().map(fn);
155 };
156
1572 this.fast = self.fast;
158
1592 return this;
160 }).call(new Enum());
161};
162
1631Enum.prototype.filter = function(fn) {
164
165};
166
1671Enum.fromArray = function(array) {
16819 return (function() {
16919 var counter = 0;
17019 this.next = function() {
17154 var value = null;
17254 if (typeof array[counter] !== "undefined") {
17346 value = array[counter];
17446 counter += 1;
175 }
176
17754 return value;
178 };
179
18019 this.count = function() {
1816 return array.length - counter; // todo: not good probably
182 };
183
18419 this.clone = function() {
1852 var e = Enum.fromArray([1, 2, 3, 4, 5, 6]);
1862 e._setCounter(counter);
1872 return e;
188 };
189
19019 this._setCounter = function(n) {
1912 counter = n;
192 };
193
19419 return this;
195 }).call(new Enum());
196};
197
1981Enum.fromString = function(string) {
1990 return (function() {
2000 var counter = 0;
2010 this.next = function() {
2020 var value = null;
2030 if (typeof string[counter] !== "undefined") {
2040 value = string[counter];
2050 counter += 1;
206 }
207
2080 return value;
209 };
210
2110 this.count = function() {
2120 return array.length - counter; // todo: not good probably
213 };
214
2150 this.clone = function() {
2160 var e = Enum.fromString(string);
2170 e._setCounter(counter);
2180 return e;
219 };
220
2210 this._setCounter = function(n) {
2220 counter = n;
223 };
224
2250 return this;
226 }).call(new Enum());
227};
228