-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-pipes.ts
46 lines (41 loc) · 1.46 KB
/
sync-pipes.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
import {take} from 'take-n-pipe';
const countryDisplayNames: Map<string, string> = new Map([
['AF', 'Afghanistan'],
['AL', 'Albania'],
['DZ', 'Algeria'],
['AS', 'American Samoa'],
['AD', 'Andorra']
]);
const inputData: string = 'AR';
test('Example: synchronous pipes', () => {
// 1. Take country code as input
const pipeOutput: string = take(inputData)
// 2. Validate if it's valid, 2 letters ISO code
.pipe((code: string) => {
if(!/^[A-Z]{2}$/.test(code))
throw new Error('Invalid country code');
else
return code;
})
// 3. Map with country display name
.pipe(countryDisplayNames.get.bind(countryDisplayNames))
// 4. Throw error if display name for this country wasn't found
.pipe((displayName: string | undefined) => {
if(!displayName) throw new Error('Display name not found.');
else return displayName;
})
/* 5. Catch errors, then:
* - find out if error is related to missing display name issue;
* - and replace it with 'Unknown country name' placeholder.
*/
.catch((e: unknown) => {
if(e instanceof Error && e.message === 'Display name not found.')
return 'Unknown country name'; // placeholder
throw e; // On the other hand - throw the error further
})
/* 6. Obtain results
* Be aware that calling `get()` method can throw errors raised during pipe chaining.
*/
.get();
expect(pipeOutput).toBe('Unknown country name');
});