A simple BSON indenter / compacter BSON is an extended JSON format. For example these documents are valid BSON documents, but invalid JSON documents: 

prettified documents: 

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "k": 10,
    "dt": ISODate("2018-10-10")
  },
  {
    "_id": ObjectId("5a934e000102030405000001"),
    "k": {
      "field1": "someVal",
      "field2": 12.2323242
    }
  },
  {
    _id: ObjectId("5a934e000102030405000002"),
    k: 7
  }
]


compacted documents

[{"_id":ObjectId("5a934e000102030405000000"),"k":10,"dt":ISODate("2018-10-10")},{"_id":ObjectId("5a934e000102030405000001"),"k":{"field1":"someVal","field2":12.2323242}},{_id:ObjectId("5a934e000102030405000002"),k:7}]


I recently needed a tool to compact or prettify BSON documents. After several attempts with javascript formatter (beautifyjs, prettifier ...) I ended up implementing my own solution. 



Important: 

the goal of this code is not to check if a document has a valid BSON syntax, but only to prettify/compact a BSON document. Hence, it assumes that the src is valid BSON  

Here is my solution: 

const compactMode = 0
const indentMode = 1


function indent(src) {
    return write(src, indentMode)
}

function compact(src) {
    return write(src, compactMode)
}

function write(src, mode) {

    var result = ""
    var needIndent = false
    var inValue = false
    var depth = 0

    for (var i = 0; i &lt; src.length; i++) {
        c = src.charAt(i)

        if (c === " " &amp;&amp; !inValue || c === "\n") {
            continue
        }

        if (needIndent &amp;&amp; c !== "]" &amp;&amp; c !== "}") {
            needIndent = false
            depth++
            result += newline(mode, depth)
        }

        switch (c) {
            case "{":
            case "[":
                needIndent = true
                result += c
                break
            case ",":
                result += c
                result += newline(mode, depth)
                break
            case ":":
                result += c
                if (mode === indentMode &amp;&amp; !inValue) {
                    result += " "
                } 
                break
            case "}":
            case "]":
                if (needIndent) {
                    needIndent = false
                } else {
                    depth--
                    result += newline(mode, depth)
                }
                result += c
                break
            case "\"":
            case "'": // if we get a `'`, replace it with `"`
                inValue = !inValue
                result += "\""
                break
            default:
                result += c
        }
    }
    return result
}

function newline(mode, depth) {
    if (mode === compactMode) {
        return ""
    }
    var line = "\n"
    for (var i = 0; i &lt; depth; i++) {
        line += "  "
    }
    return line
}


How can I improve this ?
the goal of this code is not to check if a document has a valid BSON syntax


But that's the first requirement of a formatter, understanding the syntax. For any formatter to work, it must understand the structure of the input. For instance, if I stick a { inside some random string, how would case "{": tell it apart from a structural {?

So in some form, your parser, no matter how simple, would need to know about some of these rules. You don't need to build a robust parser, you just need to know where the current character is based characters already encountered.

You can use JSON's flow diagrams as a guide to know when it's possible to encounter such characters. You just need to extend it for BSON. Also, here's a simple JS comment remover which removes comments from JS without having to understand the entire JS syntax. In a gist, it went through the string, checking the current character against the current state (i.e. am I in a string, a regexp, comment, etc.) then modifying the code accordingly as it went through.