Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

0.4.6 is on the books, with kylichuku's range function

  • Loading branch information...
commit 4b2744a75adb3697ae2c99101704abdf512551dd 1 parent 64cac95
@jashkenas jashkenas authored
View
37 index.html
@@ -107,11 +107,11 @@
<p>
<table>
<tr>
- <td><a href="underscore.js">Development Version (0.4.5)</a></td>
+ <td><a href="underscore.js">Development Version (0.4.6)</a></td>
<td><i>18kb, Uncompressed with Comments</i></td>
</tr>
<tr>
- <td><a href="underscore-min.js">Production Version (0.4.5)</a></td>
+ <td><a href="underscore-min.js">Production Version (0.4.6)</a></td>
<td><i>2kb, Packed and Gzipped</i></td>
</tr>
</table>
@@ -186,7 +186,7 @@ <h2 id="styles">Object-Oriented and Functional Styles</h2>
<span class="methods"><a href="#first">first</a>, <a href="#rest">rest</a>, <a href="#last">last</a>,
<a href="#compact">compact</a>, <a href="#flatten">flatten</a>, <a href="#without">without</a>, <a href="#uniq">uniq</a>,
<a href="#intersect">intersect</a>, <a href="#zip">zip</a>, <a href="#indexOf">indexOf</a></span>,
- <a href="#lastIndexOf">lastIndexOf</a></span>
+ <a href="#lastIndexOf">lastIndexOf</a>, <a href="#range">range</a></span>
</p>
<p>
@@ -585,6 +585,28 @@ <h2 id="styles">Object-Oriented and Functional Styles</h2>
=&gt; 4
</pre>
+ <p id="range">
+ <b class="header">range</b><code>_.range([start], stop, [step])</code>
+ <br />
+ A function to create flexibly-numbered lists of integers, handy for
+ <tt>each</tt> and <tt>map</tt> loops. <b>start</b>, if omitted, defaults
+ to <i>0</i>; <b>step</b> defaults to <i>1</i>. Returns a list of integers
+ from <b>start</b> to <b>stop</b>, incremented (or decremented) by <b>step</b>,
+ exclusive.
+ </p>
+ <pre>
+_.range(10);
+=&gt; [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+_.range(1, 11);
+=&gt; [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+_.range(0, 30, 5);
+=&gt; [0, 5, 10, 15, 20, 25]
+_.range(0, -10, -1);
+=&gt; [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
+_.range(0);
+=&gt; []
+</pre>
+
<h2>Function (uh, ahem) Functions</h2>
<p id="bind">
@@ -931,6 +953,15 @@ <h2 id="styles">Object-Oriented and Functional Styles</h2>
<h2>Change Log</h2>
<p>
+ <b class="header">0.4.6</b><br />
+ Added the <tt>range</tt> function, a port of the
+ <a href="http://docs.python.org/library/functions.html#range">Python
+ function of the same name</a>, for generating flexibly-numbered lists
+ of integers. Original patch contributed by
+ <a href="http://github.com/kylichuku">Kirill Ishanov</a>.
+ </p>
+
+ <p>
<b class="header">0.4.5</b><br />
Added <tt>rest</tt> for Arrays and arguments objects, and aliased
<tt>first</tt> as <tt>head</tt>, and <tt>rest</tt> as <tt>tail</tt>,
View
11 test/arrays.js
@@ -86,4 +86,15 @@ $(document).ready(function() {
equals(result, 5, 'works on an arguments object');
});
+ test("arrays: range", function() {
+ equals(_.range(0).join(''), '', 'range with 0 as a first argument generates an empty array');
+ equals(_.range(4).join(' '), '0 1 2 3', 'range with a single positive argument generates an array of elements 0,1,2,...,n-1');
+ equals(_.range(5, 8).join(' '), '5 6 7', 'range with two arguments a & b, a<b generates an array of elements a,a+1,a+2,...,b-2,b-1');
+ equals(_.range(8, 5).join(''), '', 'range with two arguments a & b, b<a generates an empty array');
+ equals(_.range(3, 10, 3).join(' '), '3 6 9', 'range with three arguments a & b & c, c < b-a, a < b generates an array of elements a,a+c,a+2c,...,b - (multiplier of a) < c');
+ equals(_.range(3, 10, 15).join(''), '3', 'range with three arguments a & b & c, c > b-a, a < b generates an array with a single element, equal to a');
+ equals(_.range(12, 7, -2).join(' '), '12 10 8', 'range with three arguments a & b & c, a > b, c < 0 generates an array of elements a,a-c,a-2c and ends with the number not less than b');
+ equals(_.range(0, -10, -1).join(' '), '0 -1 -2 -3 -4 -5 -6 -7 -8 -9', 'final example in the Python docs');
+ });
+
});
View
15 test/generators.js
@@ -1,15 +0,0 @@
-$(document).ready(function() {
-
- module("Generator functions (range...)");
-
- test("generators: range", function() {
- equals(_.range(0).join(''), '', 'range with 0 as a first argument generates an empty array');
- equals(_.range(4).join(' '), '0 1 2 3', 'range with a single positive argument generates an array of elements 0,1,2,...,n-1');
- equals(_.range(5, 8).join(' '), '5 6 7', 'range with two arguments a & b, a<b generates an array of elements a,a+1,a+2,...,b-2,b-1');
- equals(_.range(8, 5).join(''), '', 'range with two arguments a & b, b<a generates an empty array');
- equals(_.range(3, 10, 3).join(' '), '3 6 9', 'range with three arguments a & b & c, c < b-a, a < b generates an array of elements a,a+c,a+2c,...,b - (multiplier of a) < c');
- equals(_.range(3, 10, 15).join(''), '3', 'range with three arguments a & b & c, c > b-a, a < b generates an array with a single element, equal to a');
- equals(_.range(12, 7, -2).join(' '), '12 10 8', 'range with three arguments a & b & c, a > b, c < 0 generates an array of elements a,a-c,a-2c and ends with the number not less than b');
- });
-});
-
View
4 test/speed.js
@@ -63,4 +63,8 @@
return _.intersect(numbers, randomized);
});
+ JSLitmus.test('_.range()', function() {
+ return _.range(1000);
+ });
+
})();
View
1  test/test.html
@@ -7,7 +7,6 @@
<script type="text/javascript" src="vendor/qunit.js"></script>
<script type="text/javascript" src="vendor/jslitmus.js"></script>
<script type="text/javascript" src="../underscore.js"></script>
- <script type="text/javascript" src="generators.js"></script>
<script type="text/javascript" src="collections.js"></script>
<script type="text/javascript" src="arrays.js"></script>
<script type="text/javascript" src="functions.js"></script>
View
17 underscore-min.js
@@ -1,2 +1,15 @@
-(function(){var j=this;var k=j._;var m=function(a){this._wrapped=a};var o=typeof StopIteration!=='undefined'?StopIteration:'__break__';var _=j._=function(a){return new m(a)};if(typeof exports!=='undefined')exports._=_;_.VERSION='0.4.5';_.range=function(a,b,c){if(!b){var b=a;a=0}if(!c)var c=1;var d=Math.ceil((b-a)/c);if(d<0){return[]}var e=new Array(d);var f=0;for(var i=a;(a<=b?b-i>0:i-b>0);i+=c){e[f++]=i}return e};_.each=function(a,b,c){var d=0;try{if(a.forEach){a.forEach(b,c)}else if(a.length){for(var i=0,l=a.length;i<l;i++)b.call(c,a[i],i,a)}else{for(var f in a)if(Object.prototype.hasOwnProperty.call(a,f)){b.call(c,a[f],f,a)}}}catch(e){if(e!=o)throw e;}return a};_.map=function(d,e,f){if(d&&d.map)return d.map(e,f);var g=[];_.each(d,function(a,b,c){g.push(e.call(f,a,b,c))});return g};_.reduce=function(d,e,f,g){if(d&&d.reduce)return d.reduce(_.bind(f,g),e);_.each(d,function(a,b,c){e=f.call(g,e,a,b,c)});return e};_.reduceRight=function(c,d,e,f){if(c&&c.reduceRight)return c.reduceRight(_.bind(e,f),d);var g=_.clone(_.toArray(c)).reverse();_.each(g,function(a,b){d=e.call(f,d,a,b,c)});return d};_.detect=function(d,e,f){var g;_.each(d,function(a,b,c){if(e.call(f,a,b,c)){g=a;_.breakLoop()}});return g};_.select=function(d,e,f){if(d.filter)return d.filter(e,f);var g=[];_.each(d,function(a,b,c){e.call(f,a,b,c)&&g.push(a)});return g};_.reject=function(d,e,f){var g=[];_.each(d,function(a,b,c){!e.call(f,a,b,c)&&g.push(a)});return g};_.all=function(d,e,f){e=e||_.identity;if(d.every)return d.every(e,f);var g=true;_.each(d,function(a,b,c){if(!(g=g&&e.call(f,a,b,c)))_.breakLoop()});return g};_.any=function(d,e,f){e=e||_.identity;if(d.some)return d.some(e,f);var g=false;_.each(d,function(a,b,c){if(g=e.call(f,a,b,c))_.breakLoop()});return g};_.include=function(b,c){if(_.isArray(b))return _.indexOf(b,c)!=-1;var d=false;_.each(b,function(a){if(d=a===c)_.breakLoop()});return d};_.invoke=function(b,c){var d=_.rest(arguments,2);return _.map(b,function(a){return(c?a[c]:a).apply(a,d)})};_.pluck=function(b,c){return _.map(b,function(a){return a[c]})};_.max=function(e,f,g){if(!f&&_.isArray(e))return Math.max.apply(Math,e);var h={computed:-Infinity};_.each(e,function(a,b,c){var d=f?f.call(g,a,b,c):a;d>=h.computed&&(h={value:a,computed:d})});return h.value};_.min=function(e,f,g){if(!f&&_.isArray(e))return Math.min.apply(Math,e);var h={computed:Infinity};_.each(e,function(a,b,c){var d=f?f.call(g,a,b,c):a;d<h.computed&&(h={value:a,computed:d})});return h.value};_.sortBy=function(e,f,g){return _.pluck(_.map(e,function(a,b,c){return{value:a,criteria:f.call(g,a,b,c)}}).sort(function(c,d){var a=c.criteria,b=d.criteria;return a<b?-1:a>b?1:0}),'value')};_.sortedIndex=function(a,b,c){c=c||_.identity;var d=0,high=a.length;while(d<high){var e=(d+high)>>1;c(a[e])<c(b)?d=e+1:high=e}return d};_.toArray=function(b){if(!b)return[];if(b.toArray)return b.toArray();if(_.isArray(b))return b;return _.map(b,function(a){return a})};_.size=function(a){return _.toArray(a).length};_.first=function(a,n){return n?Array.prototype.slice.call(a,0,n):a[0]};_.rest=function(a,b){return Array.prototype.slice.call(a,_.isUndefined(b)?1:b)};_.last=function(a){return a[a.length-1]};_.compact=function(b){return _.select(b,function(a){return!!a})};_.flatten=function(c){return _.reduce(c,[],function(a,b){if(_.isArray(b))return a.concat(_.flatten(b));a.push(b);return a})};_.without=function(b){var c=_.rest(arguments);return _.select(b,function(a){return!_.include(c,a)})};_.uniq=function(c,d){return _.reduce(c,[],function(a,b,i){if(0==i||(d?_.last(a)!=b:!_.include(a,b)))a.push(b);return a})};_.intersect=function(c){var d=_.rest(arguments);return _.select(_.uniq(c),function(b){return _.all(d,function(a){return _.indexOf(a,b)>=0})})};_.zip=function(){var a=_.toArray(arguments);var b=_.max(_.pluck(a,'length'));var c=new Array(b);for(var i=0;i<b;i++)c[i]=_.pluck(a,String(i));return c};_.indexOf=function(a,b){if(a.indexOf)return a.indexOf(b);for(var i=0,l=a.length;i<l;i++)if(a[i]===b)return i;return-1};_.lastIndexOf=function(a,b){if(a.lastIndexOf)return a.lastIndexOf(b);var i=a.length;while(i--)if(a[i]===b)return i;return-1};_.bind=function(a,b){var c=_.rest(arguments,2);return function(){return a.apply(b||j,c.concat(_.toArray(arguments)))}};_.bindAll=function(){var b=Array.prototype.pop.call(arguments);_.each(arguments,function(a){b[a]=_.bind(b[a],b)})};_.delay=function(a,b){var c=_.rest(arguments,2);return setTimeout(function(){return a.apply(a,c)},b)};_.defer=function(a){return _.delay.apply(_,[a,1].concat(_.rest(arguments)))};_.wrap=function(b,c){return function(){var a=[b].concat(_.toArray(arguments));return c.apply(c,a)}};_.compose=function(){var a=_.toArray(arguments);return function(){for(var i=a.length-1;i>=0;i--){arguments=[a[i].apply(this,arguments)]}return arguments[0]}};_.keys=function(c){return _.map(c,function(a,b){return b})};_.values=function(a){return _.map(a,_.identity)};_.extend=function(a,b){for(var c in b)a[c]=b[c];return a};_.clone=function(a){if(_.isArray(a))return a.slice(0);return _.extend({},a)};_.isEqual=function(a,b){if(a===b)return true;var c=typeof(a),btype=typeof(b);if(c!=btype)return false;if(a==b)return true;if(a.isEqual)return a.isEqual(b);if(_.isNumber(a)&&_.isNumber(b)&&isNaN(a)&&isNaN(b))return true;if(c!=='object')return false;var d=_.keys(a),bKeys=_.keys(b);if(d.length!=bKeys.length)return false;for(var e in a)if(!_.isEqual(a[e],b[e]))return false;return true};_.isEmpty=function(a){return(_.isArray(a)?a:_.values(a)).length==0};_.isElement=function(a){return!!(a&&a.nodeType==1)};_.isArray=function(a){return Object.prototype.toString.call(a)=='[object Array]'};_.isFunction=function(a){return Object.prototype.toString.call(a)=='[object Function]'};_.isString=function(a){return Object.prototype.toString.call(a)=='[object String]'};_.isNumber=function(a){return Object.prototype.toString.call(a)=='[object Number]'};_.isUndefined=function(a){return typeof a=='undefined'};_.noConflict=function(){j._=k;return this};_.identity=function(a){return a};_.breakLoop=function(){throw o;};var p=0;_.uniqueId=function(a){var b=p++;return a?a+b:b};_.functions=function(){var a=[];for(var b in _)if(Object.prototype.hasOwnProperty.call(_,b))a.push(b);return _.without(a,'VERSION','prototype','noConflict').sort()};_.template=function(a,b){var c=new Function('obj','var p=[],print=function(){p.push.apply(p,arguments);};'+'with(obj){p.push(\''+a.replace(/[\r\t\n]/g," ").split("<%").join("\t").replace(/((^|%>)[^\t]*)'/g,"$1\r").replace(/\t=(.*?)%>/g,"',$1,'").split("\t").join("');").split("%>").join("p.push('").split("\r").join("\\'")+"');}return p.join('');");return b?c(b):c};_.forEach=_.each;_.foldl=_.inject=_.reduce;_.foldr=_.reduceRight;_.filter=_.select;_.every=_.all;_.some=_.any;_.head=_.first;_.tail=_.rest;_.methods=_.functions;var q=function(a,b){return b?_(a).chain():a};_.each(_.functions(),function(a){m.prototype[a]=function(){Array.prototype.unshift.call(arguments,this._wrapped);return q(_[a].apply(_,arguments),this._chain)}});_.each(['pop','push','reverse','shift','sort','splice','unshift'],function(a){m.prototype[a]=function(){Array.prototype[a].apply(this._wrapped,arguments);return q(this._wrapped,this._chain)}});_.each(['concat','join','slice'],function(a){m.prototype[a]=function(){return q(Array.prototype[a].apply(this._wrapped,arguments),this._chain)}});m.prototype.chain=function(){this._chain=true;return this};m.prototype.value=function(){return this._wrapped}})();
-
+(function(){var j=this,m=j._,i=function(a){this._wrapped=a},l=typeof StopIteration!=="undefined"?StopIteration:"__break__",b=j._=function(a){return new i(a)};if(typeof exports!=="undefined")exports._=b;b.VERSION="0.4.6";b.each=function(a,c,d){try{if(a.forEach)a.forEach(c,d);else if(a.length)for(var e=0,f=a.length;e<f;e++)c.call(d,a[e],e,a);else for(e in a)Object.prototype.hasOwnProperty.call(a,e)&&c.call(d,a[e],e,a)}catch(g){if(g!=l)throw g;}return a};b.map=function(a,c,d){if(a&&a.map)return a.map(c,
+d);var e=[];b.each(a,function(f,g,h){e.push(c.call(d,f,g,h))});return e};b.reduce=function(a,c,d,e){if(a&&a.reduce)return a.reduce(b.bind(d,e),c);b.each(a,function(f,g,h){c=d.call(e,c,f,g,h)});return c};b.reduceRight=function(a,c,d,e){if(a&&a.reduceRight)return a.reduceRight(b.bind(d,e),c);var f=b.clone(b.toArray(a)).reverse();b.each(f,function(g,h){c=d.call(e,c,g,h,a)});return c};b.detect=function(a,c,d){var e;b.each(a,function(f,g,h){if(c.call(d,f,g,h)){e=f;b.breakLoop()}});return e};b.select=function(a,
+c,d){if(a.filter)return a.filter(c,d);var e=[];b.each(a,function(f,g,h){c.call(d,f,g,h)&&e.push(f)});return e};b.reject=function(a,c,d){var e=[];b.each(a,function(f,g,h){!c.call(d,f,g,h)&&e.push(f)});return e};b.all=function(a,c,d){c=c||b.identity;if(a.every)return a.every(c,d);var e=true;b.each(a,function(f,g,h){(e=e&&c.call(d,f,g,h))||b.breakLoop()});return e};b.any=function(a,c,d){c=c||b.identity;if(a.some)return a.some(c,d);var e=false;b.each(a,function(f,g,h){if(e=c.call(d,f,g,h))b.breakLoop()});
+return e};b.include=function(a,c){if(b.isArray(a))return b.indexOf(a,c)!=-1;var d=false;b.each(a,function(e){if(d=e===c)b.breakLoop()});return d};b.invoke=function(a,c){var d=b.rest(arguments,2);return b.map(a,function(e){return(c?e[c]:e).apply(e,d)})};b.pluck=function(a,c){return b.map(a,function(d){return d[c]})};b.max=function(a,c,d){if(!c&&b.isArray(a))return Math.max.apply(Math,a);var e={computed:-Infinity};b.each(a,function(f,g,h){g=c?c.call(d,f,g,h):f;g>=e.computed&&(e={value:f,computed:g})});
+return e.value};b.min=function(a,c,d){if(!c&&b.isArray(a))return Math.min.apply(Math,a);var e={computed:Infinity};b.each(a,function(f,g,h){g=c?c.call(d,f,g,h):f;g<e.computed&&(e={value:f,computed:g})});return e.value};b.sortBy=function(a,c,d){return b.pluck(b.map(a,function(e,f,g){return{value:e,criteria:c.call(d,e,f,g)}}).sort(function(e,f){e=e.criteria;f=f.criteria;return e<f?-1:e>f?1:0}),"value")};b.sortedIndex=function(a,c,d){d=d||b.identity;for(var e=0,f=a.length;e<f;){var g=e+f>>1;d(a[g])<d(c)?
+(e=g+1):(f=g)}return e};b.toArray=function(a){if(!a)return[];if(a.toArray)return a.toArray();if(b.isArray(a))return a;return b.map(a,function(c){return c})};b.size=function(a){return b.toArray(a).length};b.first=function(a,c){return c?Array.prototype.slice.call(a,0,c):a[0]};b.rest=function(a,c){return Array.prototype.slice.call(a,b.isUndefined(c)?1:c)};b.last=function(a){return a[a.length-1]};b.compact=function(a){return b.select(a,function(c){return!!c})};b.flatten=function(a){return b.reduce(a,
+[],function(c,d){if(b.isArray(d))return c.concat(b.flatten(d));c.push(d);return c})};b.without=function(a){var c=b.rest(arguments);return b.select(a,function(d){return!b.include(c,d)})};b.uniq=function(a,c){return b.reduce(a,[],function(d,e,f){if(0==f||(c?b.last(d)!=e:!b.include(d,e)))d.push(e);return d})};b.intersect=function(a){var c=b.rest(arguments);return b.select(b.uniq(a),function(d){return b.all(c,function(e){return b.indexOf(e,d)>=0})})};b.zip=function(){for(var a=b.toArray(arguments),c=
+b.max(b.pluck(a,"length")),d=new Array(c),e=0;e<c;e++)d[e]=b.pluck(a,String(e));return d};b.indexOf=function(a,c){if(a.indexOf)return a.indexOf(c);for(var d=0,e=a.length;d<e;d++)if(a[d]===c)return d;return-1};b.lastIndexOf=function(a,c){if(a.lastIndexOf)return a.lastIndexOf(c);for(var d=a.length;d--;)if(a[d]===c)return d;return-1};b.range=function(a,c,d){var e=b.toArray(arguments),f=e.length<=1;a=f?0:e[0];c=f?e[0]:e[1];d=e[2]||1;e=Math.ceil((c-a)/d);if(e<=0)return[];e=new Array(e);f=a;for(var g=0;1;f+=
+d){if((d>0?f-c:c-f)>=0)return e;e[g++]=f}};b.bind=function(a,c){var d=b.rest(arguments,2);return function(){return a.apply(c||j,d.concat(b.toArray(arguments)))}};b.bindAll=function(){var a=Array.prototype.pop.call(arguments);b.each(arguments,function(c){a[c]=b.bind(a[c],a)})};b.delay=function(a,c){var d=b.rest(arguments,2);return setTimeout(function(){return a.apply(a,d)},c)};b.defer=function(a){return b.delay.apply(b,[a,1].concat(b.rest(arguments)))};b.wrap=function(a,c){return function(){var d=
+[a].concat(b.toArray(arguments));return c.apply(c,d)}};b.compose=function(){var a=b.toArray(arguments);return function(){for(var c=a.length-1;c>=0;c--)arguments=[a[c].apply(this,arguments)];return arguments[0]}};b.keys=function(a){return b.map(a,function(c,d){return d})};b.values=function(a){return b.map(a,b.identity)};b.extend=function(a,c){for(var d in c)a[d]=c[d];return a};b.clone=function(a){if(b.isArray(a))return a.slice(0);return b.extend({},a)};b.isEqual=function(a,c){if(a===c)return true;
+var d=typeof a,e=typeof c;if(d!=e)return false;if(a==c)return true;if(a.isEqual)return a.isEqual(c);if(b.isNumber(a)&&b.isNumber(c)&&isNaN(a)&&isNaN(c))return true;if(d!=="object")return false;d=b.keys(a);e=b.keys(c);if(d.length!=e.length)return false;for(var f in a)if(!b.isEqual(a[f],c[f]))return false;return true};b.isEmpty=function(a){return(b.isArray(a)?a:b.values(a)).length==0};b.isElement=function(a){return!!(a&&a.nodeType==1)};b.isArray=function(a){return Object.prototype.toString.call(a)==
+"[object Array]"};b.isFunction=function(a){return Object.prototype.toString.call(a)=="[object Function]"};b.isString=function(a){return Object.prototype.toString.call(a)=="[object String]"};b.isNumber=function(a){return Object.prototype.toString.call(a)=="[object Number]"};b.isUndefined=function(a){return typeof a=="undefined"};b.noConflict=function(){j._=m;return this};b.identity=function(a){return a};b.breakLoop=function(){throw l;};var n=0;b.uniqueId=function(a){var c=n++;return a?a+c:c};b.functions=
+function(){var a=[];for(var c in b)Object.prototype.hasOwnProperty.call(b,c)&&a.push(c);return b.without(a,"VERSION","prototype","noConflict").sort()};b.template=function(a,c){a=new Function("obj","var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push('"+a.replace(/[\r\t\n]/g," ").split("<%").join("\t").replace(/((^|%>)[^\t]*)'/g,"$1\r").replace(/\t=(.*?)%>/g,"',$1,'").split("\t").join("');").split("%>").join("p.push('").split("\r").join("\\'")+"');}return p.join('');");return c?
+a(c):a};b.forEach=b.each;b.foldl=b.inject=b.reduce;b.foldr=b.reduceRight;b.filter=b.select;b.every=b.all;b.some=b.any;b.head=b.first;b.tail=b.rest;b.methods=b.functions;var k=function(a,c){return c?b(a).chain():a};b.each(b.functions(),function(a){i.prototype[a]=function(){Array.prototype.unshift.call(arguments,this._wrapped);return k(b[a].apply(b,arguments),this._chain)}});b.each(["pop","push","reverse","shift","sort","splice","unshift"],function(a){i.prototype[a]=function(){Array.prototype[a].apply(this._wrapped,
+arguments);return k(this._wrapped,this._chain)}});b.each(["concat","join","slice"],function(a){i.prototype[a]=function(){return k(Array.prototype[a].apply(this._wrapped,arguments),this._chain)}});i.prototype.chain=function(){this._chain=true;return this};i.prototype.value=function(){return this._wrapped}})();
View
46 underscore.js
@@ -31,35 +31,7 @@
if (typeof exports !== 'undefined') exports._ = _;
// Current version.
- _.VERSION = '0.4.5';
-
- /*------------------------ Generator Functions: ---------------------------*/
-
- // Generates an Array, containing an arithmetic progressions
- // Analog of python's built-in function 'range'
- _.range = function(start, stop, step) {
- if (!stop) {
- var stop = start;
- start = 0;
- }
-
- if (!step) var step = 1;
-
- var length = Math.ceil((stop - start) / step);
-
- if (length < 0) {
- return [];
- }
-
- var results = new Array(length);
- var resIdx = 0;
-
- for (var i = start; (start <= stop ? stop - i > 0 : i - stop > 0); i += step) {
- results[resIdx++] = i;
- }
-
- return results;
- };
+ _.VERSION = '0.4.6';
/*------------------------ Collection Functions: ---------------------------*/
@@ -343,6 +315,22 @@
return -1;
};
+ // Generate an integer Array containing an arithmetic progression. A port of
+ // the native Python range() function. See:
+ // http://docs.python.org/library/functions.html#range
+ _.range = function(start, stop, step) {
+ var a = _.toArray(arguments);
+ var solo = a.length <= 1;
+ var start = solo ? 0 : a[0], stop = solo ? a[0] : a[1], step = a[2] || 1;
+ var len = Math.ceil((stop - start) / step);
+ if (len <= 0) return [];
+ var range = new Array(len);
+ for (var i = start, idx = 0; true; i += step) {
+ if ((step > 0 ? i - stop : stop - i) >= 0) return range;
+ range[idx++] = i;
+ }
+ };
+
/* ----------------------- Function Functions: -----------------------------*/
// Create a function bound to a given object (assigning 'this', and arguments,
Please sign in to comment.
Something went wrong with that request. Please try again.