Permalink
Browse files

Add goog.array.range for generating numeric ranges.

This produces the same results as
goog.iter.toArray(goog.iter.range(...)), but a little more directly.

R=arv
DELTA=71 (71 added, 0 deleted, 0 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=6269


git-svn-id: http://closure-library.googlecode.com/svn/trunk@2516 0b95b8e8-c90f-11de-9d4f-f947ee5921c8
  • Loading branch information...
brenneman@google.com
brenneman@google.com committed Feb 7, 2013
1 parent 5a1eab4 commit 71e2bf6f8d3add5264ba67867c93f38e8982fc87
Showing with 71 additions and 0 deletions.
  1. +48 −0 closure/goog/array/array.js
  2. +23 −0 closure/goog/array/array_test.html
@@ -1310,6 +1310,54 @@ goog.array.toObject = function(arr, keyFunc, opt_obj) {
};
+/**
+ * Creates a range of numbers in an arithmetic progression.
+ *
+ * Range takes 1, 2, or 3 arguments:
+ * <pre>
+ * range(5) is the same as range(0, 5, 1) and produces [0, 1, 2, 3, 4]
+ * range(2, 5) is the same as range(2, 5, 1) and produces [2, 3, 4]
+ * range(-2, -5, -1) produces [-2, -3, -4]
+ * range(-2, -5, 1) produces [], since stepping by 1 wouldn't ever reach -5.
+ * </pre>
+ *
+ * @param {number} startOrEnd The starting value of the range if an end argument
+ * is provided. Otherwise, the start value is 0, and this is the end value.
+ * @param {number=} opt_end The optional end value of the range.
+ * @param {number=} opt_step The step size between range values. Defaults to 1
+ * if opt_step is undefined or 0.
+ * @return {!Array.<number>} An array of numbers for the requested range. May be
+ * an empty array if adding the step would not converge toward the end
+ * value.
+ */
+goog.array.range = function(startOrEnd, opt_end, opt_step) {
+ var array = [];
+ var start = 0;
+ var end = startOrEnd;
+ var step = opt_step || 1;
+ if (opt_end !== undefined) {
+ start = startOrEnd;
+ end = opt_end;
+ }
+
+ if (step * (end - start) < 0) {
+ // Sign mismatch: start + step will never reach the end value.
+ return [];
+ }
+
+ if (step > 0) {
+ for (var i = start; i < end; i += step) {
+ array.push(i);
+ }
+ } else {
+ for (var i = start; i > end; i += step) {
+ array.push(i);
+ }
+ }
+ return array;
+};
+
+
/**
* Returns an array consisting of the given value repeated N times.
*
@@ -1433,6 +1433,29 @@
}
}
+function testRange() {
+ assertArrayEquals([], goog.array.range(0));
+ assertArrayEquals([], goog.array.range(5, 5, 5));
+ assertArrayEquals([], goog.array.range(-3, -3));
+ assertArrayEquals([], goog.array.range(10, undefined, -1));
+ assertArrayEquals([], goog.array.range(8, 0));
+ assertArrayEquals([], goog.array.range(-5, -10, 3));
+
+ assertArrayEquals([0], goog.array.range(1));
+ assertArrayEquals([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], goog.array.range(10));
+
+ assertArrayEquals([1], goog.array.range(1, 2));
+ assertArrayEquals([-3, -2, -1, 0, 1, 2], goog.array.range(-3, 3));
+
+ assertArrayEquals([4], goog.array.range(4, 40, 400));
+ assertArrayEquals([5, 8, 11, 14], goog.array.range(5, 15, 3));
+ assertArrayEquals([1, -1, -3], goog.array.range(1, -5, -2));
+ assertElementsRoughlyEqual([.2, .3, .4], goog.array.range(.2, .5, .1), 0.001);
+
+ assertArrayEquals([0], goog.array.range(7, undefined, 9));
+ assertArrayEquals([0, 2, 4, 6], goog.array.range(8, undefined, 2));
+}
+
function testArrayRepeat() {
assertArrayEquals([], goog.array.repeat(3, 0));
assertArrayEquals([], goog.array.repeat(3, -1));

0 comments on commit 71e2bf6

Please sign in to comment.