@@ -13,5 +13,8 @@ 1.0.0 / 2016-03-23 | ||
1.2.0 / 2016-04-01 | ||
================== | ||
* Add Traverse object abilities | ||
* Add Traverse object abilities | ||
1.2.1 / 2016-04-05 | ||
================== | ||
* Correct a bug in the Traverse loop |
30
index.js
@@ -27,6 +27,11 @@ var traverse = require('traverse'); | ||
// Traverse object | ||
var f = null; | ||
var f; | ||
traverse(variables).forEach(function (x) { | ||
if (typeof x[found] != 'undefined') { | ||
f = x[found]; | ||
if (x && typeof x[found] != 'undefined') { | ||
if (typeof x[found] != 'string') { | ||
// Stringify if not string yet | ||
f = JSON.stringify(x[found]); | ||
} else { | ||
f = x[found] || found; | ||
} | ||
} | ||
@@ -36,2 +41,6 @@ }); | ||
}); | ||
// Array must have the first and last " stripped | ||
// otherwise the JSON object won't be valid on parse | ||
output = output.replace(/"\[(.*)\]"/, '[$1]'); | ||
@@ -59,6 +68,11 @@ callback(JSON.parse(output)); | ||
// Traverse object | ||
var f = null; | ||
var f; | ||
traverse(variables).forEach(function (x) { | ||
if (typeof x[found] != 'undefined') { | ||
f = x[found]; | ||
if (x && typeof x[found] != 'undefined') { | ||
if (typeof x[found] != 'string') { | ||
// Stringify if not string yet | ||
f = JSON.stringify(x[found]); | ||
} else { | ||
f = x[found] || found; | ||
} | ||
} | ||
@@ -69,2 +83,6 @@ }); | ||
// Array must have the first and last " stripped | ||
// otherwise the JSON object won't be valid on parse | ||
output = output.replace(/"\[(.*)\]"/, '[$1]'); | ||
return JSON.parse(output); | ||
@@ -71,0 +89,0 @@ } |
{ | ||
"name": "json-sub", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Substitute placeholders in json Objects.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -20,8 +20,8 @@ #jsonSub | ||
var json = [{ | ||
method : 'get', | ||
path : '/reports/{{campaign_id}}/members', | ||
"method" : "get", | ||
"path" : "/reports/{{campaign_id}}/members", | ||
}, | ||
{ | ||
method : 'get', | ||
path : '/city/{{store_id}}/turnover', | ||
"method" : "get", | ||
"path" : "/city/{{store_id}}/turnover", | ||
}]; | ||
@@ -42,3 +42,3 @@ | ||
// Substitution | ||
var jsonSub.substitute(json, variables, function(result) { | ||
jsonSub.substitute(json, variables, function(result) { | ||
console.log(result); | ||
@@ -48,3 +48,3 @@ }); | ||
/** With memberMode : true | ||
var jsonSub.substitute(json, variables, true, function(result) { | ||
jsonSub.substitute(json, variables, true, function(result) { | ||
console.log(result); | ||
@@ -51,0 +51,0 @@ }); |
5895
13.04%6
20%79
25.4%