mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 11:57:49 +00:00
0.4.6 is on the books, with kylichuku's range function
This commit is contained in:
37
index.html
37
index.html
@@ -107,11 +107,11 @@
|
|||||||
<p>
|
<p>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<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>
|
<td><i>18kb, Uncompressed with Comments</i></td>
|
||||||
</tr>
|
</tr>
|
||||||
<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>
|
<td><i>2kb, Packed and Gzipped</i></td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
@@ -186,7 +186,7 @@ _(lyrics).chain()
|
|||||||
<span class="methods"><a href="#first">first</a>, <a href="#rest">rest</a>, <a href="#last">last</a>,
|
<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="#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="#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>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@@ -583,6 +583,28 @@ _.indexOf([1, 2, 3], 2);
|
|||||||
<pre>
|
<pre>
|
||||||
_.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
|
_.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
|
||||||
=> 4
|
=> 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);
|
||||||
|
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
|
_.range(1, 11);
|
||||||
|
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
|
_.range(0, 30, 5);
|
||||||
|
=> [0, 5, 10, 15, 20, 25]
|
||||||
|
_.range(0, -10, -1);
|
||||||
|
=> [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
|
||||||
|
_.range(0);
|
||||||
|
=> []
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<h2>Function (uh, ahem) Functions</h2>
|
<h2>Function (uh, ahem) Functions</h2>
|
||||||
@@ -930,6 +952,15 @@ _([1, 2, 3]).value();
|
|||||||
|
|
||||||
<h2>Change Log</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>
|
<p>
|
||||||
<b class="header">0.4.5</b><br />
|
<b class="header">0.4.5</b><br />
|
||||||
Added <tt>rest</tt> for Arrays and arguments objects, and aliased
|
Added <tt>rest</tt> for Arrays and arguments objects, and aliased
|
||||||
|
|||||||
@@ -86,4 +86,15 @@ $(document).ready(function() {
|
|||||||
equals(result, 5, 'works on an arguments object');
|
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');
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
@@ -63,4 +63,8 @@
|
|||||||
return _.intersect(numbers, randomized);
|
return _.intersect(numbers, randomized);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
JSLitmus.test('_.range()', function() {
|
||||||
|
return _.range(1000);
|
||||||
|
});
|
||||||
|
|
||||||
})();
|
})();
|
||||||
@@ -7,7 +7,6 @@
|
|||||||
<script type="text/javascript" src="vendor/qunit.js"></script>
|
<script type="text/javascript" src="vendor/qunit.js"></script>
|
||||||
<script type="text/javascript" src="vendor/jslitmus.js"></script>
|
<script type="text/javascript" src="vendor/jslitmus.js"></script>
|
||||||
<script type="text/javascript" src="../underscore.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="collections.js"></script>
|
||||||
<script type="text/javascript" src="arrays.js"></script>
|
<script type="text/javascript" src="arrays.js"></script>
|
||||||
<script type="text/javascript" src="functions.js"></script>
|
<script type="text/javascript" src="functions.js"></script>
|
||||||
|
|||||||
17
underscore-min.js
vendored
17
underscore-min.js
vendored
File diff suppressed because one or more lines are too long
@@ -31,35 +31,7 @@
|
|||||||
if (typeof exports !== 'undefined') exports._ = _;
|
if (typeof exports !== 'undefined') exports._ = _;
|
||||||
|
|
||||||
// Current version.
|
// Current version.
|
||||||
_.VERSION = '0.4.5';
|
_.VERSION = '0.4.6';
|
||||||
|
|
||||||
/*------------------------ 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;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*------------------------ Collection Functions: ---------------------------*/
|
/*------------------------ Collection Functions: ---------------------------*/
|
||||||
|
|
||||||
@@ -343,6 +315,22 @@
|
|||||||
return -1;
|
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: -----------------------------*/
|
/* ----------------------- Function Functions: -----------------------------*/
|
||||||
|
|
||||||
// Create a function bound to a given object (assigning 'this', and arguments,
|
// Create a function bound to a given object (assigning 'this', and arguments,
|
||||||
|
|||||||
Reference in New Issue
Block a user