1function range(start, end) {
2 /* generate a range : [start, start+1, ..., end-1, end] */
3 var len = end - start + 1;
4 var a = new Array(len);
5 for (let i = 0; i < len; i++) a[i] = start + i;
6 return a;
7}
1for (i of range(1, 5)) {
2 console.log(i);
3}
4/* Output
5 * 1 2 3 4 5 */
6
7[...range(1, 5)] // [1, 2, 3, 4, 5]
8
1function range(start, stop, step = 1, circularFill = false, map = (value) => value) {
2 if (typeof stop === 'undefined') {
3 stop = start;
4 start = 0;
5 }
6
7 if (step > 0 && start >= stop) {
8 step = -step;
9 }
10
11 if (step < 0 && start <= stop) {
12 return [];
13 }
14
15 let index = start;
16 const result = [];
17
18 if (circularFill) {
19 const size = start + stop;
20 for (index; step > 0 ? index < size : index > size; index += step) {
21 result.push(map(index % stop));
22 }
23 return result;
24 }
25
26 for (index; step > 0 ? index < stop : index > stop; index += step) {
27 result.push(map(index));
28 }
29
30 return result;
31}
32
33// Range examples:
34
35range(8)
36// [0, 1, 2, 3, 4, 5, 6, 7]
37
38range(-8)
39// [0, -1, -2, -3, -4, -5, -6, -7]
40
41range(25, 30, 2);
42// [25, 27, 29]
43
44range(-25, -30, -2);
45// [-25, -27, -29]
46
47range(5, 10, 1, true)
48// [5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
49
50range(-5, -10, -1, true)
51// [-5, -6, -7, -8, -9, -0, -1, -2, -3, -4]
52
53function rangeChars(text, start, stop, step, circularFill, map = (value) => value) {
54 start = text.indexOf(start);
55 stop = text.indexOf(stop) + 1;
56 return range(start, stop, step, circularFill, (i) => {
57 return map(text[i]);
58 });
59}
60rangeChars('shooooouuut!!!!', 'o', '!');
61// ["o", "o", "o", "o", "o", "u", "u", "u", "t", "!"]
1
2function range(start, end, interval = 0) {
3 let arr = [];
4 interval = interval > 0 ? interval-1 : 0
5 for (let i = start; i < end; i++) {
6 arr.push(i)
7 i+=interval;
8 }
9 return arr
10 }
11// example range(0 , 100 , 5)