commit 3a7042e527ae1d91ef0e56309680bbcf1fc5be48
parent feb47caa6b2235fef178f444a348a7bb607acab7
Author: Dan Stillman <dstillman@zotero.org>
Date: Thu, 28 Mar 2013 19:22:14 -0400
Zotero.Utilities.forEachChunk(arr, chunkSize, func)
Run a function on chunks of a given size of an array's elements and
return an array with the return values from the successive runs.
Diffstat:
1 file changed, 24 insertions(+), 0 deletions(-)
diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js
@@ -625,6 +625,30 @@ Zotero.Utilities = {
},
/**
+ * Run a function on chunks of a given size of an array's elements.
+ *
+ * @param {Array} arr
+ * @param {Integer} chunkSize
+ * @param {Function} func
+ * @return {Array} The return values from the successive runs
+ */
+ "forEachChunk":function(arr, chunkSize, func) {
+ var retValues = [];
+ var tmpArray = arr.concat();
+ var num = arr.length;
+ var done = 0;
+
+ do {
+ var chunk = tmpArray.splice(0, chunkSize);
+ done += chunk.length;
+ retValues.push(func(chunk));
+ }
+ while (done < num);
+
+ return retValues;
+ },
+
+ /**
* Generate a random integer between min and max inclusive
*
* @param {Integer} min