godkimchi-read-line
Version:
kimchi style's read line module
136 lines (112 loc) • 4.22 kB
Markdown
# godkimchi-read-line
[](https://travis-ci.org/joemccann/dillinger)
kimchi style's read line moudle.
### Example
```javascript
const readline = require('godkimchi-read-line')
readline('your file path', { encoding: 'utf8', type: 'file' }, (err, response) => {
if (err) {
throw err
} else {
const { line, lineCount } = response
consoel.log(`${lineCount} - ${line}`)
return false
}
})
```
### readline(path, [options], callback)
`path` string
`options` object
* `skipHeader` boolean **default** false
* `encoding` string **default** utf8 **check iconv-lite**
* `type` string **option** [file, csv] **default** file
* `quote` string **default** empty string
* `delimiter` string **default** ,
`callback` function
* `err` Error
* `response` object
* `line` string
* `lineCount` number
* `fields` array **only type is** `csv`
### Handle `readline` 'finish' event
`readline` function is `AsyncFunction`. so you can handle readline finish event.
``` javascript
const readline = require('godkimchi-read-line')
readline('your file path', { encoding: 'utf8', type: 'file' }, (err, response) => {
if (err) {
throw err
} else {
...
}
})
.then(response => console.log(response))
.catch(error => console.error(error)
.finally(() => {
process.exit()
})
```
### Continue read line or not
``` javascript
const readline = require('godkimchi-read-line')
readline('your file path', { encoding: 'utf8', type: 'file' }, (err, response) => {
if (err) {
throw err
} else {
...
// return false // stop read line
return true // continue
}
})
```
### Wait until `readline` function read whole file
``` javascript
const readline = require('godkimchi-read-line')
async function readWholeFile () {
const { lineCount } = await readline('your file path', { encoding: 'utf8', type: 'file' }, (err, response) => {
if (err) {
throw err
} else {
...
// return false // stop read line
return true // continue
}
})
console.log('@@@ - file line count is ' + lineCount)
}
```
### Read csv file
``` javascript
const readline = require('godkimchi-read-line')
readline('your file path', { encoding: 'utf8', type: 'csv', quote: '', delimiter: '|' }, (err, response) => {
if (err) {
throw err
} else {
const { line, lineCount, fields } = response
console.log(fields)
return false
}
})
```
License
----
MIT
[//]: # (These are reference links used in the body of this note and get stripped out when the markdown processor does its job. There is no need to format nicely because it shouldn't be seen. Thanks SO - http://stackoverflow.com/questions/4823468/store-comments-in-markdown-syntax)
[dill]: <https://github.com/joemccann/dillinger>
[git-repo-url]: <https://github.com/joemccann/dillinger.git>
[john gruber]: <http://daringfireball.net>
[df1]: <http://daringfireball.net/projects/markdown/>
[markdown-it]: <https://github.com/markdown-it/markdown-it>
[Ace Editor]: <http://ace.ajax.org>
[node.js]: <http://nodejs.org>
[Twitter Bootstrap]: <http://twitter.github.com/bootstrap/>
[jQuery]: <http://jquery.com>
[]: <http://twitter.com/tjholowaychuk>
[express]: <http://expressjs.com>
[AngularJS]: <http://angularjs.org>
[Gulp]: <http://gulpjs.com>
[PlDb]: <https://github.com/joemccann/dillinger/tree/master/plugins/dropbox/README.md>
[PlGh]: <https://github.com/joemccann/dillinger/tree/master/plugins/github/README.md>
[PlGd]: <https://github.com/joemccann/dillinger/tree/master/plugins/googledrive/README.md>
[PlOd]: <https://github.com/joemccann/dillinger/tree/master/plugins/onedrive/README.md>
[PlMe]: <https://github.com/joemccann/dillinger/tree/master/plugins/medium/README.md>
[PlGa]: <https://github.com/RahulHP/dillinger/blob/master/plugins/googleanalytics/README.md>