-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (65 loc) · 2.18 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
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
const Maybe = require('./monad.js');
var user = {
email: '[email protected]',
accountDetails: {
address: {
street: '123 Fake St',
city: 'Exampleville',
province: 'NS',
postcode: '1234'
}
},
preferences: {}
}
var banners = {
'AB': '/assets/banners/alberta.jpg',
'BC': '/assets/banners/british-columbia.jpg',
'MB': '/assets/banners/manitoba.jpg',
'NL': '/assets/banners/newfoundland-labrador.jpg',
'NS': '/assets/banners/nova-scotia.jpg',
'NT': '/assets/banners/northwest-territories.jpg',
'ON': '/assets/banners/ontario.jpg',
'PE': '/assets/banners/prince-edward.jpg',
'QC': '/assets/banners/quebec.jpg',
'SK': '/assets/banners/saskatchewan.jpg',
'YT': '/assets/banners/yukon.jpg',
};
// const getUserBanner = (user, banners) => {
// return banners[user.accountDetails.address.province];
// };
// console.log(getUserBanner(user, banners));
const prop = (p) => (u) => u[p];
const getBannerUrl = (banners) => (province) => banners[province];
const setBannerUrl = (prolog) => (src) => { console.log(prolog + src); }
const getUserBanner2 = (user, banners) => {
return Maybe.of(user)
.map(prop('accountDetails'))
.map(prop('address'))
.map(prop('province'))
.map(getBannerUrl(banners))
.map(setBannerUrl('Finally setting the banner source to: '));
};
getUserBanner2(user, banners);
const getBannerUrlSafe = (banners) => (province) => Maybe.of(banners[province]);
const propSafe = (p) => (u) => Maybe.of(u[p]);
const getUserBannerWithChain = (user, banners) => {
return Maybe.of(user)
.chain(propSafe('accountDetails'))
.chain(propSafe('address'))
.chain(propSafe('province'))
.chain(getBannerUrlSafe(banners))
.map(setBannerUrl('Finally setting the banner source to: '));
};
getUserBannerWithChain(user, banners);
const errorUser = null;
getUserBanner2(errorUser, banners);
const unknownUser = {};
getUserBanner2(unknownUser, banners);
const unknownAddress = {
email: '[email protected]',
accountDetails: {
address: {}
},
preferences: {}
};
getUserBanner2(unknownAddress, banners);