-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (38 loc) · 1.08 KB
/
index.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
'use strict';
const { isPlainObject } = require('is-plain-object');
const resolveValue = async (value, replacer) => {
const seen = new Set();
const resolve = async (val, parent) => {
if (seen.has(val)) return val;
seen.add(val);
if (val instanceof Promise) {
return val.then(v => resolve(v, val));
}
if (typeof val === 'function') {
if (typeof replacer === 'function') {
return replacer(val, parent, resolve);
}
return resolve(val(), val);
}
if (typeof val.toJSON === 'function') {
return resolve(val.toJSON(), val);
}
if (Array.isArray(val)) {
return Promise.all(val.map(v => resolve(v, val)));
}
if (isPlainObject(val)) {
const pending = [];
for (const key of Object.keys(val)) {
const promise = resolve(val[key], val).then(v => {
val[key] = v;
pending.splice(pending.indexOf(promise), 1);
});
pending.push(promise);
}
await Promise.all(pending);
}
return val;
};
return resolve(value);
};
module.exports = resolveValue;