Skip to content

Instantly share code, notes, and snippets.

@hemanth
Created June 28, 2012 09:44
Show Gist options
  • Select an option

  • Save hemanth/3010278 to your computer and use it in GitHub Desktop.

Select an option

Save hemanth/3010278 to your computer and use it in GitHub Desktop.
Create an array from Range in node
Array.prototype.range = function(str) {
var inclusive = str.indexOf("...") > -1;
var p = str.split( ((inclusive) ? "..." : "..") );
p[0] = parseInt(p[0]), p[1] = parseInt(p[1]) - (( inclusive ) ? 0 : 1);
var n = []; for ( var i=p[0]; i<=p[1]; i++ ) n.push(i); return n;
}
console.log( [].range("1...20") );
// prints
// [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
console.log( [].range("1..9") );
// [ 1, 2, 3, 4, 5, 6, 7, 8 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment