-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.js
More file actions
138 lines (120 loc) · 4.08 KB
/
Copy pathformat.js
File metadata and controls
138 lines (120 loc) · 4.08 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
/*Stats order of operation:
1. CSV to JSON
2. add in PER
3. removed all unwanted fields but retains duplicates
4. Remove duplicates
5. Add in team names for duplicate players
6. formattedStatsJSON --> no duplicates, ready to be hashed and searched (requires team names to be added back in for "tot" players)
7. [Not Yet Implemented] - Create list of special name players and map them to existing names and have them inserted into the hash table
*/
//Convert stats file from csv to JSON
const fs = require("fs");
const csvFilePath = './stats/players.csv';
const advCsvFilePath = './stats/Advplayers.csv';
const playerURLs = './stats/URLplayers.csv'
const csv = require('csvtojson');
csv()
.fromFile(csvFilePath)
.then((jsonObj) => {
csv()
.fromFile(advCsvFilePath)
.then((advJsonObj) => {
csv()
.fromFile(playerURLs)
.then((playerURLsObj) => {
const formattedStatsJSON = JSON.stringify(format(jsonObj, advJsonObj, playerURLsObj));
fs.writeFile("formattedStatsObject.js", `let formattedStatsObjectJSON = ${formattedStatsJSON}\n module.exports = formattedStatsObjectJSON`, function(err) {
if(err){
console.log(err);
};
});
});
});
});
//remove unwanted stats, add in PER stat
function format(arr, advArr, urlArr) {
arr.forEach((player, index) => {
delete player["2P"];
delete player["2PA"];
delete player["2P%"];
delete player["3P"];
delete player["3PA"];
// delete player["3P%"];
delete player["BLK"];
delete player["DRB"];
delete player["FG"];
delete player["FGA"];
delete player["GS"];
delete player["eFG"];
delete player["FT"];
delete player["FTA"];
delete player["FT%"];
delete player["ORB"];
delete player["STL"];
delete player["TOV"];
delete player["PF"];
delete player["eFG%"];
player["URL"] = urlArr[index].HREF;
if(player.Player = advArr[index].Player) { //add PER stat to each record
player.PER = advArr[index].PER;
player["TS%"] = advArr[index]["TS%"];
}
});
return removeDuplicateNames(arr);
};
//Populates the teams for all traded players, taking in the formatted Stats Array and an array with the proper team names.
function populateDuplicatePlayerTeams(arr, dupArr) {
arr.forEach((player, index) => {
dupArr.map((item, index) => {
if(item.Player === player.Player){
player.Tm = item.Tm;
};
});
});
return arr;
};
//remove duplicate names created by trading, retains full year stats
function removeDuplicateNames(arr) {
let duplicate = false;
let compare;
let spliceIndexArr = [];
let cleanStats;
let lastTeam;
let duplicatePlayerTeams = [];
arr.map(function(item, index) {
if(item.Tm === "TOT"){
if(duplicate) {
duplicatePlayerTeams.push({Player: compare, Tm: lastTeam});
compare = item.Player;
}
else {
duplicate = true;
compare = item.Player;
}
}
else if(duplicate) {
if(item.Player === compare){
spliceIndexArr.push(index);
lastTeam = item.Tm;
}
else { //use last team and compare to create an array to fill in TOT players
duplicatePlayerTeams.push({Player: compare, Tm: lastTeam});
duplicate = false;
compare = "";
lastTeam = "";
}
}
});
cleanStats = arr.filter(function(item, index) {
if(spliceIndexArr.indexOf(index) == -1){
return true;
}
else {
return false
}
});
return populateDuplicatePlayerTeams(cleanStats, duplicatePlayerTeams);
};
module.exports.format = format;
module.exports.populateDuplicatePlayerTeams = populateDuplicatePlayerTeams;
module.exports.removeDuplicateNames = removeDuplicateNames;