Permalink
Browse files

Added include plugin

  • Loading branch information...
1 parent ba0d5b6 commit 0b996ed281a9f2e64d41747caff32f6664170262 @tj committed Mar 21, 2012
Showing with 33 additions and 2 deletions.
  1. +4 −2 lib/Parser.js
  2. +1 −0 lib/ejson.js
  3. +11 −0 lib/plugins/include.js
  4. +3 −0 test/fixtures/include.json
  5. +14 −0 test/include.js
View
@@ -56,13 +56,15 @@ Parser.prototype.read = function(path){
*/
Parser.prototype.parse = function(str){
- var plugins = this.plugins;
+ var self = this
+ , plugins = this.plugins;
+
return JSON.parse(str, function(key, val){
var plugin, ret;
if ('' === key) return val;
for (var i = 0, len = plugins.length; i < len; ++i) {
plugin = plugins[i];
- ret = plugin(key, val);
+ ret = plugin(key, val, self);
if (undefined != ret) val = ret;
}
return val;
View
@@ -24,4 +24,5 @@ exports.version = '0.0.1';
exports.ms = require('./plugins/ms');
exports.glob = require('./plugins/glob');
exports.replace = require('./plugins/replace');
+exports.include = require('./plugins/include');
exports.dimensions = require('./plugins/dimensions');
View
@@ -0,0 +1,11 @@
+
+/**
+ * Include another JSON file,
+ * for example "include: config/permissions.json".
+ */
+
+module.exports = function(key, val, parser){
+ var m = /^include: *(.+)$/.exec(val);
+ if (!m) return;
+ return parser.read(m[1]);
+}
@@ -0,0 +1,3 @@
+{
+ "permissions": "include: test/fixtures/config.json"
+}
View
@@ -0,0 +1,14 @@
+
+var Parser = require('../')
+ , include = Parser.include;
+
+describe('include', function(){
+ it('should parse the given file', function(){
+ Parser()
+ .use(include)
+ .read('test/fixtures/include.json')
+ .should.eql({
+ permissions: { foo: 'bar', bar: 'baz' }
+ });
+ })
+})

0 comments on commit 0b996ed

Please sign in to comment.