import stream from 'stream'

export default class HtmlWriterStream extends stream.Transform {
  constructor(options = {}) {
    super(options)
    Object.assign(this, options, {
      started: false
    })
  }

  head(data) {
    this.push(this.header)
    this.push(data.toString())
    this.started = true
  }

  body(data) {
    this.push(data.toString())
  }

  foot() {
    this.push(this.footer)
    this.push(null)
  }

  _transform(chunk, encoding, done) {
    const data = chunk.toString()
    this.started ? this.body(data) : this.head(data)
    done()
  }

  _flush(done) {
    this.foot()
    this._lastLineData = null
    done()
  }
}

@@ -87,16 +87,35 @@
 sform %7B%0A
+  started = false%0A%0A
   constr
@@ -131,13 +131,8 @@
 ions
- = %7B%7D
 ) %7B%0A
@@ -141,23 +141,16 @@
   super(
-options
 )%0A    Ob
@@ -178,38 +178,8 @@
 ions
-, %7B%0A      started: false%0A    %7D
 )%0A  
@@ -188,20 +188,16 @@
 %0A  head(
-data
 ) %7B%0A    
@@ -223,39 +223,8 @@
 er)%0A
-    this.push(data.toString())%0A
     
@@ -281,27 +281,16 @@
 ush(data
-.toString()
 )%0A  %7D%0A%0A 
@@ -393,42 +393,8 @@
 ) %7B%0A
-    const data = chunk.toString()%0A
     
@@ -410,42 +410,47 @@
 ted 
-?
+%7C%7C
  this.
-body(data) : this.head(data
+head(chunk)%0A    this.body(chunk
 )%0A  

refactor of HtmlWriterStream