Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mafintosh/peek-stream
Transform stream that lets you peek the first line before deciding how to parse it
https://github.com/mafintosh/peek-stream
Last synced: 9 days ago
JSON representation
Transform stream that lets you peek the first line before deciding how to parse it
- Host: GitHub
- URL: https://github.com/mafintosh/peek-stream
- Owner: mafintosh
- License: mit
- Created: 2014-07-23T15:17:38.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-03-31T11:29:03.000Z (over 1 year ago)
- Last Synced: 2024-10-16T23:40:26.987Z (23 days ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 56
- Watchers: 4
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-nodejs-cn - peek-stream - 转换流,让你可预览第一行再决定如何解析它 (包 / 文件流)
- awesome-nodejs - peek-stream - Transform stream that lets you peek the first line before deciding how to parse it. (Repository / Streams)
- awesome-nodejs-cn - peek-stream - **star:56** 转换流,它允许您在决定如何解析第一行之前先查看第一行 (包 / 流)
- awesome-nodejs - peek-stream - Transform stream that lets you peek the first line before deciding how to parse it. (Packages / Streams)
- awesome-nodejs-streams - mafintosh/peek-stream - Transform stream that lets you peek the first line before deciding how to parse it (Modules)
- awesome-nodejs - peek-stream - Transform stream that lets you peek the first line before deciding how to parse it - ★ 42 (Streams)
- awesome-node - peek-stream - Transform stream that lets you peek the first line before deciding how to parse it. (Packages / Streams)
- awesome-nodejs-cn - peek-stream - 转换流,使您可以先决定第一行,然后再决定如何解析它. (目录 / 流处理)
README
# peek-stream
Transform stream that lets you peek the first line before deciding how to parse it
```
npm install peek-stream
```[![build status](http://img.shields.io/travis/mafintosh/peek-stream.svg?style=flat)](http://travis-ci.org/mafintosh/peek-stream)
![dat](http://img.shields.io/badge/Development%20sponsored%20by-dat-green.svg?style=flat)## Usage
``` js
var peek = require('peek-stream')
var ldjson = require('ldjson-stream')
var csv = require('csv-parser')var isCSV = function(data) {
return data.toString().indexOf(',') > -1
}var isJSON = function(data) {
try {
JSON.parse(data)
return true
} catch (err) {
return false
}
}// call parser to create a new parser
var parser = function() {
return peek(function(data, swap) {
// maybe it is JSON?
if (isJSON(data)) return swap(null, ldjson())// maybe it is CSV?
if (isCSV(data)) return swap(null, csv())// we do not know - bail
swap(new Error('No parser available'))
})
}
```The above parser will be able to parse both line delimited JSON and CSV
``` js
var parse = parser()parse.write('{"hello":"world"}\n{"hello":"another"}\n')
parse.on('data', function(data) {
console.log(data) // prints {hello:'world'} and {hello:'another'}
})
```Or
``` js
var parse = parser()parse.write('test,header\nvalue-1,value-2\n')
parse.on('data', function(data) {
console.log(data) // prints {test:'value-1', header:'value-2'}
})
```Per default `data` is the first line (or the first `65535` bytes if no newline is found).
To change the max buffer pass an options map to the constructor``` js
var parse = peek({
maxBuffer: 10000
}, function(data, swap) {
...
})
```If you want to emit an error if no newline is found set `strict: true` as well.
## License
MIT