-
Notifications
You must be signed in to change notification settings - Fork 3
/
episode-02.js
37 lines (31 loc) · 1.15 KB
/
episode-02.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
// Ok, nice! But that code is pretty verbose..
// And not exactly easy to read.
// All those `i` counters seem pretty redundant 😩
// Let's make use of JavaScript's native Array methods to refactor!
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Episode 2: The Array Menace ~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var axios = require('axios');
var chalk = require('chalk');
(async () => {
// Query all movies
var response = await axios.get('https://swapi.co/api/films/');
// Instead of a for loop, we can use our trusty
// Array method to `map` each result to a new object
var { results } = response.data;
var movies = results.map(movie => ({
title: movie.title,
release_date: movie.release_date
}));
// Let's reduce duplication and boilerplate by creating a
// function for property access
var getReleaseDate = movie => new Date(movie.release_date);
var sortedMovies = [...movies].sort(
(movie1, movie2) => getReleaseDate(movie1) - getReleaseDate(movie2)
);
// Create watch list
sortedMovies
.map((movie, i) => `${i + 1}: ${movie.title}`)
.map(listEntry => chalk.cyanBright.bold(listEntry))
.forEach(row => console.log(row));
})();