From 3a7042e527ae1d91ef0e56309680bbcf1fc5be48 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 28 Mar 2013 19:22:14 -0400 Subject: [PATCH] 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. --- chrome/content/zotero/xpcom/utilities.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js index c4808c9822..d50428cec4 100644 --- a/chrome/content/zotero/xpcom/utilities.js +++ b/chrome/content/zotero/xpcom/utilities.js @@ -624,6 +624,30 @@ Zotero.Utilities = { return a; }, + /** + * 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 *