1 | import dateformat from 'dateformat';
|
2 | import chalk from 'chalk';
|
3 |
|
4 | function formatData(data) {
|
5 | return `\n${data.store} / ${data.author} / ${data.title} / ${data.rating} / ${data.date}\n${data.content}\n`;
|
6 | }
|
7 |
|
8 | function getContent(content) {
|
9 | return content
|
10 | .filter((item) => item.attributes.type === 'text')
|
11 | .map((item) => item.label);
|
12 | }
|
13 |
|
14 | function isNewer(fromDate) {
|
15 | return (element) => {
|
16 | const reviewDate = new Date(element.updated);
|
17 |
|
18 | return reviewDate >= fromDate;
|
19 | };
|
20 | }
|
21 |
|
22 | function format(data, fromDate = null) {
|
23 | let formatted = '';
|
24 |
|
25 | for (const country in data) {
|
26 | if ({}.hasOwnProperty.call(data, country)) {
|
27 | let reviews = [];
|
28 |
|
29 | if (fromDate !== null) {
|
30 | reviews = data[country].filter(isNewer(fromDate));
|
31 | } else {
|
32 | reviews = data[country];
|
33 | }
|
34 |
|
35 | reviews.forEach((review) => {
|
36 | const tReview = {
|
37 | store: chalk.bold.black(country.toUpperCase()),
|
38 | author: chalk.bold.red(review.author.name),
|
39 | title: chalk.bold.black(review.title),
|
40 | rating: chalk.bold.black(review['im:rating']),
|
41 | date: chalk.bold.black(dateformat(review.updated, 'yyyy-mm-dd HH:MM:ss')),
|
42 | content: getContent(review.content)[0],
|
43 | };
|
44 |
|
45 | formatted += formatData(tReview);
|
46 | });
|
47 | }
|
48 | }
|
49 |
|
50 | if (formatted === '') {
|
51 | formatted += chalk.bold.red('\nNo reviews\n');
|
52 | }
|
53 |
|
54 | return formatted;
|
55 | }
|
56 |
|
57 | module.exports = format;
|