json-transformer-cli
Advanced tools
Comparing version
@@ -6,5 +6,5 @@ /*jshint evil:true*/ | ||
'use strict'; | ||
const {JSONPath} = require('jsonpath-plus'); | ||
module.exports = factory(JSONPath); | ||
}(this, function(jsonPath) { | ||
const { JSONPath } = require('jsonpath-plus'); | ||
module.exports = factory(JSONPath); | ||
}(this, function (jsonPath) { | ||
'use strict'; | ||
@@ -20,3 +20,3 @@ | ||
*/ | ||
function walk(data, path, result, key) { | ||
function walk(data, path, result, key, parentKey) { | ||
var fn; | ||
@@ -38,3 +38,3 @@ switch (type(path)) { | ||
if (fn) { | ||
fn(data, path, result, key); | ||
fn(data, path, result, key, parentKey); | ||
} | ||
@@ -61,8 +61,9 @@ } | ||
*/ | ||
function seekSingle(data, pathStr, result, key) { | ||
if (typeof(key) !== 'undefined') { | ||
function seekSingle(data, pathStr, result, key, parentKey) { | ||
if (typeof (key) !== 'undefined') { | ||
key = renameKey(data, key); | ||
if(key.indexOf('$$') >= 0){ | ||
if (key.indexOf('$$') >= 0) { | ||
var parsedKey = key.substring(1, key.length); // remove the extra dollar sign from the path | ||
var seek = jsonPath({path: parsedKey, json: data}); | ||
parsedKey = parentKey ? parentKey + "." + parsedKey : parsedKey; | ||
var seek = jsonPath({ path: parsedKey, json: data }); | ||
for (var property in seek[0]) { | ||
@@ -73,7 +74,8 @@ if (seek[0].hasOwnProperty(property)) { | ||
} | ||
} else if(pathStr.indexOf('$$') < 0) { | ||
} else if (pathStr.indexOf('$$') < 0) { | ||
result[key] = pathStr; | ||
} else { | ||
pathStr = pathStr.substring(1, pathStr.length); // remove the extra dollar sign from the path | ||
var seek = jsonPath({path: pathStr, json: data}) || []; | ||
pathStr = parentKey ? parentKey + "." + pathStr : pathStr; | ||
var seek = jsonPath({ path: pathStr, json: data }) || []; | ||
result[key] = seek.length ? seek[0] : undefined; | ||
@@ -83,7 +85,7 @@ } | ||
else { | ||
if(pathStr.indexOf('$$') < 0){ | ||
if (pathStr.indexOf('$$') < 0) { | ||
result = pathStr; | ||
}else{ | ||
} else { | ||
pathStr = pathStr.substring(1, pathStr.length); // remove the extra dollar sign from the path | ||
var seek = jsonPath({path: pathStr, json: data}) || []; | ||
var seek = jsonPath({ path: pathStr, json: data }) || []; | ||
result = seek.length ? seek[0] : undefined; | ||
@@ -103,10 +105,10 @@ } | ||
*/ | ||
function seekArray(data, path, result, key) { | ||
function seekArray(data, path, result, key, parentKey) { | ||
if (typeof result[key] === 'undefined') | ||
result[key] = []; | ||
path.forEach(function(item, index) { | ||
path.forEach(function (item, index) { | ||
if (type(item) === 'string') { | ||
var result2 = {"result": ""}; | ||
walk(data, item, result2, "result"); | ||
var result2 = { "result": "" }; | ||
walk(data, item, result2, "result", parentKey); | ||
result[key].push(result2.result); | ||
@@ -133,6 +135,6 @@ } | ||
if (typeof key !== 'undefined') { | ||
result = result[renameKey(data, key)] = {}; | ||
result = result[renameKey(data, key)] = {}; | ||
} | ||
Object.keys(pathObj).forEach(function(name) { | ||
walk(data, pathObj[name], result, name); | ||
Object.keys(pathObj).forEach(function (name) { | ||
walk(data, pathObj[name], result, name, key); | ||
}); | ||
@@ -147,7 +149,7 @@ } | ||
*/ | ||
function renameKey(data, key){ | ||
if(key.indexOf('${') >= 0){ | ||
function renameKey(data, key) { | ||
if (key.indexOf('${') >= 0) { | ||
var parsedKey = key.substring(key.indexOf('}') + 1, key.length); | ||
var pathVariable = key.substring(key.indexOf('${') + 2, key.indexOf('}')); | ||
var seek = jsonPath({path: pathVariable, json: data}); | ||
var seek = jsonPath({ path: pathVariable, json: data }); | ||
return seek[0] + '_' + parsedKey; | ||
@@ -160,3 +162,3 @@ } | ||
var tmp = string.split(''); // convert to an array | ||
tmp.splice(i - 1 , 1); // remove 1 element from the array (adjusting for non-zero-indexed counts) | ||
tmp.splice(i - 1, 1); // remove 1 element from the array (adjusting for non-zero-indexed counts) | ||
return tmp.join(''); // reconstruct the string | ||
@@ -171,3 +173,3 @@ } | ||
*/ | ||
return function(data, path) { | ||
return function (data, path) { | ||
var result = {}; | ||
@@ -174,0 +176,0 @@ |
@@ -33,3 +33,3 @@ { | ||
}, | ||
"version": "1.0.6" | ||
"version": "1.0.7" | ||
} |
11753
1.87%255
0.79%