-
Notifications
You must be signed in to change notification settings - Fork 3
/
episode-06.js
91 lines (80 loc) · 2.72 KB
/
episode-06.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 'Stop handholding me man, you make me sick!'
// Ok, ok.. you asked for it, so don't come crying to me later
// ~~~~~~~~~~~~~~~~~~~~~~~
// Episode 6: Epilogue ~
// ~~~~~~~~~~~~~~~~~~~~~~~
var axios = require('axios');
var chalk = require('chalk');
var { forEach, pipe, path, map, pick, sortBy, prop, construct, addIndex, add } = require('ramda');
// 🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃
// 🧙 Wizard Level Functional-Style Programming™️
// 🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃🍃
axios
.get('https://swapi.co/api/films/')
.then(
pipe(
path(['data', 'results']), // Films are stored in `response.data.results`
map(pick(['title', 'release_date'])), // For every film get `.title` and `.release_date`
// Now, sort by release date
sortBy(
// Movies are compared with this function
pipe(
prop('release_date'), // Get release date
construct(Date) // Make it 'comparable' by constructing a new `Date` object
)
),
// Loop over list passing the item and an index
addIndex(forEach)(
pipe(
(movie, index) => `${add(1)(index)}: ${prop('title', movie)}`, // Format the output
chalk.cyanBright.bold, // Make the font fancy
console.log // Log it to the console
)
)
)
)
.catch(
pipe(
error => `An error occurred: ${error.message}`,
chalk.redBright.bold,
console.error
)
);
// Without comments, for your viewing pleasure:
// axios
// .get('https://swapi.co/api/films/')
// .then(
// pipe(
// path(['data', 'results']),
// map(pick(['title', 'release_date'])),
// sortBy(
// pipe(
// prop('release_date'),
// construct(Date)
// )
// ),
// addIndex(forEach)(
// pipe(
// (movie, index) => `${add(1)(index)}: ${prop('title', movie)}`,
// chalk.cyanBright.bold,
// console.log
// )
// )
// )
// )
// .catch(
// pipe(
// error => `An error occurred: ${error.message}`,
// chalk.redBright.bold,
// console.error
// )
// );
// ⚠️ Please proceed with caution ⚠️
// Let's keep our style of programming compatible with other people 👫
// The most important goal of your code should be the readability for
// other people on your team or in your company.
// Consider how much other people reading the code will be familiar
// with functional programming and adapt your style accordingly.
// And, of course, add comments if you think a concept might be
// unfamiliar to them.
// 🧙✌️