From 31cf79ec73895b6b96f376003ebdad2d53f05e85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Malbr=C3=A1n?= Date: Wed, 13 Mar 2013 00:34:15 -0300 Subject: [PATCH 1/4] Break the loop if the callback returns false --- src/utils/CollectionUtils.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/utils/CollectionUtils.js b/src/utils/CollectionUtils.js index c9145a509af..b51e8b11662 100644 --- a/src/utils/CollectionUtils.js +++ b/src/utils/CollectionUtils.js @@ -49,8 +49,9 @@ define(function (require, exports, module) { /** * Iterates over all the properties in an object or elements in an array. Differs from * $.each in that it iterates over array-like objects like regular objects. - * @param {*} object The object or array to iterate over. - * @param {function(value, key)} callback The function that will be executed on every object. + * @param {*} object - The object or array to iterate over. + * @param {function(value, key)} callback - The function that will be executed on every object. + * If the function returns false the loop will break at that iteration. */ function forEach(object, callback) { var keys = Object.keys(object), @@ -58,7 +59,9 @@ define(function (require, exports, module) { i; for (i = 0; i < len; i++) { - callback(object[keys[i]], keys[i]); + if (callback(object[keys[i]], keys[i]) === false) { + break; + } } } From 69d4a0b0e5901dc9ece5da9a577345832069b1af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Malbr=C3=A1n?= Date: Wed, 20 Mar 2013 02:18:44 -0300 Subject: [PATCH 2/4] Adding CollectionUtils.some instead of breaking CollectionUtils.forEach --- src/utils/CollectionUtils.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/utils/CollectionUtils.js b/src/utils/CollectionUtils.js index b51e8b11662..135f5543ae0 100644 --- a/src/utils/CollectionUtils.js +++ b/src/utils/CollectionUtils.js @@ -51,7 +51,6 @@ define(function (require, exports, module) { * $.each in that it iterates over array-like objects like regular objects. * @param {*} object - The object or array to iterate over. * @param {function(value, key)} callback - The function that will be executed on every object. - * If the function returns false the loop will break at that iteration. */ function forEach(object, callback) { var keys = Object.keys(object), @@ -59,13 +58,33 @@ define(function (require, exports, module) { i; for (i = 0; i < len; i++) { - if (callback(object[keys[i]], keys[i]) === false) { - break; + callback(object[keys[i]], keys[i]); + } + } + + /** + * Iterates over all the properties in an object or elements in an array. If a callback returns a + * truthly value then it will inmediatelly return true, if not, it will return false. Differs from + * $.each in that it iterates over array-like objects like regular objects. + * @param {*} object - The object or array to iterate over. + * @param {function(value, key)} callback - The function that will be executed on every object. + * @return {boolean} + */ + function some(object, callback) { + var keys = Object.keys(object), + len = keys.length, + i; + + for (i = 0; i < len; i++) { + if (callback(object[keys[i]], keys[i])) { + return true; } } + return false; } // Define public API exports.indexOf = indexOf; exports.forEach = forEach; + exports.some = some; }); From 285833be5bbfec24717b9ec01b8e7142b7bafb73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Malbr=C3=A1n?= Date: Thu, 21 Mar 2013 03:19:34 -0300 Subject: [PATCH 3/4] Typo fixes --- src/utils/CollectionUtils.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/utils/CollectionUtils.js b/src/utils/CollectionUtils.js index 135f5543ae0..ee749e8315d 100644 --- a/src/utils/CollectionUtils.js +++ b/src/utils/CollectionUtils.js @@ -48,7 +48,8 @@ define(function (require, exports, module) { /** * Iterates over all the properties in an object or elements in an array. Differs from - * $.each in that it iterates over array-like objects like regular objects. + * $.each in that it always iterates over the properties of an object, even if it has a length + * property making it look like an array. * @param {*} object - The object or array to iterate over. * @param {function(value, key)} callback - The function that will be executed on every object. */ @@ -64,8 +65,9 @@ define(function (require, exports, module) { /** * Iterates over all the properties in an object or elements in an array. If a callback returns a - * truthly value then it will inmediatelly return true, if not, it will return false. Differs from - * $.each in that it iterates over array-like objects like regular objects. + * truthly value then it will immediately return true, if not, it will return false. Differs from + * $.each in that it always iterates over the properties of an object, even if it has a length + * property making it look like an array. * @param {*} object - The object or array to iterate over. * @param {function(value, key)} callback - The function that will be executed on every object. * @return {boolean} From 3d005678639cc63d7733a56d9e9603edaa39ae05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Malbr=C3=A1n?= Date: Fri, 22 Mar 2013 03:24:18 -0300 Subject: [PATCH 4/4] Swithing a forEach to some to make it work the same when using $.each --- src/preferences/PreferenceStorage.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/preferences/PreferenceStorage.js b/src/preferences/PreferenceStorage.js index 6f780eace39..75c0f2ceb12 100644 --- a/src/preferences/PreferenceStorage.js +++ b/src/preferences/PreferenceStorage.js @@ -145,13 +145,13 @@ define(function (require, exports, module) { error = null; // validate all name/value pairs before committing - CollectionUtils.forEach(obj, function (value, key) { + CollectionUtils.some(obj, function (value, key) { try { _validateJSONPair(key, value); } catch (err) { // fail fast error = err; - return false; + return true; } });