From 67cd02bc64772c667d0cf58a1db89f52462236ea Mon Sep 17 00:00:00 2001 From: Rodrigo Formigone Date: Wed, 18 Jan 2017 06:14:40 -0700 Subject: [PATCH] Optionally pretty print the serialized JSON output --- lib/naive_bayes.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/naive_bayes.js b/lib/naive_bayes.js index a258db1..0728b68 100644 --- a/lib/naive_bayes.js +++ b/lib/naive_bayes.js @@ -256,16 +256,18 @@ Naivebayes.prototype.frequencyTable = function (tokens) { /** * Dump the classifier's state as a JSON string. + * @param {Boolean} Optionally format the serialized JSON output for easier human consumption * @return {String} Representation of the classifier. */ -Naivebayes.prototype.toJson = function () { +Naivebayes.prototype.toJson = function (prettyPrint) { var state = {} var self = this + var prettyPrintSpaces = prettyPrint ? 2 : 0 STATE_KEYS.forEach(function (k) { state[k] = self[k] }) - var jsonStr = JSON.stringify(state) + var jsonStr = JSON.stringify(state, null, prettyPrintSpaces) return jsonStr }