-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
163 lines (152 loc) · 4.76 KB
/
Copy pathindex.js
File metadata and controls
163 lines (152 loc) · 4.76 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
const fs = require('fs')
const path = require('path')
const readline = require('readline')
const dottie = require('dottie')
module.exports = class CSV {
constructor(filepath, options = { flags: 'a', header: 1 }) {
this.options = options
this.filepath = path.resolve(filepath)
this.fd = fs.closeSync(fs.openSync(this.filepath, this.options.flags))
this.readStream = fs.createReadStream(this.filepath)
this.writeStream = null
this.header = null
this.data = []
this.writeStream = fs.createWriteStream(this.filepath, {
flags: this.options.flags,
start: fs.statSync(this.filepath).size
})
}
read() {
return new Promise((resolve, reject) => {
let lineReader = readline.createInterface({
input: this.readStream
})
let index = 0
this.data = []
lineReader.on('line', line => {
if (index === 0) {
this.header = this.parseLine(line)
}
if (index >= this.options.header) {
let row = this.formatLine(this.parseLine(line))
this.data.push(row)
}
index++
})
lineReader.on('close', _ => {
resolve(this.data)
})
})
}
writeHeader(arrayHeader) {
return new Promise((resolve, reject) => {
this.header = arrayHeader
this.writeStream.write(`${this.header.join(',')}\n`, error => {
if (error) {
reject(error)
} else {
resolve()
}
})
})
}
writeRow(object) {
return new Promise((resolve, reject) => {
let row = []
for (let i in this.header) {
let column = this.header[i]
let value = dottie.get(object, column)
if (typeof value === 'string') {
value = `"${value.replace(/"/g, '\\"')}"`
}
row.push(value)
}
this.writeStream.write(`${row.join(',')}\n`, error => {
if (error) {
reject(error)
} else {
this.length++
resolve()
}
})
})
}
write(object, done) {
let result = Promise.resolve()
if (!this.header) {
let header = dottie.paths(object).sort()
result = this.writeHeader(header)
}
result.then(_ => {
return this.writeRow(object)
})
return result
}
empty() {
fs.truncateSync(this.filepath, 0)
}
formatLine(array) {
let object = {}
for (let index in this.header) {
let columnName = this.header[index]
object[columnName] = array[index]
}
return dottie.transform(object)
}
parseLine(line) {
line = Buffer.from(line)
if (line[line.length - 1] !== 10) {
line = Buffer.concat([line, Buffer.from('\n')])
}
let data = []
let value = null
let escape = null
let enter = false
for (let i in line) {
let index = parseInt(i)
let charCode = line[index]
let char = String.fromCharCode(charCode)
// console.log(char, escape === index, enter) // useful for debugging...
if (charCode === 92) { // \ (backslash)
escape = index + 1
continue
}
if (enter) {
if (escape !== index && charCode === 34) {
enter = false
continue
}
value += char
continue
} else {
if (escape !== index && charCode === 34) { // " (double quote)
enter = true
value = ''
continue
}
if (charCode === 44 || charCode === 10) { // , (comma), \n (newline)
data.push(this.parseValue(value))
value = null
continue
}
value = value || ''
value += char
}
}
if (enter) {
throw new Error('unexpected end of line')
}
return data
}
parseValue(value) {
if (/^\d+$/.test(value)) {
return parseInt(value)
} else if (/^[+-]?\d+\.?\d+$/.test(value)) {
return parseFloat(value)
} else if (typeof value === 'string') {
return value.trim()
} else {
return value
}
}
}