forked from JSONPath-Plus/JSONPath
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add test case for issue JSONPath-Plus#126
- Docs: Link to XPath 2.0 tester
- Loading branch information
Showing
3 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,4 +102,56 @@ describe('JSONPath - Callback', function () { | |
assert.deepEqual(result[2][prop], expected[2][prop]); | ||
}); | ||
}); | ||
|
||
// https://github.com/s3u/JSONPath/issues/126 | ||
it('Using callback to set', function () { | ||
const expected = { | ||
age: 30, | ||
email: '[email protected]', | ||
'something_deeper': { | ||
abc: 1, | ||
quantity: 11 | ||
}, | ||
first_name: 'John', | ||
last_name: 'Doe' | ||
}; | ||
const givenPerson = { | ||
age: 30, | ||
email: '[email protected]', | ||
// let's add first_name, last_name fields | ||
'something_deeper': { | ||
abc: 1 | ||
// let's add quantity here | ||
} | ||
}; | ||
|
||
// defined an object with "json_path":"value" format, | ||
// made sure it is not a deep object. | ||
const obj1 = { | ||
$: { | ||
'first_name': 'John', | ||
'last_name': 'Doe' | ||
}, | ||
'$.something_deeper': { | ||
quantity: 11 | ||
} | ||
}; | ||
|
||
// eslint-disable-next-line compat/compat | ||
Object.entries(obj1).forEach(([path, valuesToSet]) => { | ||
jsonpath({ | ||
json: givenPerson, | ||
path, | ||
wrap: false, | ||
callback (obj) { | ||
// eslint-disable-next-line compat/compat | ||
Object.entries(valuesToSet).forEach(([key, val]) => { | ||
obj[key] = val; | ||
}); | ||
} | ||
}); | ||
}); | ||
const result = givenPerson; | ||
assert.deepEqual(result, expected); | ||
}); | ||
}); |