-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
index.test-d.ts
82 lines (67 loc) · 2.5 KB
/
index.test-d.ts
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
71
72
73
74
75
76
77
78
79
80
81
82
import {expectType, expectAssignable} from 'tsd';
import pMap, {pMapIterable, type Options, type Mapper, pMapSkip} from './index.js';
const sites = [
'https://sindresorhus.com',
'https://avajs.dev',
'https://github.com',
];
const sitesWithPromises = [
'https://sindresorhus.com',
Promise.resolve('https://avajs.dev'),
Promise.resolve('https://github.com'),
];
const sitesAsyncIterable = {
async * [Symbol.asyncIterator]() {
yield 'https://sindresorhus.com';
yield 'https://avajs.dev';
yield 'https://github.com';
},
};
const sitesAsyncIterableWithPromises: AsyncIterable<Promise<string>> = {
[Symbol.asyncIterator]() {
return {
async next() {
return {
done: false,
value: Promise.resolve('https://github.com'),
};
},
};
},
};
const numbers = [
0,
1,
2,
];
const asyncMapper = async (site: string): Promise<string> => site;
const asyncSyncMapper = async (site: string, index: number): Promise<string> =>
index > 1 ? site : Promise.resolve(site);
const multiResultTypeMapper = async (site: string, index: number): Promise<string | number> =>
index > 1 ? site.length : site;
expectAssignable<Mapper>(asyncMapper);
expectAssignable<Mapper<string, string>>(asyncMapper);
expectAssignable<Mapper>(asyncSyncMapper);
expectAssignable<Mapper<string, string | Promise<string>>>(asyncSyncMapper);
expectAssignable<Mapper>(multiResultTypeMapper);
expectAssignable<Mapper<string, string | number>>(multiResultTypeMapper);
expectAssignable<Options>({});
expectAssignable<Options>({concurrency: 0});
expectAssignable<Options>({stopOnError: false});
expectType<Promise<string[]>>(pMap(sites, asyncMapper));
expectType<Promise<string[]>>(pMap(sites, asyncMapper, {concurrency: 2}));
expectType<Promise<string[]>>(pMap(sites, asyncSyncMapper));
expectType<Promise<Array<string | number>>>(pMap(sites, multiResultTypeMapper));
expectType<Promise<string[]>>(pMap(sites, (site: string) => site));
expectType<Promise<number[]>>(pMap(sites, (site: string) => site.length));
expectType<Promise<number[]>>(pMap(numbers, (number: number) => number * 2));
expectType<Promise<number[]>>(pMap(numbers, (number: number) => {
if (number % 2 === 0) {
return number * 2;
}
return pMapSkip;
}));
expectType<AsyncIterable<string>>(pMapIterable(sites, asyncMapper));
expectType<AsyncIterable<string>>(pMapIterable(sitesWithPromises, asyncMapper));
expectType<AsyncIterable<string>>(pMapIterable(sitesAsyncIterable, asyncMapper));
expectType<AsyncIterable<string>>(pMapIterable(sitesAsyncIterableWithPromises, asyncMapper));