-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathparse-stream-readable.js
38 lines (32 loc) · 991 Bytes
/
parse-stream-readable.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
'use strict'
const Readable = require('stream').Readable
const TOML = require('..')
const util = require('util')
const dump = d => util.inspect(d, {colors: true, depth: Infinity})
success().then(() => failure())
function streamString (str) {
// creates a readable stream from a string that just emits the stream as a
// single block.
let streamed = false
return new Readable({
read () {
if (streamed) return this.push(null)
streamed = true
this.push(str)
}
})
}
function success () {
let testtoml = `a = [1.0,1e0]`
console.log('Parsing:', testtoml)
return TOML.parse.stream(streamString(testtoml))
.then(_ => console.log('Result:', dump(_)))
.catch(_ => console.error('Error:', _.message))
}
function failure () {
let testtoml = `a = [1.0,1e0`
console.log('Parsing:', testtoml)
return TOML.parse.stream(streamString(testtoml))
.then(_ => console.log('Result:', dump(_)))
.catch(_ => console.error('Error:', _.message))
}