Skip to content

Instantly share code, notes, and snippets.

@westc
Last active July 7, 2020 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westc/d7cf6cbad9216638366bb6e83fd716aa to your computer and use it in GitHub Desktop.
Save westc/d7cf6cbad9216638366bb6e83fd716aa to your computer and use it in GitHub Desktop.
Creates a span of numbers as an array.
/**
* Creates an array with the range of numbers going from `first` and ending but
* not including `limit` depending on the specified step value.
* @name range
* @param {number} first
* First number that should appear in the returned array.
* @param {number} limit
* The number just outside of the range which serves as a limit for the last
* number in the returned array.
* @param {number=} opt_step
* Defaults to `1` if not given or if `0` or `NaN` is specified. The
* difference between each subsequent number in the returned array.
* @returns {Array}
* An array containing the sequence of numbers starting at `first` and ending
* at a number before `limit` with each number being separated by `opt_step`.
*/
/**
* Creates an array with the span of numbers going from `first` and ending at
* `last` if possible depending on the specified step value.
* @name span
* @param {number} first
* First number that should appear in the returned array.
* @param {number} last
* Last number that should appear in the returned array.
* @param {number=} opt_step
* Defaults to `1` if not given or if `0` or `NaN` is specified. The
* difference between each subsequent number in the returned array.
* @returns {Array}
* An array containing the sequence of numbers starting at `first` and ending
* at `last`. If `first` is less than `last` and `opt_step` is less than `0`
* or if `last` is less than `first` and `opt_step` is greater than `0` an
* empty array will be returned.
*/
// Creates both the range() and span() functions.
eval('range span='.replace(/(\w+)(.)/g,'function $1(a,c,b){2>arguments.length&&(c=a,a=0);b=+b||1;for(var d=[],e=0>b?-1:1;0<$2e*(c-a);a+=b)d.push(a);return d}'));
/**
* Creates an array with the range of numbers going from `first` and ending but
* not including `limit` depending on the specified step value.
* @name range
* @param {number} first
* First number that should appear in the returned array.
* @param {number} limit
* The number just outside of the range which serves as a limit for the last
* number in the returned array.
* @param {number=} opt_step
* Defaults to `1` if not given or if `0` or `NaN` is specified. The
* difference between each subsequent number in the returned array.
* @returns {Array}
* An array containing the sequence of numbers starting at `first` and ending
* at a number before `limit` with each number being separated by `opt_step`.
*/
function range(first, limit, opt_step) {
if (arguments.length < 2) {
limit = first;
first = 0;
}
opt_step = +opt_step || 1;
let result = [];
for (let mult = opt_step < 0 ? -1 : 1; mult * (limit - first) > 0; first += opt_step) {
result.push(first);
}
return result;
}
/**
* Creates an array with the span of numbers going from `first` and ending at
* `last` if possible depending on the specified step value.
* @name span
* @param {number} first
* First number that should appear in the returned array.
* @param {number} last
* Last number that should appear in the returned array.
* @param {number=} opt_step
* Defaults to `1` if not given or if `0` or `NaN` is specified. The
* difference between each subsequent number in the returned array.
* @returns {Array}
* An array containing the sequence of numbers starting at `first` and ending
* at `last`. If `first` is less than `last` and `opt_step` is less than `0`
* or if `last` is less than `first` and `opt_step` is greater than `0` an
* empty array will be returned.
*/
function span(first, last, opt_step) {
if (arguments.length < 2) {
last = first;
first = 0;
}
opt_step = +opt_step || 1;
let result = [];
for (let mult = opt_step < 0 ? -1 : 1; mult * (last - first) >= 0; first += opt_step) {
result.push(first);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment