-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_import.js
177 lines (155 loc) · 4.74 KB
/
csv_import.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
var Decimal = require('decimal.js')
function isCurrency(value) {
return typeof value === 'string' && (value.startsWith('$') || value.startsWith('-$'))
}
module.exports = {
asyncLoop(iterations, func, callback){
var index = 0
var done = false
var loop = {
next() {
if (done) {
return
}
if (index < iterations) {
index++
func(loop)
} else {
done = true
callback()
}
},
iteration() {
return index - 1
},
break() {
done = true
callback()
}
}
loop.next()
return loop
},
validateNumber(aString) {
var results = aString.replace(/(\D)/g, '0')
return results
},
// /*
// * Converts integer strings containing ',' and '$' into integers.
// **/
mixedDataCleaner(arrayOfObjects) {
const parseCurrency = (aString) => {
var convert = aString.replace(/\$/g, '')
convert = convert.replace(/\,/g, '')
convert = new Decimal(convert).times(100)
return convert.toNumber()
}
arrayOfObjects.forEach(function(record) {
if(Array.isArray(record)) {
record.forEach(function(value, index){
if(!isCurrency(value)) { return }
record[index] = parseCurrency(value)
})
} else {
//For an Array containing Objects (JSON)
for(var key in record){
var value = record[key]
if(!isCurrency(value)) { continue }
record[key] = parseCurrency(value)
}
}
})
return arrayOfObjects
},
//Generate Layered Data. This is specifically used with Stacked Bar Graph
generateLayers(arrayOfArrays) {
var layers = []
for(var i = 0; i < arrayOfArrays.length; i++) {
var candidate = []
for(var q = 0; q < arrayOfArrays[i].length; q++) {
candidate.push({ x: q, y: arrayOfArrays[i][q] })
}
layers.push(candidate)
}
return layers
},
/*
* Parses Object data for integers. Removes ',' or '%' then uses
* the JSON.parse() to parse the integers.
**/
numericDataCleaner(arrayOfObjects) {
function parseNumbers(numberString) {
numberString = JSON.stringify(numberString),
numberString = numberString.replace(/\,/g, ''),
numberString = JSON.parse(numberString),
numberString = JSON.parse(numberString)
return numberString
}
function parsePercentage(numberString){
numberString = JSON.stringify(numberString),
numberString = numberString.replace(/\%/g, ''),
numberString = JSON.parse(numberString),
numberString = JSON.parse(numberString)
return numberString
}
for(var i = 0; i < arrayOfObjects.length; i++){
for(var q in arrayOfObjects[i]) {
var currentJSON = arrayOfObjects[i]
//remove all '%'
if(currentJSON[q][currentJSON[q].length - 1] === '%'){
currentJSON[q] = parsePercentage(currentJSON[q])
}
//remove all ','
else if (!isNaN(Number(currentJSON[q][currentJSON[q].length - 1]))) {
currentJSON[q] = parseNumbers(currentJSON[q])
}
}
}
return arrayOfObjects
},
collectBills: function (uri, model) {
fetch(uri, function(req, res) {
}).then(function(res) {
//console.log('*********************\n\n\n\n\response from housebills \n\n\n\n\n\n********************', res.json())
return res.json()
}).then( function(houseBill){
var i = 0
var iterations = houseBill.objects.length
var skippedBills = []
asyncLoop(iterations, function(loop) {
console.log('loop.iteration()', loop.iteration())
const bill = new model()
bill.billNumber = houseBill.objects[i].display_number
bill.billName = houseBill.objects[i].title_without_number
bill.fullTextLink = houseBill.objects[i].thomas_link
bill.terms = houseBill.objects[i].terms
bill.statusDescription = houseBill.objects[i].current_status_label
bill.sponsor = houseBill.objects[i].sponsor.name
bill.save(function(err, success) {
if (err) {
console.warn(loop.iteration(), ' bill data was skipped! ', err)
skippedBills.push({
index: i,
error: err
})
if (i < iterations) {
i++
loop.next()
} else {
loop.next()
}
} else {
if (i < iterations) {
i++
console.log('iteration ', loop.iteration(), ' bill has been saved')
loop.next()
}
}
})
},
function() {
console.log('Bills have finished uploading. The following bills were skipped', skippedBills)
})
})
}
}