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; } }); diff --git a/src/utils/CollectionUtils.js b/src/utils/CollectionUtils.js index 1e338976a8a..c52535c100f 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. */ @@ -62,6 +63,28 @@ 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 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} + */ + 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; + } + /** * Returns true if the object has the specified property. * This calls the Object.prototype.hasOwnProperty function directly, rather than @@ -76,7 +99,8 @@ define(function (require, exports, module) { } // Define public API - exports.indexOf = indexOf; - exports.forEach = forEach; + exports.indexOf = indexOf; + exports.forEach = forEach; + exports.some = some; exports.hasProperty = hasProperty; });