Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Add CollectionUtils.some() to allow breaking out of iteration #3117

Merged
merged 6 commits into from
Apr 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/preferences/PreferenceStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});

Expand Down
30 changes: 27 additions & 3 deletions src/utils/CollectionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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
Expand All @@ -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;
});