Skip to content

Commit

Permalink
Allow cjson.replace variables to be Objects, Arrays or false-y
Browse files Browse the repository at this point in the history
  • Loading branch information
rafzi authored and Rafael Stahl committed Mar 21, 2016
1 parent e5bd478 commit dba7c2f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ exports.parse = function(str, reviver) {
*/
exports.replace = function(str, data) {
return str.replace(/\{\{([^}]+)\}\}/g, function(match, search) {
return data[search] ? data[search] : match;
if (data.hasOwnProperty(search)) {
// If the variable is an object, stringify it before replacement.
// The false positive of "null" is fine in this case.
if (typeof data[search] === 'object') {
return JSON.stringify(data[search]);
}
return data[search];
}
return match;
});
};

Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/conf11.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"subobject": {
"test": 5
},
"subarray": [
{
"foo": 7
}
]
}
8 changes: 8 additions & 0 deletions test/fixtures/conf12.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"num": 1,
"str": "moo",
"bool": false,
"arr": [],
"obj": {},
"null": null
}
4 changes: 4 additions & 0 deletions test/fixtures/templates/conf11tmpl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"subobject": {{subobject}},
"subarray": {{subarray}}
}
8 changes: 8 additions & 0 deletions test/fixtures/templates/conf12tmpl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"num": {{num}},
"str": "{{str}}",
"bool": {{bool}},
"arr": {{arr}},
"obj": {{obj}},
"null": {{null}}
}
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ var data = {
conf7: {"key": "{{root}}/src"},
conf8: {},
conf10: {"test":"valid JSON, except for the the hidden BOM character"},
conf11: {
"subobject": {
"test": 5
},
"subarray": [
{
"foo": 7
}
]
},
conf12: {
"num": 1,
"str": "moo",
"bool": false,
"arr": [],
"obj": {},
"null": null
}
};

a.doesNotThrow(function() {
Expand All @@ -41,6 +59,12 @@ a.deepEqual(cjson.load(fixtures + '/conf8.json'), data.conf8, 'string-like comme

a.deepEqual(cjson.load(fixtures + '/conf10.json'), data.conf10, 'BOM character');

var conf11 = cjson.load(fixtures + '/conf11.json');
a.deepEqual(cjson.load(fixtures + '/templates/conf11tmpl.json', {replace: conf11}), data.conf11, 'object and array as template variables');

var conf12 = cjson.load(fixtures + '/conf12.json');
a.deepEqual(cjson.load(fixtures + '/templates/conf12tmpl.json', {replace: conf12}), data.conf12, 'JSON types as template variables');

var data1 = {
conf1: {key: 'value'},
conf6: data.conf6
Expand Down

0 comments on commit dba7c2f

Please sign in to comment.