1
0
Fork 0
forked from 0x2620/oxjs

remove Ox.Break

This commit is contained in:
rolux 2012-07-05 10:58:08 +02:00
commit bda90f6b6b
44 changed files with 152 additions and 165 deletions

View file

@ -148,10 +148,9 @@ Ox.filter = function(collection, iterator, that) {
/*@
Ox.forEach <f> forEach loop
`Ox.forEach` loops over arrays, objects and strings. Calling `Ox.Break`
inside the iterator or returning `false` from the iterator acts like a
`break` statement. Unlike `Array.prototype.forEach`, which leaks its counter
variable to the outer scope, `Ox.forEach` returns it.
`Ox.forEach` loops over arrays, objects and strings. Returning `false` from
the iterator acts like a `break` statement. Unlike `for`, which leaks its
counter variable to the outer scope, `Ox.forEach` returns it.
(collection, iterator[, that]) -> <n> Next index
collection <a|o|s> Collection
iterator <f> Iterator
@ -172,33 +171,23 @@ Ox.forEach <f> forEach loop
@*/
Ox.forEach = function(collection, iterator, that) {
var i = 0, key, type = Ox.typeOf(collection);
try {
if (type == 'object' || type == 'storage') {
for (key in collection) {
if (Ox.hasOwn(collection, key)) {
// iterator.call(that, collection[key], key, collection);
if (iterator.call(that, collection[key], key, collection) === false) {
// console.warn('Returning false in Ox.forEach is deprecated.');
break;
}
}
i++;
}
} else {
collection = Ox.toArray(collection);
for (i = 0; i < collection.length; i++) {
if (i in collection) {
// iterator.call(that, collection[i], i, collection);
if (iterator.call(that, collection[i], i, collection) === false) {
// console.warn('Returning false in Ox.forEach is deprecated.');
break;
}
if (type == 'object' || type == 'storage') {
for (key in collection) {
if (Ox.hasOwn(collection, key)) {
if (iterator.call(that, collection[key], key, collection) === false) {
break;
}
}
i++;
}
} catch (error) {
if (error !== Ox.BreakError) {
throw error;
} else {
collection = Ox.toArray(collection);
for (i = 0; i < collection.length; i++) {
if (i in collection) {
if (iterator.call(that, collection[i], i, collection) === false) {
break;
}
}
}
}
return i;
@ -217,7 +206,7 @@ Ox.indexOf <f> Returns the first index of a collection element that passes a tes
@*/
Ox.indexOf = function(collection, test) {
var index = Ox.forEach(collection, function(value) {
test(value) && Ox.Break();
return !test(value); // break if test succeeds
});
return Ox.isObject(collection) ? Object.keys(collection)[index] || null
: index == collection.length ? -1 : index;