65 lines
1.4 KiB
JavaScript
65 lines
1.4 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
function _cycler(items) {
|
||
|
var index = -1;
|
||
|
return {
|
||
|
current: null,
|
||
|
reset: function reset() {
|
||
|
index = -1;
|
||
|
this.current = null;
|
||
|
},
|
||
|
next: function next() {
|
||
|
index++;
|
||
|
if (index >= items.length) {
|
||
|
index = 0;
|
||
|
}
|
||
|
this.current = items[index];
|
||
|
return this.current;
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
function _joiner(sep) {
|
||
|
sep = sep || ',';
|
||
|
var first = true;
|
||
|
return function () {
|
||
|
var val = first ? '' : sep;
|
||
|
first = false;
|
||
|
return val;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
// Making this a function instead so it returns a new object
|
||
|
// each time it's called. That way, if something like an environment
|
||
|
// uses it, they will each have their own copy.
|
||
|
function globals() {
|
||
|
return {
|
||
|
range: function range(start, stop, step) {
|
||
|
if (typeof stop === 'undefined') {
|
||
|
stop = start;
|
||
|
start = 0;
|
||
|
step = 1;
|
||
|
} else if (!step) {
|
||
|
step = 1;
|
||
|
}
|
||
|
var arr = [];
|
||
|
if (step > 0) {
|
||
|
for (var i = start; i < stop; i += step) {
|
||
|
arr.push(i);
|
||
|
}
|
||
|
} else {
|
||
|
for (var _i = start; _i > stop; _i += step) {
|
||
|
// eslint-disable-line for-direction
|
||
|
arr.push(_i);
|
||
|
}
|
||
|
}
|
||
|
return arr;
|
||
|
},
|
||
|
cycler: function cycler() {
|
||
|
return _cycler(Array.prototype.slice.call(arguments));
|
||
|
},
|
||
|
joiner: function joiner(sep) {
|
||
|
return _joiner(sep);
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
module.exports = globals;
|