Skip to content

Commit 12b1d76

Browse files
author
JeanBaptisteWATENBERG
authored
Merge pull request #4 from JeanBaptisteWATENBERG/preview-length-in-json
(feat) add length preview in jsonpath previewer
2 parents 53ff9b9 + d7b393f commit 12b1d76

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

demo/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Demo extends Component {
6363
'price': 19.95
6464
}
6565
}
66-
}} jsonPath='$.store' />
66+
}} jsonPath='$.store.book.length' />
6767

6868
</div>;
6969
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-jsonpath-editor",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "react-jsonpath-editor React component",
55
"main": "lib/index.js",
66
"module": "es/index.js",

src/components/JsonPathPreviewer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ class JsonPathPreviewer extends Component {
2424
tagPartOfJsonToHighlight(jsonAsObject, paths, traversedPath = ['$']) {
2525
if (Array.isArray(jsonAsObject)) {
2626
const doesTraversingPathMatch = paths.filter(oneOfPathsToRetrieve => oneOfPathsToRetrieve.join(',') === traversedPath.join(',')).length > 0;
27+
const isALengthPathMatchingThisCollection = paths.filter(oneOfPathsToRetrieve => oneOfPathsToRetrieve.join(',') === [...traversedPath, 'length'].join(',')).length > 0;
2728
return `${carriageReturnTag + indentationIncrementationTag}${doesTraversingPathMatch ? highlightingTags.start : ''}[${carriageReturnTag + indentationIncrementationTag}${jsonAsObject.map((item, index) =>
2829
this.tagPartOfJsonToHighlight(item, paths, [...traversedPath, index])
29-
).join(',' + carriageReturnTag)}${indentationDecrementationTag + carriageReturnTag}]${doesTraversingPathMatch ? highlightingTags.end : ''}${indentationDecrementationTag}`;
30+
).join(',' + carriageReturnTag)}${indentationDecrementationTag + carriageReturnTag}]${
31+
doesTraversingPathMatch ? highlightingTags.end : ''
32+
}${isALengthPathMatchingThisCollection ? highlightingTags.start + '.length = ' + jsonAsObject.length + highlightingTags.end : ''}${indentationDecrementationTag}`;
3033
}
3134

3235
if (typeof jsonAsObject === 'object') {

tests/JsonPathPreviewer-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ describe('Json path preview component', () => {
8181
expect(taggedJSON).toContain('£CR££INC£££TAGGED£[£CR££INC£{£CR££INC£"category": "reference",£CR£"author": "Nigel Rees",£CR£"title": "Sayings of the Century",£CR£"price": 8.95£DEC££CR£},£CR£{£CR££INC£"category": "fiction",£CR£"author": "Evelyn Waugh",£CR£"title": "Sword of Honour",£CR£"price": 12.99£DEC££CR£},£CR£{£CR££INC£"category": "fiction",£CR£"author": "Herman Melville",£CR£"title": "Moby Dick",£CR£"isbn": "0-553-21311-3",£CR£"price": 8.99£DEC££CR£},£CR£{£CR££INC£"category": "fiction",£CR£"author": "J. R. R. Tolkien",£CR£"title": "The Lord of the Rings",£CR£"isbn": "0-395-19395-8",£CR£"price": 22.99£DEC££CR£}£DEC££CR£]£TAGGED£££DEC£');
8282
});
8383

84+
it('should display length preview', () => {
85+
const wraper = mount(<Previewer json={defaultJson} jsonPath='$..book.length' />);
86+
const paths = wraper.instance().evalJsonPath(defaultJson, '$..book.length');
87+
const taggedJSON = wraper.instance().tagPartOfJsonToHighlight(defaultJson, paths);
88+
89+
expect(taggedJSON).toContain('.length = 4');
90+
});
91+
8492
it ('should not tag anything', () => {
8593
const wraper = mount(<Previewer json={defaultJson} jsonPath='$..books' />);
8694
const paths = wraper.instance().evalJsonPath(defaultJson, '$..books');

0 commit comments

Comments
 (0)