-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatautils.js
More file actions
213 lines (203 loc) · 6.25 KB
/
Copy pathdatautils.js
File metadata and controls
213 lines (203 loc) · 6.25 KB
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
210
211
212
213
/**
* Parses shipping data from API and reduces it to necessary parts
*
* @param {Object} shippingData whole object from response to parse
*/
function parseShippingData(packageName, trackNum, shippingData) {
return {
packageName: packageName,
trackingNumber: trackNum,
date: getPickupDate(shippingData),
latestLocation: getLocation(shippingData),
status: getLatestActivityStatus(shippingData)
};
}
/**
* Parses date given in data and returns object with separate month day and year
*
* @param {Object} shippingData data from which date will be obtained
*/
function getPickupDate(shippingData) {
var outDate = {};
if(shippingData.PickupDate === undefined) {
outDate = {
month: '00',
day: '00',
year: '0000',
fullDate: 'n/a'
}
}
else {
var month = shippingData.PickupDate.substring(4, 6),
day = shippingData.PickupDate.substring(6, 8),
year = shippingData.PickupDate.substring(0, 4);
outDate = {
month: month,
day: day,
year: year,
fullDate: month + '/' + day + '/' + year
}
}
return outDate;
}
/**
* Gets object for latest Activity regardless of status
*
* @param {Object} shippingData data from which activity will be obtained
*/
function getLatestActivityLocation(shippingData) {
const packageActivity = shippingData.Package !== undefined ? shippingData.Package.Activity : shippingData.Activity;
var latestLocation;
if(Array.isArray(packageActivity)) {
var mostRecentActivity = packageActivity[0]; // guaranteed to exist if array
for(var i = 0; i < packageActivity.length; i++) {
const activity = packageActivity[i];
if(mostRecentActivity.Date < activity.Date && activity.ActivityLocation !== undefined) {
mostRecentActivity = activity;
}
}
latestLocation = mostRecentActivity.ActivityLocation;
}
else {
latestLocation = packageActivity.ActivityLocation;
}
return latestLocation;
}
/**
* Gets object for latest status(progress).
*
* @param {Object} shippingData data from which activity will be obtained
*/
function getLatestActivityStatus(shippingData) {
if (shippingData.Package === undefined) {
return shippingData.CurrentStatus.Description;
}
else{
const packageStatus = shippingData.Package.Activity;
var latestStatus;
if(Array.isArray(packageStatus)) {
var mostRecentActivity = packageStatus[0]; // guaranteed to exist if array
for(var i = 0; i < packageStatus.length; i++) {
const activity = packageStatus[i];
if(mostRecentActivity.Date < activity.Date && activity.Status !== undefined) {
mostRecentActivity = activity;
}
}
latestStatus = mostRecentActivity.Status;
}
else {
latestStatus = packageStatus.Status;
}
return latestStatus.Description;
}
}
/**
* Trims location string to be cleaner and human-readable
*
* @param {string} location object with location data
*/
function getLocationString(location) {
var outStr = '';
// Fill with location data
if(location.city !== undefined) {
outStr += location.city + ', ';
}
if(location.stateProvince !== undefined) {
outStr += location.stateProvince + ', ';
}
if(location.country !== undefined) {
outStr += location.country + ' ';
}
if(location.postalCode !== undefined) {
outStr += location.postalCode;
}
// Trim end if needed
if(outStr.endsWith(', ')) {
outStr = outStr.slice(0, -2);
}
if(outStr.endsWith(' ')) {
outStr = outStr.slice(0, -1);
}
return outStr;
}
/**
* Formats location string to be valid in URL for Google Maps
*
* @param {string} locationStr string for full location
*/
function formatForUrl(locationStr) {
return locationStr.replace(/ /g, '+');
}
/**
* Gets latest location
*
* @param {Object} shippingData data from which location will be collected
*/
function getLocation(shippingData) {
var latestLocation = getLatestActivityLocation(shippingData);
var addrObj = {};
if(latestLocation === undefined) {
addrObj = {
fullLocation: 'No known location',
mapsUrl: 'https://maps.google.com/'
}
}
else {
var location = latestLocation.Address === undefined ?
{
city: latestLocation.City,
stateProvince: latestLocation.StateProvinceCode,
country: latestLocation.CountryCode,
postalCode: latestLocation.PostalCode
} :
{
city: latestLocation.Address.City,
stateProvince: latestLocation.Address.StateProvinceCode,
country: latestLocation.Address.CountryCode,
postalCode: latestLocation.Address.PostalCode
}
var locationStr = getLocationString(location);
addrObj = {
fullLocation: locationStr,
mapsUrl: 'https://www.google.com/maps/place/' + formatForUrl(locationStr) + '/'
}
}
return addrObj;
}
/**
* Clears storage content and wipes HTML list
*/
function clearStorage() {
chrome.storage.sync.clear(() => {
console.log('Successfully cleared storage');
emptyView();
});
}
function interpretStatus(shippingData) {
var outStatus = {}
if (shippingData.status.toLowerCase().indexOf("delivered") !== -1){
outStatus = {
img: '../images/deliveredcheck.png',
alt: 'Delivered'
};
}
else if(shippingData.status === 'Could not find tracking info') {
outStatus = {
img: '../images/notfoundmark.png',
alt: 'Not Found'
}
}
else if(shippingData.status.toLowerCase().indexOf("2nd delivery attempt") !== -1) {
outStatus = {
img: '../images/2nddeliverywarn.png',
alt: '2nd Delivery Attempt'
}
}
else {
outStatus = {
img: '../images/inprogresstruck.png',
alt: 'In Progress'
};
}
return outStatus;
}