Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions 23 lib/jsonpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,31 @@ function jsonPath(obj, expr, arg) {

var $ = obj;
var resultType = P.resultType.toLowerCase();
if (expr && obj && (resultType == "value" || resultType == "path")) {
if (expr && obj && (resultType == "value" || resultType == "path" || resultType == 'both')) {
var exprList = P.normalize(expr);
if (exprList[0] === "$" && exprList.length > 1) exprList.shift();
var result = P.trace(exprList, obj, ["$"]);
result = result.filter(function(ea) { return ea && !ea.isParentSelector; });
if (!result.length) return P.wrap ? [] : false;
if (result.length === 1 && !P.wrap && !Array.isArray(result[0].value)) return result[0][resultType] || false;
if (result.length === 1 && !P.wrap && !Array.isArray(result[0].value)) {
if (resultType == 'both') {
return result[0];
}
return result[0][resultType] || false;
}
return result.reduce(function(result, ea) {
var valOrPath = ea[resultType];
if (resultType === 'path') valOrPath = P.asPath(valOrPath);
var valOrPath;
switch (resultType) {
case 'value':
valOrPath = ea[resultType];
break;
case 'path':
valOrPath = P.asPath(ea[resultType]);
break;
case 'both':
result.push(ea);
return result;
}
if (P.flatten && Array.isArray(valOrPath)) {
result = result.concat(valOrPath);
} else {
Expand Down
65 changes: 65 additions & 0 deletions 65 test/test.both.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var jsonpath = require("../").eval,
testCase = require('nodeunit').testCase

var json = {
"name": "root",
"children": [
{"name": "child1", "children": [{"name": "child1_1"},{"name": "child1_2"}]},
{"name": "child2", "children": [{"name": "child2_1"}]},
{"name": "child3", "children": [{"name": "child3_1"}, {"name": "child3_2"}]}
]
};


module.exports = testCase({

// ============================================================================
"simple parent selection, return both path and value": function(test) {
// ============================================================================
test.expect(1);
var result = jsonpath(json, "$.children[0]^", {resultType: 'both'});
test.deepEqual([{path: ['$', 'children'], value: json.children}], result);
test.done();
},

// ============================================================================
"parent selection with multiple matches, return both path and value": function(test) {
// ============================================================================
test.expect(1);
var expectedOne = {path: ['$', 'children'], value: json.children};
var expected = [expectedOne, expectedOne];
var result = jsonpath(json, "$.children[1:3]^", {resultType: 'both'});
test.deepEqual(expected, result);
test.done();
},

// ============================================================================
"select sibling via parent, return both path and value": function(test) {
// ============================================================================
test.expect(1);
var expected = [{path: [ '$', 'children', 2, 'children', 1], value: {"name": "child3_2"}}];
var result = jsonpath(json, "$..[?(@.name && @.name.match(/3_1$/))]^[?(@.name.match(/_2$/))]", {resultType: 'both'});
test.deepEqual(expected, result);
test.done();
},

// ============================================================================
"parent parent parent, return both path and value": function(test) {
// ============================================================================
test.expect(1);
var expected = [{path: ['$', 'children', 0, 'children'], value: json.children[0].children}];
var result = jsonpath(json, "$..[?(@.name && @.name.match(/1_1$/))].name^^", {resultType: 'both'});
test.deepEqual(expected, result);
test.done();
},

// ============================================================================
"no such parent": function(test) {
// ============================================================================
test.expect(1);
var result = jsonpath(json, "name^^", {resultType: 'both'});
test.deepEqual([], result);
test.done();
}

});
3 changes: 2 additions & 1 deletion 3 test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ <h1 id="nodeunit-header">JSONPath Tests</h1>
loadJS("test.examples.js");
loadJS("test.intermixed.arr.js");
loadJS("test.parent-selector.js");
loadJS("test.both.js");
nodeunit.run(suites);
</script>
</body>
</html>
</html>
Morty Proxy This is a proxified and sanitized view of the page, visit original site.