-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (95 loc) · 2.44 KB
/
index.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
const cTable = require('console.table'),
Papa = require('papaparse'),
fs = require('fs'),
_ = require('lodash')
function convert (filepath) {
const file = fs.createReadStream(filepath)
return new Promise((resolve, reject) => {
Papa.parse(file, {
header: true,
dynamicTyping: true,
transformHeader (header) {
return header.trim()
},
transform (val) {
return typeof val === 'string' ? val.trim() : val
},
complete (results, file) {
resolve(results)
},
error (err, file) {
reject(err)
}
})
})
}
class TabDat {
constructor (data) {
this.data = data.data
this.meta = data.meta
}
addCol (name, userFunction) {
this.meta.fields.push(name)
this.data = this.data.map(row => _.set(row, name, userFunction(row)))
return this
}
addRow (row) {
const diffs = _.difference(_.keys(row), this.meta.fields, )
if (diffs.length === 0) {
this.data.push(row)
} else {
throw new Error(`New rows must include the same columns that currently exist in the table. The following columns do not currently exist in the table: ${diffs}. To add new columns, use the addCol method.`)
}
return this
}
deleteCol (colNames) {
this.data = this.data.map(row => _.omit(row, colNames))
return this
}
filter (userFunction) {
this.data = _.filter(this.data, userFunction)
return this
}
printColNames () {
console.log(this.meta.fields)
return this
}
printTable () {
console.table(this.data)
return this
}
renameCol (oldColName, newColName) {
_.remove(this.meta.fields, field => field === oldColName)
this.meta.fields.push(newColName)
this.data = this.data.map(row => {
_.set(row, newColName, row[oldColName])
delete row[oldColName]
return row
})
return this
}
save (filepath) {
const csv = Papa.unparse(this.data)
fs.writeFileSync(filepath, csv)
return this
}
size () {
console.log(`${this.data.length} rows x ${this.meta.fields.length} columns`)
return this
}
sortBy (userFunction, array) {
if (userFunction) {
this.data = _.sortBy(this.data, userFunction)
return this
} else if (array) {
this.data = _.sortBy(this.data, array)
return this
} else {
throw new Error('You must pass either a function or an array to the sortBy method')
}
}
}
module.exports = {
convert,
TabDat
}