From 83332e06da297bd17edb963ffd429e2e000ffc16 Mon Sep 17 00:00:00 2001 From: Jeremy Ruppel Date: Thu, 15 May 2014 13:23:09 -0700 Subject: [PATCH 1/2] Expose Parser's serialization format via #format --- lib/Parser.js | 20 +++++++++++++++++--- test/parser.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/lib/Parser.js b/lib/Parser.js index b52905e..639adb9 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -74,11 +74,12 @@ Parser.prototype.read = function(path){ Parser.prototype.parse = function(str){ var self = this - , plugins = this.plugins; + , plugins = this.plugins + , format = this.format; - if (typeof str !== 'string') str = JSON.stringify(str); + if (typeof str !== 'string') str = format.stringify(str); - return JSON.parse(str, function(key, val){ + return format.parse(str, function(key, val){ var plugin, ret; if ('' === key) return val; for (var i = 0, len = plugins.length; i < len; ++i) { @@ -89,3 +90,16 @@ Parser.prototype.parse = function(str){ return val; }); }; + +/** + * The parser's serialization format. + * By default, exposes the JSON API, but not the actual object, + * so that individual methods may be overwritten. + * + * @api public + */ + +Parser.prototype.format = { + parse: JSON.parse, + stringify: JSON.stringify +}; diff --git a/test/parser.js b/test/parser.js index bfe6d45..f6d9ed6 100644 --- a/test/parser.js +++ b/test/parser.js @@ -85,4 +85,35 @@ describe('Parser', function(){ parser.parse(data).should.eql(data); }) }) + + describe('.format', function() { + it('should expose the JSON api by default', function() { + var parser = new Parser + , format = parser.format; + + format.parse.should.equal(JSON.parse); + format.stringify.should.equal(JSON.stringify); + }) + + it('should not be the JSON global', function() { + var parser = new Parser + , format = parser.format; + + format.should.not.equal(JSON); + }) + + it('should accept a custom format object', function() { + var parser = new Parser; + + parser.format = { + parse: function(str) { + return JSON.parse(str.toUpperCase()); + }, + stringify: JSON.stringify + }; + + parser.parse('{ "foo": "bar" }') + .should.eql({ FOO: 'BAR' }); + }) + }) }) From 673a3f34c6c6fe4fd4e5b966264bec462ab28d77 Mon Sep 17 00:00:00 2001 From: Jeremy Ruppel Date: Thu, 15 May 2014 13:30:14 -0700 Subject: [PATCH 2/2] Determine extname from Parser#format --- lib/Parser.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Parser.js b/lib/Parser.js index 639adb9..61ee5af 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -60,7 +60,7 @@ Parser.prototype.clone = function(){ Parser.prototype.read = function(path){ this.path = path; - if (!~path.indexOf('.json')) path += '.json'; + if (!~path.indexOf(this.format.extname)) path += this.format.extname; return this.parse(fs.readFileSync(path, 'utf8')); }; @@ -100,6 +100,7 @@ Parser.prototype.parse = function(str){ */ Parser.prototype.format = { + extname: '.json', parse: JSON.parse, stringify: JSON.stringify };