Skip to content

Commit

Permalink
added simple text dumper
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 committed Nov 10, 2014
1 parent 60645f6 commit e4535ff
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/dumper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var Map = require('./map');
var Entity = require('./entity');
var Decoder = require('./decoder');

function toText(xvar) {
if (xvar instanceof Date) {
return "(date) " + xvar.toJSON() + ")";

} else if (xvar instanceof Entity) {
var attrs = '';
if (xvar.attributes !== null && typeof xvar.attributes === "object") {
attrs = toText(xvar.attributes);
}
return "(entity)\n\tname: " + toText(xvar.value)
+ "\n\tattributes: " + (attrs.indexOf("\n") === -1 ? ("" + attrs) : attrs.replace(/\n/g, "\n\t"));
}

if (xvar instanceof Map) {
var s = "array (" + xvar.length + ")\n";
xvar.forEach(function (key, val) {
value = toText(val);
s += "\t" + (toText(key) + " => ")
+ "" + (value.indexOf("\n") === -1 ? ("" + value) : value.replace(/\n/g, "\n\t"))
+ "" + "\n";
});
return s.trim() + "\n";

} else if (typeof xvar == "string" && isNaN(xvar)
&& !xvar.match(/[\x00-\x1F]|^\d{4}|^(true|false|yes|no|on|off|null)$/i)
&& (new RegExp("^" + Decoder.patterns[1] + "$")).exec(xvar) // 1 = literals
) {
return '"' + xvar + '" (' + xvar.length + ")";
} else {
return JSON.stringify(xvar);
}
}

module.exports.toText = toText;
1 change: 1 addition & 0 deletions src/neon.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ module.exports.Entity = require('./entity');
module.exports.Map = require('./map');
module.exports.CHAIN = DecoderClass.CHAIN;
module.exports.BLOCK = EncoderClass.BLOCK;
module.exports.dumper = require('./dumper');
module.exports.Error = require('./error');

0 comments on commit e4535ff

Please sign in to comment.