-
Notifications
You must be signed in to change notification settings - Fork 4
/
product-helper.js
209 lines (159 loc) · 4.26 KB
/
product-helper.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
'use strict';
import Product from '../class/product-class';
const productHelper = (() => {
/**
* This function change active tab when is clicked
*
*/
function changeActiveTab(list, item) {
for (let i in list) {
if (list[i].id == item.id) {
list[i].status = true
} else {
list[i].status = false;
}
}
return list;
};
/*
* Create a default list of tabs in product details
* to see more informations of showed product
*
*/
function createTabList () {
return [
{ id:0, title:'Extended credits and info', status: true},
{ id:1, title:'Cover information', status: false},
{ id:2, title:'Stories', status: false},
];
}
/*
* This method is responsible for handling a list
* and returning objects with updated data
*
*/
function createList (list) {
let newList = [];
for (let i in list) {
let product = list[i];
let obj = new Product({
id: product.id,
image: validateImageNotFound(product.thumbnail.path, product.thumbnail.extension),
title: product.title,
creator: getCreator(product.creators, 'editor'),
penciler: getCreator(product.creators, 'penciller'),
writer: getCreator(product.creators, 'writer'),
coverArtist: getCreator(product.creators, 'penciller (cover)'),
modified: getDate(product.modified),
prices: product.prices,
description: product.description,
digitalRead: isDigitalRead(product.prices),
qtd: 1,
upc: product.upc,
format: product.format,
stories: product.stories,
});
newList.push(obj);
}
return newList;
};
/**
* This function return a value to width of div when get rating
* Remember 165 is a default width of div
*
*/
function ratingCount(value) {
const maxRatingValue = 5;
return (value / maxRatingValue) * 165;
};
/**
* This method checks if the comic has a virtual file
*
*/
function isDigitalRead (prices) {
for (let i in prices) {
if (prices[i].type == 'digitalPurchasePrice') {
return true;
break;
}
}
return false;
};
/**
* This function will return if the state is true for a description with
* up to 100 characters, if more than 300 to, otherwise returns a full description
*
*/
function cropDescription(description, status) {
const maxLengthLess = 100;
const maxLengthMore = 300;
if (status && description.length >= maxLengthLess) {
return `${description.substring(0, maxLengthLess)} ...`;
} else if (description.length >= maxLengthMore) {
return `${description.substring(0, maxLengthMore)} ...`;
} else {
return description;
}
};
/**
* This function is responsible for returning a valid date
*
*/
function getDate(data) {
const parsedDate = Date.parse(data);
if (isNaN(parsedDate) == false) {
const date = new Date(parsedDate);
return date.toDateString();
}
return;
};
/**
* This role is responsible for returning a creator
*
*/
function getCreator (creators, type) {
if (creators.available > 0) {
const editor = creators.items.find(item => item.role == type)
return (editor) ? editor.name : null;
}
return;
};
/**
* This method return a full name with comma
*
*/
function inverseCreator (creator) {
// Transform full name in array
const nameToSplit = creator.split(' ');
if (nameToSplit.length >= 1 && nameToSplit.length <=2) {
return `${nameToSplit[1]}, ${nameToSplit[0]}`
}
return creator;
};
/*
* This method finds image not availabe in path of thumbnail
* end replace for a local picture
*
*/
function validateImageNotFound (path, extension) {
// Find image not available in path
const findInString = path.includes('image_not_available');
if (!findInString) {
return `${path}.${extension}`;
}
return '/images/layout/image_not_found.jpg'
};
return {
createList,
ratingCount,
isDigitalRead,
cropDescription,
getDate,
getCreator,
inverseCreator,
validateImageNotFound,
createTabList,
changeActiveTab
};
})();
export default productHelper;