-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require Node.js 8, add TypeScript definition, refactor to async-await (…
…#9)
- Loading branch information
1 parent
00ba4d3
commit f6071ee
Showing
8 changed files
with
161 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
* text=auto | ||
*.js text eol=lf | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,3 @@ language: node_js | |
node_js: | ||
- '10' | ||
- '8' | ||
- '6' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {Options} from 'p-map'; | ||
|
||
export type Mapper<ValueType, KeyType, MappedValueType> = ( | ||
element: PromiseLike<ValueType> | ValueType, | ||
key: KeyType | ||
) => MappedValueType | PromiseLike<MappedValueType>; | ||
|
||
/** | ||
* Like [`Promise.all()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) but for `Map` and `Object`. | ||
* | ||
* @param input - Resolves entry values that are promises. Other values are passed through. | ||
* @param mapper - Receives the current value and key as parameters. Expected to return a `Promise` or value. | ||
* @param options - See the [`p-map` options](https://github.com/sindresorhus/p-map#options). | ||
* @returns A promise that is fulfilled when all promises in `input` and ones returned from `mapper` are fulfilled, or rejects if any of the promises reject. The fulfilled value is the same as `input`, but with a fulfilled version of each entry value, or the fulfilled value returned from `mapper`, if defined. | ||
*/ | ||
export default function pProps< | ||
KeyType extends unknown, | ||
ValueType extends unknown, | ||
MappedValueType = ValueType | ||
>( | ||
input: Map<KeyType, PromiseLike<ValueType> | ValueType>, | ||
mapper?: Mapper<ValueType, KeyType, MappedValueType>, | ||
options?: Options | ||
): Promise<Map<KeyType, MappedValueType>>; | ||
export default function pProps< | ||
KeyType extends string, | ||
ValueType extends unknown, | ||
MappedValueType = ValueType | ||
>( | ||
input: {[key in KeyType]: PromiseLike<ValueType> | ValueType}, | ||
mapper?: Mapper<ValueType, KeyType, MappedValueType>, | ||
options?: Options | ||
): Promise<{[key in KeyType]: MappedValueType}>; | ||
|
||
export {Options} from 'p-map'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,35 @@ | ||
'use strict'; | ||
|
||
const pMap = require('p-map'); | ||
|
||
const map = (input, mapper, options) => { | ||
return pMap(input.entries(), ([key, value]) => mapper(value, key), options).then(values => { | ||
const ret = new Map(); | ||
const map = async (input, mapper, options) => { | ||
const values = await pMap(input.entries(), ([key, value]) => mapper(value, key), options); | ||
const result = new Map(); | ||
|
||
for (const [i, key] of [...input.keys()].entries()) { | ||
ret.set(key, values[i]); | ||
} | ||
for (const [i, key] of [...input.keys()].entries()) { | ||
result.set(key, values[i]); | ||
} | ||
|
||
return ret; | ||
}); | ||
return result; | ||
}; | ||
|
||
const obj = (input, mapper, options) => { | ||
// TODO: Use `Object.entries()` when targeting Node.js 8 | ||
return pMap(Object.keys(input), key => mapper(input[key], key), options).then(values => { | ||
const ret = {}; | ||
const obj = async (input, mapper, options) => { | ||
const values = await pMap(Object.entries(input), ([key, value]) => mapper(value, key), options); | ||
const result = {}; | ||
|
||
for (const [i, key] of Object.keys(input).entries()) { | ||
ret[key] = values[i]; | ||
} | ||
for (const [i, key] of Object.keys(input).entries()) { | ||
result[key] = values[i]; | ||
} | ||
|
||
return ret; | ||
}); | ||
return result; | ||
}; | ||
|
||
module.exports = (input, mapper, options) => { | ||
const pProps = (input, mapper, options) => { | ||
mapper = mapper || (value => value); | ||
return input instanceof Map ? map(input, mapper, options) : obj(input, mapper, options); | ||
return input instanceof Map ? | ||
map(input, mapper, options) : | ||
obj(input, mapper, options); | ||
}; | ||
|
||
module.exports = pProps; | ||
module.exports.default = pProps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import {expectType} from 'tsd-check'; | ||
import pProps from '.'; | ||
|
||
expectType<Promise<{[key in 'foo']: string}>>(pProps({foo: 'bar'})); | ||
expectType<Promise<{[key in 'foo']: boolean}>>( | ||
pProps({foo: 'bar'}, (value, key) => { | ||
expectType<string | PromiseLike<string>>(value); | ||
expectType<'foo'>(key); | ||
return Math.random() > 0.5 ? false : Promise.resolve(true); | ||
}) | ||
); | ||
expectType<Promise<{[key in 'foo']: boolean}>>( | ||
pProps( | ||
{foo: 'bar'}, | ||
(value, key) => { | ||
expectType<string | PromiseLike<string>>(value); | ||
expectType<'foo'>(key); | ||
return Math.random() > 0.5 ? false : Promise.resolve(true); | ||
}, | ||
{ | ||
concurrency: 1 | ||
} | ||
) | ||
); | ||
|
||
const hashMap = { | ||
unicorn: Promise.resolve(1), | ||
foo: 'bar' | ||
}; | ||
|
||
expectType<Promise<{[key: string]: string | number}>>( | ||
pProps<string, string | number>(hashMap) | ||
); | ||
expectType<Promise<{[key: string]: boolean}>>( | ||
pProps<string, string | number, boolean>(hashMap, (value, key) => { | ||
expectType<string | number | PromiseLike<string | number>>(value); | ||
expectType<string>(key); | ||
return Math.random() > 0.5 ? false : Promise.resolve(true); | ||
}) | ||
); | ||
expectType<Promise<{[key: string]: boolean}>>( | ||
pProps<string, string | number, boolean>( | ||
hashMap, | ||
(value, key) => { | ||
expectType<string | number | PromiseLike<string | number>>(value); | ||
expectType<string>(key); | ||
return Math.random() > 0.5 ? false : Promise.resolve(true); | ||
}, | ||
{ | ||
concurrency: 1 | ||
} | ||
) | ||
); | ||
|
||
const map = new Map<number, string | Promise<string>>([ | ||
[1, Promise.resolve('1')], | ||
[2, '2'] | ||
]); | ||
|
||
pProps(map).then(result => { | ||
expectType<string | undefined>(result.get(1)); | ||
}); | ||
|
||
expectType<Promise<Map<number, string>>>(pProps(map)); | ||
expectType<Promise<Map<number, boolean>>>( | ||
pProps(map, (value, key) => { | ||
expectType<string | PromiseLike<string>>(value); | ||
expectType<number>(key); | ||
return Math.random() > 0.5 ? false : Promise.resolve(true); | ||
}) | ||
); | ||
expectType<Promise<Map<number, boolean>>>( | ||
pProps( | ||
map, | ||
(value, key) => { | ||
expectType<string | PromiseLike<string>>(value); | ||
expectType<number>(key); | ||
return Math.random() > 0.5 ? false : Promise.resolve(true); | ||
}, | ||
{ | ||
concurrency: 1 | ||
} | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters