rename vars

This commit is contained in:
rolux 2012-05-25 09:40:27 +02:00
parent 7f7a5aa44c
commit 631ad8222d

View file

@ -2,14 +2,14 @@
(function() { (function() {
function asyncMap(forEach, col, iterator, that, callback) { function asyncMap(forEach, collection, iterator, that, callback) {
var type = Ox.typeOf(col), var type = Ox.typeOf(collection),
results = type == 'object' ? {} : []; results = type == 'object' ? {} : [];
callback = Ox.last(arguments); callback = Ox.last(arguments);
that = arguments.length == 5 ? that : null; that = arguments.length == 5 ? that : null;
forEach(col, function(val, key, obj, callback) { forEach(collection, function(value, key, collection, callback) {
iterator(val, key, obj, function(v) { iterator(value, key, collection, function(value) {
results[key] = v; results[key] = value;
callback(); callback();
}); });
}, that, function() { }, that, function() {
@ -17,15 +17,16 @@
}); });
}; };
Ox.asyncMap = function(arr, iterator, that, callback) { Ox.asyncMap = function(array, iterator, that, callback) {
array = Ox.makeArray(array);
callback = Ox.last(arguments); callback = Ox.last(arguments);
that = arguments.length == 4 ? that : null; that = arguments.length == 4 ? that : null;
if (arr.some(Ox.isArray)) { if (array.some(Ox.isArray)) {
Ox.serialMap(arr, function(val, key, obj, callback) { Ox.serialMap(array, function(value, key, array, callback) {
Ox.parallelMap(Ox.makeArray(val), iterator, callback); Ox.parallelMap(Ox.makeArray(value), iterator, callback);
}, callback); }, callback);
} else { } else {
Ox.parallelMap(Ox.makeArray(val), iterator, callback); Ox.parallelMap(array, iterator, callback);
} }
}; };
@ -33,13 +34,16 @@
Ox.nonblockingForEach <f> Non-blocking forEach with synchronous iterator Ox.nonblockingForEach <f> Non-blocking forEach with synchronous iterator
(col, iterator[, that], callback[, ms]) -> <u> undefined (col, iterator[, that], callback[, ms]) -> <u> undefined
@*/ @*/
Ox.nonblockingForEach = function(col, iterator, that, callback, ms) { Ox.nonblockingForEach = function(collection, iterator, that, callback, ms) {
var i = 0, keys, last = Ox.last(arguments), n, time, type = Ox.typeOf(col); var i = 0, keys, last = Ox.last(arguments),
n, time, type = Ox.typeOf(collection);
callback = Ox.isFunction(last) ? last : arguments[arguments.length - 2]; callback = Ox.isFunction(last) ? last : arguments[arguments.length - 2];
col = type == 'array' || type == 'object' ? col : Ox.toArray(col); collection = type == 'array' || type == 'object'
keys = type == 'object' ? Object.keys(col) : Ox.range(col.length); ? collection : Ox.toArray(collection);
keys = type == 'object'
? Object.keys(collection) : Ox.range(collection.length);
ms = ms || 1000; ms = ms || 1000;
n = Ox.len(col); n = Ox.len(collection);
that = arguments.length == 5 || ( that = arguments.length == 5 || (
arguments.length == 4 && Ox.isFunction(last) arguments.length == 4 && Ox.isFunction(last)
) ? that : null; ) ? that : null;
@ -47,18 +51,18 @@
iterate(); iterate();
function iterate() { function iterate() {
Ox.forEach(keys.slice(i), function(key) { Ox.forEach(keys.slice(i), function(key) {
if (key in col) { if (key in collection) {
try { try {
iterator.call(that, col[key], key, col); iterator.call(that, collection[key], key, collection);
} catch(e) { } catch(error) {
if (e === Ox.BreakError) { if (error === Ox.BreakError) {
i = n; i = n;
} }
throw e; throw e;
} }
} }
i++; i++;
+new Date() >= time + ms && Ox.Break()(); +new Date() >= time + ms && Ox.Break();
}); });
if (i < n) { if (i < n) {
setTimeout(function() { setTimeout(function() {
@ -73,8 +77,8 @@
/*@ /*@
Ox.nonblockingMap <f> Non-blocking map with synchronous iterator Ox.nonblockingMap <f> Non-blocking map with synchronous iterator
(col, iterator[, that]) -> <u> undefined (collection, iterator[, that]) -> <u> undefined
col <a|o|s> Collection collection <a|o|s> Collection
iterator <f> Iterator function iterator <f> Iterator function
that <o> The iterator's this binding that <o> The iterator's this binding
callback <f> Callback function callback <f> Callback function
@ -92,16 +96,16 @@
); );
</script> </script>
@*/ @*/
Ox.nonblockingMap = function(col, iterator, that, callback, ms) { Ox.nonblockingMap = function(collection, iterator, that, callback, ms) {
var last = Ox.last(arguments), var last = Ox.last(arguments),
type = Ox.typeOf(col), type = Ox.typeOf(collection),
results = type == 'object' ? {} : []; results = type == 'object' ? {} : [];
callback = Ox.isFunction(last) ? last : arguments[arguments.length - 2]; callback = Ox.isFunction(last) ? last : arguments[arguments.length - 2];
that = arguments.length == 5 || ( that = arguments.length == 5 || (
arguments.length == 4 && Ox.isFunction(last) arguments.length == 4 && Ox.isFunction(last)
) ? that : null; ) ? that : null;
Ox.nonblockingForEach(col, function(val, key, obj) { Ox.nonblockingForEach(collection, function(value, key, collection) {
results[key] = iterator.call(that, val, key, obj); results[key] = iterator.call(that, value, key, collection);
}, function() { }, function() {
callback(type == 'string' ? results.join('') : results); callback(type == 'string' ? results.join('') : results);
}, ms); }, ms);
@ -109,24 +113,25 @@
/*@ /*@
Ox.parallelForEach <f> forEach with asynchronous iterator, running in parallel Ox.parallelForEach <f> forEach with asynchronous iterator, running in parallel
(col, iterator[, that], callback) -> <u> undefined (collection, iterator[, that], callback) -> <u> undefined
col <a|o|s> Collection collection <a|o|s> Collection
iterator <f> Iterator function iterator <f> Iterator function
val <*> Value value <*> Value
key <n|s> Key key <n|s> Key
obj <a|o> The collection collection <a|o|s> The collection
callback <f> Callback function callback <f> Callback function
that <o> The iterator's this binding that <o> The iterator's this binding
callback <f> Callback function callback <f> Callback function
@*/ @*/
Ox.parallelForEach = function(col, iterator, that, callback) { Ox.parallelForEach = function(collection, iterator, that, callback) {
var i = 0, n, type = Ox.typeOf(col); var i = 0, n, type = Ox.typeOf(collection);
callback = Ox.last(arguments); callback = Ox.last(arguments);
col = type == 'array' || type == 'object' ? col : Ox.toArray(col); collection = type == 'array' || type == 'object'
n = Ox.len(col); ? collection : Ox.toArray(collection);
n = Ox.len(collection);
that = arguments.length == 4 ? that : null; that = arguments.length == 4 ? that : null;
Ox.forEach(col, function(val, key, obj) { Ox.forEach(collection, function(value, key, collection) {
iterator.call(that, val, key, obj, function() { iterator.call(that, value, key, collection, function() {
++i == n && callback(); ++i == n && callback();
}); });
}); });
@ -134,12 +139,12 @@
/*@ /*@
Ox.parallelMap <f> Parallel map with asynchronous iterator Ox.parallelMap <f> Parallel map with asynchronous iterator
(col, iterator[, that], callback) -> <u> undefined (collection, iterator[, that], callback) -> <u> undefined
col <a|o|s> Collection collection <a|o|s> Collection
iterator <f> Iterator function iterator <f> Iterator function
val <*> Value value <*> Value
key <n|s> Key key <n|s> Key
obj <a|o|s> The collection collection <a|o|s> The collection
callback <f> Callback function callback <f> Callback function
that <o> The iterator's this binding that <o> The iterator's this binding
callback <f> Callback function callback <f> Callback function
@ -165,44 +170,51 @@
/*@ /*@
Ox.serialForEach <f> forEach with asynchronous iterator, run serially Ox.serialForEach <f> forEach with asynchronous iterator, run serially
(col, iterator[, that], callback) -> <u> undefined (collection, iterator[, that], callback) -> <u> undefined
col <a|o|s> Collection collection <a|o|s> Collection
iterator <f> Iterator function iterator <f> Iterator function
val <*> Value value <*> Value
key <n|s> Key key <n|s> Key
obj <a|o> The collection collection <a|o|s> The collection
callback <f> Callback function callback <f> Callback function
that <o> The iterator's this binding that <o> The iterator's this binding
callback <f> Callback function callback <f> Callback function
@*/ @*/
Ox.serialForEach = function(col, iterator, that, callback) { Ox.serialForEach = function(collection, iterator, that, callback) {
var i = 0, keys, n, type = Ox.typeOf(col); var i = 0, keys, n, type = Ox.typeOf(collection);
callback = Ox.last(arguments); callback = Ox.last(arguments);
col = type == 'array' || type == 'object' ? col : Ox.toArray(col); collection = type == 'array' || type == 'object'
keys = type == 'object' ? Object.keys(col) : Ox.range(col.length); ? collection : Ox.toArray(collection);
n = Ox.len(col); keys = type == 'object'
? Object.keys(collection) : Ox.range(collection.length);
n = Ox.len(collection);
that = arguments.length == 4 ? that : null; that = arguments.length == 4 ? that : null;
iterate(); iterate();
function iterate() { function iterate() {
keys[i] in col && iterator.call(that, col[keys[i]], keys[i], col, function() { keys[i] in collection && iterator.call(
++i < n ? iterate() : callback(); that,
}); collection[keys[i]],
keys[i],
collection,
function() {
++i < n ? iterate() : callback();
}
);
} }
}; };
/*@ /*@
Ox.serialMap <f> Serial map with asynchronous iterator Ox.serialMap <f> Serial map with asynchronous iterator
(col, iterator[, that], callback) -> <u> undefined (collection, iterator[, that], callback) -> <u> undefined
col <a|o|s> Collection collection <a|o|s> Collection
iterator <f> Iterator function iterator <f> Iterator function
val <*> Value value <*> Value
key <n|s> Key key <n|s> Key
obj <a|o> The collection collection <a|o|s> The collection
callback <f> Callback function callback <f> Callback function
that <o> The iterator's this binding that <o> The iterator's this binding
callback <f> Callback function callback <f> Callback function
err <o|u> Error object results <a|o|s> Results
results <a|o> Results
<script> <script>
var time = +new Date(); var time = +new Date();
Ox.serialMap( Ox.serialMap(
@ -218,7 +230,7 @@
); );
</script> </script>
@*/ @*/
Ox.serialMap = function(col, iterator, that, callback) { Ox.serialMap = function(collection, iterator, that, callback) {
asyncMap.apply(null, [Ox.serialForEach].concat(Ox.toArray(arguments))); asyncMap.apply(null, [Ox.serialForEach].concat(Ox.toArray(arguments)));
}; };