'use strict'

const through = require('pull-through')

module.exports = function block (size, opts) {
  if (!opts) opts = {}
  if (typeof size === 'object') {
    opts = size
    size = opts.size
  }
  size = size || 512

  let zeroPadding

  if (opts.nopad) {
    zeroPadding = false
  } else {
    zeroPadding = typeof opts.zeroPadding !== 'undefined' ? opts.zeroPadding : true
  }

  let buffered = []
  let bufferedBytes = 0

  return through(function transform (data) {
    if (typeof data === 'number') {
      data = Buffer([data])
    }
    bufferedBytes += data.length
    buffered.push(data)

    while (bufferedBytes >= size) {
      const b = Buffer.concat(buffered)
      bufferedBytes -= size
      this.queue(b.slice(0, size))
      buffered = [ b.slice(size, b.length) ]
    }
  }, function flush (end) {
    if (bufferedBytes && zeroPadding) {
      const zeroes = new Buffer(size - bufferedBytes)
      zeroes.fill(0)
      buffered.push(zeroes)
      this.queue(Buffer.concat(buffered))
      buffered = null
    } else if (bufferedBytes) {
      this.queue(Buffer.concat(buffered))
      buffered = null
    }

    this.queue(null)
  })
}

@@ -7,21 +7,19 @@
 trict'%0A%0A
-const
+var
  through
@@ -215,19 +215,19 @@
  512%0A%0A  
-let
+var
  zeroPad
@@ -378,19 +378,19 @@
 %0A  %7D%0A%0A  
-let
+var
  buffere
@@ -402,11 +402,11 @@
 %5D%0A  
-let
+var
  buf
@@ -636,21 +636,19 @@
 %7B%0A      
-const
+var
  b = Buf
@@ -860,13 +860,11 @@
     
-const
+var
  zer

remove ES2015 syntax