Skip to content

Commit 6c4152c

Browse files
committed
Added Array.Range - which allows for complex ranges of 2 arrays.
1 parent 3e8e614 commit 6c4152c

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

v3/src/utils/array/Range.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
var Shuffle = require('./Shuffle');
2+
var GetObjectValue = require('../object/GetObjectValue');
3+
4+
var BuildChunk = function (a, b, qty)
5+
{
6+
var out = [];
7+
8+
for (var aIndex = 0; aIndex < a.length; aIndex++)
9+
{
10+
for (var bIndex = 0; bIndex < b.length; bIndex++)
11+
{
12+
for (var i = 0; i < qty; i++)
13+
{
14+
out.push({ a: a[aIndex], b: b[bIndex] });
15+
}
16+
}
17+
}
18+
19+
return out;
20+
};
21+
22+
// options = repeat, random, randomB, yoyo, max, qty
23+
24+
// Range ([a,b,c], [1,2,3]) =
25+
// a1, a2, a3, b1, b2, b3, c1, c2, c3
26+
27+
// Range ([a,b], [1,2,3], qty = 3) =
28+
// a1, a1, a1, a2, a2, a2, a3, a3, a3, b1, b1, b1, b2, b2, b2, b3, b3, b3
29+
30+
// Range ([a,b,c], [1,2,3], repeat x1) =
31+
// a1, a2, a3, b1, b2, b3, c1, c2, c3, a1, a2, a3, b1, b2, b3, c1, c2, c3
32+
33+
// Range ([a,b], [1,2], repeat -1 = endless, max = 14) =
34+
// Maybe if max is set then repeat goes to -1 automatically?
35+
// a1, a2, b1, b2, a1, a2, b1, b2, a1, a2, b1, b2, a1, a2 (capped at 14 elements)
36+
37+
// Range ([a], [1,2,3,4,5], random = true) =
38+
// a4, a1, a5, a2, a3
39+
40+
// Range ([a, b], [1,2,3], random = true) =
41+
// b3, a2, a1, b1, a3, b2
42+
43+
// Range ([a, b, c], [1,2,3], randomB = true) =
44+
// a3, a1, a2, b2, b3, b1, c1, c3, c2
45+
46+
// Range ([a], [1,2,3,4,5], yoyo = true) =
47+
// a1, a2, a3, a4, a5, a5, a4, a3, a2, a1
48+
49+
// Range ([a, b], [1,2,3], yoyo = true) =
50+
// a1, a2, a3, b1, b2, b3, b3, b2, b1, a3, a2, a1
51+
52+
var Range = function (a, b, options)
53+
{
54+
var max = GetObjectValue(options, 'max', 0);
55+
var qty = GetObjectValue(options, 'qty', 1);
56+
var random = GetObjectValue(options, 'random', false);
57+
var randomB = GetObjectValue(options, 'randomB', false);
58+
var repeat = GetObjectValue(options, 'repeat', 0);
59+
var yoyo = GetObjectValue(options, 'yoyo', false);
60+
61+
var out = [];
62+
63+
if (randomB)
64+
{
65+
Shuffle(b);
66+
}
67+
68+
// Endless repeat, so limit by max
69+
if (repeat === -1)
70+
{
71+
if (max === 0)
72+
{
73+
repeat = 0;
74+
}
75+
else
76+
{
77+
// Work out how many repeats we need
78+
var total = (a.length * b.length) * qty;
79+
80+
if (yoyo)
81+
{
82+
total *= 2;
83+
}
84+
85+
repeat = Math.ceil(max / total);
86+
}
87+
}
88+
89+
for (var i = 0; i <= repeat; i++)
90+
{
91+
var chunk = BuildChunk(a, b, qty);
92+
93+
if (random)
94+
{
95+
Shuffle(chunk);
96+
}
97+
98+
out = out.concat(chunk);
99+
100+
if (yoyo)
101+
{
102+
chunk.reverse();
103+
104+
out = out.concat(chunk);
105+
}
106+
}
107+
108+
if (max)
109+
{
110+
out.splice(max);
111+
}
112+
113+
return out;
114+
};
115+
116+
module.exports = Range;

v3/src/utils/array/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
NumberArray: require('./NumberArray'),
66
NumberArrayStep: require('./NumberArrayStep'),
77
QuickSelect: require('./QuickSelect'),
8+
Range: require('./Range'),
89
RemoveRandomElement: require('./RemoveRandomElement'),
910
RotateLeft: require('./RotateLeft'),
1011
RotateRight: require('./RotateRight'),

0 commit comments

Comments
 (0)