-
Notifications
You must be signed in to change notification settings - Fork 0
/
unbox.js
70 lines (59 loc) · 1.41 KB
/
unbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var toString = {}.toString;
module.exports = unbox;
function unbox(obj, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
} else if (!opts) {
opts = {};
}
if (!cb) cb = function (err) { if (err) throw err; };
if (isGeneratorFunction(obj)) return unboxGenerator(obj(), opts, cb);
if (isGenerator(obj)) return unboxGenerator(obj, opts, cb);
if (typeof obj === 'function') return unboxThunk(obj, opts, cb);
if (opts.parallel && Array.isArray(obj)) return unboxArray(obj, opts, cb);
cb(null, obj);
}
function unboxGenerator(gen, opts, cb) {
next();
function next(err, val) {
var ret;
try {
ret = err ? gen.throw(err) : gen.next(val);
} catch (e) {
return cb(e);
}
if (ret.done) return cb(null, ret.value);
unbox(ret.value, opts, next);
}
}
function unboxThunk(thunk, opts, cb) {
thunk(function (err, val) {
if (err) {
cb(err);
return;
}
unbox(val, opts, cb);
});
}
function unboxArray(arr, opts, cb) {
var pending = arr.length;
var erred = false;
arr.forEach(function (item, i) {
unbox(item, opts, function (err, val) {
if (erred) return;
if (err) {
erred = true;
return cb(err);
}
arr[i] = val;
if (!--pending) cb(null, arr);
});
});
}
function isGeneratorFunction(fn) {
return fn && fn.constructor && fn.constructor.name === 'GeneratorFunction';
}
function isGenerator(gen) {
return gen && toString.call(gen) === '[object Generator]';
}