forked from sindresorhus/p-props
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (27 loc) · 1022 Bytes
/
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
'use strict';
const pMap = require('p-map');
const map = async (map, mapper, options) => {
const awaitedEntries = [...map.entries()].map(async ([key, value]) => [key, await value]);
const values = await pMap(awaitedEntries, ([key, value]) => mapper(value, key), options);
const result = new Map();
for (const [index, key] of [...map.keys()].entries()) {
result.set(key, values[index]);
}
return result;
};
const object = async (map, mapper, options) => {
const awaitedEntries = Object.entries(map).map(async ([key, value]) => [key, await value]);
const values = await pMap(awaitedEntries, ([key, value]) => mapper(value, key), options);
const result = {};
for (const [index, key] of Object.keys(map).entries()) {
result[key] = values[index];
}
return result;
};
// eslint-disable-next-line default-param-last
const pProps = (input, mapper = (value => value), options) => {
return input instanceof Map ?
map(input, mapper, options) :
object(input, mapper, options);
};
module.exports = pProps;