UNPKG

1.28 kBJavaScriptView Raw
1function mod(x, y) {
2 return x - y * Math.floor(x / y);
3}
4function is(v) {
5 return v != null && typeof v[Symbol.iterator] === "function";
6}
7function isOnce(v) {
8 return is(v) && v[Symbol.iterator]() === v;
9}
10function manyLate(x, a) {
11 return { [Symbol.iterator]: () => {
12 var i = 0, xi = x[Symbol.iterator]();
13 return { next: () => {
14 if (i < a.length)
15 return { value: a[i++], done: false };
16 var { value, done } = xi.next();
17 if (!done)
18 a[i++] = value;
19 return { value, done };
20 } };
21 } };
22}
23function many(x, now = false) {
24 if (!isOnce(x))
25 return x;
26 return now ? Array.from(x) : manyLate(x, []);
27}
28const END = Number.MAX_SAFE_INTEGER;
29function size(x, i = 0, I = END) {
30 var j = -1, n = 0;
31 for (var _ of x)
32 if (++j >= i && j < I)
33 n++;
34 return n;
35}
36function* cycle(x, i = 0, n = -1) {
37 var x = many(x);
38 var i = i === 0 ? 0 : mod(i, size(x));
39 while (true) {
40 for (var v of x) {
41 if (--i >= 0)
42 continue;
43 if (n-- === 0)
44 return;
45 yield v;
46 }
47 }
48}
49export default cycle;