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