index.js
function prettyPrintVisitor (pp, spaces) {
  var visitor = {}
  visitor.pre = function (state) {
    state.result = ''
    state.depth = 0
  }
  visitor.visitor = {
    arrayEnter: function (val, state) {
      if (val.key !== undefined) {
        state.result += repeat(state.depth * spaces, ' ')
        if (val.parent.type === 'object') {
          state.result += "'" + val.key + "': "
        }
      }
      if (val.isCircularRef) {
        state.result += '[Circular]\n'
        return
      } else if (val.length === 0) {
        state.result += '[]\n'
        return
      }
      state.result += '[\n'
      state.depth += 1
    },
    arrayExit: function (val, state) {
      if (val.length === 0 || val.isCircularRef) { return }
      state.depth -= 1
      state.result += repeat(state.depth * spaces, ' ') + ']\n'
    },
    objectEnter: function (val, state) {
      if (val.key !== undefined) {
        state.result += repeat(state.depth * spaces, ' ')
        if (val.parent.type === 'object') {
          state.result += "'" + val.key + "': "
        }
      }
      if (val.isCircularRef) {
        state.result += '[Circular]\n'
        return
      } else if (val.length === 0) {
        state.result += '{}\n'
        return
      }
      state.result += '{\n'
      state.depth += 1
    },
    objectExit: function (val, state) {
      if (val.length === 0 || val.isCircularRef) { return }
      state.depth -= 1
      state.result += repeat(state.depth * spaces, ' ') + '}\n'
    },
    stringEnter: function (val, state) {
      if (val.parent === null) {
        state.result += pp(val.value).slice(1, -1)
        return
      }
      state.result += repeat(state.depth * spaces, ' ')
      if (val.parent.type === 'object') {
        state.result += "'" + val.key + "': "
      }
      state.result += pp(val.value) + '\n'
    },
    otherEnter: function (val, state) {
      state.result += repeat(state.depth * spaces, ' ')
      if (val.key !== undefined && val.parent.type === 'object') {
        state.result += "'" + val.key + "': "
      }
      state.result += pp(val.value) + '\n'
    }
  }
  visitor.post = function (state) {
    visitor.result = state.result.trim()
  }
  return visitor
}
function prettyPrintVisitor (pp, spaces) {
  var visitor = {}
  visitor.pre = function (state) {
    state.result = ''
    state.depth = 0
  }
  visitor.visitor = {
    arrayEnter: function (val, state) {
      if (val.key !== undefined) {
        state.result += repeat(state.depth * spaces, ' ')
        if (val.parent.type === 'object') {
          state.result += "'" + val.key + "': "
        }
      }
      if (val.isCircularRef) {
        state.result += '[Circular]\n'
        return
      } else if (val.length === 0) {
        state.result += '[]\n'
        return
      }
      state.result += '[\n'
      state.depth += 1
    },
    arrayExit: function (val, state) {
      if (val.length === 0 || val.isCircularRef) { return }
      state.depth -= 1
      state.result += repeat(state.depth * spaces, ' ') + ']\n'
    },
    objectEnter: function (val, state) {
      if (val.key !== undefined) {
        state.result += repeat(state.depth * spaces, ' ')
        if (val.parent.type === 'object') {
          state.result += "'" + val.key + "': "
        }
      }
      if (val.isCircularRef) {
        state.result += '[Circular]\n'
        return
      } else if (val.length === 0) {
        state.result += '{}\n'
        return
      }
      state.result += '{\n'
      state.depth += 1
    },
    objectExit: function (val, state) {
      if (val.length === 0 || val.isCircularRef) { return }
      state.depth -= 1
      state.result += repeat(state.depth * spaces, ' ') + '}\n'
    },
    stringEnter: function (val, state) {
      if (val.parent === null) {
        state.result += pp(val.value).slice(1, -1)
        return
      }
      state.result += repeat(state.depth * spaces, ' ')
      if (val.parent.type === 'object') {
        state.result += "'" + val.key + "': "
      }
      state.result += pp(val.value) + '\n'
    },
    otherEnter: function (val, state) {
      state.result += repeat(state.depth * spaces, ' ')
      if (val.key !== undefined && val.parent.type === 'object') {
        state.result += "'" + val.key + "': "
      }
      state.result += pp(val.value) + '\n'
    }
  }
  visitor.post = function (state) {
    visitor.result = state.result.trim()
  }
  return visitor
}
function prettyPrintVisitor ( pp , spaces ) { var visitor = { } visitor . pre = function ( state ) { state . result = '' state . depth = 0 } visitor . visitor = { arrayEnter : function ( val , state ) { if ( val . key !== undefined ) { state . result += repeat ( state . depth * spaces , ' ' ) if ( val . parent . type === 'object' ) { state . result += "'" + val . key + "': " } } if ( val . isCircularRef ) { state . result += '[Circular]\n' return } else if ( val . length === 0 ) { state . result += '[]\n' return } state . result += '[\n' state . depth += 1 } , arrayExit : function ( val , state ) { if ( val . length === 0 || val . isCircularRef ) { return } state . depth -= 1 state . result += repeat ( state . depth * spaces , ' ' ) + ']\n' } , objectEnter : function ( val , state ) { if ( val . key !== undefined ) { state . result += repeat ( state . depth * spaces , ' ' ) if ( val . parent . type === 'object' ) { state . result += "'" + val . key + "': " } } if ( val . isCircularRef ) { state . result += '[Circular]\n' return } else if ( val . length === 0 ) { state . result += '{}\n' return } state . result += '{\n' state . depth += 1 } , objectExit : function ( val , state ) { if ( val . length === 0 || val . isCircularRef ) { return } state . depth -= 1 state . result += repeat ( state . depth * spaces , ' ' ) + '}\n' } , stringEnter : function ( val , state ) { if ( val . parent === null ) { state . result += pp ( val . value ) . slice ( 1 , - 1 ) return } state . result += repeat ( state . depth * spaces , ' ' ) if ( val . parent . type === 'object' ) { state . result += "'" + val . key + "': " } state . result += pp ( val . value ) + '\n' } , otherEnter : function ( val , state ) { state . result += repeat ( state . depth * spaces , ' ' ) if ( val . key !== undefined && val . parent . type === 'object' ) { state . result += "'" + val . key + "': " } state . result += pp ( val . value ) + '\n' } } visitor . post = function ( state ) { visitor . result = state . result . trim ( ) } return visitor }
Visitor factory for pretty printing a JavaScript value.

@param {function} pp Fallback pretty printer
@param {number} spaces Number of spaces for indentation
@return {object} Visitor instance
Visitor factory for pretty printing a JavaScript value .