-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Streaming parse? #20
Labels
Comments
This is fine for Node: https://github.com/mafintosh/csv-parser Would still be nice to have streaming CSV parsing in the browser. |
Closed
you probably mean something else, but I wrote a little class that "streams" csv rows as they are loaded through the progress event: import { request, csvParse, csvParseRows } from 'd3'
import EventEmitter from 'eventemitter3'
export default class CsvLoader extends EventEmitter {
constructor (url) {
super()
this.loadedChars = 0
this.header = undefined
this.data = []
this.xhr = undefined
this.request = request(url)
.mimeType('text')
.on('progress', this.onProgress)
.on('load', () => this.emit('loaded'))
return this
}
get () {
this.request.get()
return this
}
onProgress = (p) => {
const progress = p.total / p.loaded
const responseText = p.target.responseText
// console.log(responseText.length)
const lineBreak = responseText.lastIndexOf('\n') + 1
const char = responseText.slice(this.loadedChars, lineBreak)
let parsed = csvParseRows(char)
if (!this.header) {
this.header = parsed.shift()
}
const obj = parsed.map(p =>
this.header.reduce((a, b, i) => {
a[b] = p[i]
return a
}, {})
)
this.loadedChars = lineBreak
this.emit('row', obj)
this.emit('progress', progress)
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Like the new shapefile.
The text was updated successfully, but these errors were encountered: