Skip to content

Commit a9213fc

Browse files
committed
fix: filtering of null and false nodes from a collection
1 parent 294ae2a commit a9213fc

6 files changed

+73
-4
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ at http://schema.org/.
2626

2727
### Yarn
2828

29-
`yarn add react-structured-data`
29+
`yarn add @researchgate/react-structured-data`
3030

3131
### NPM
3232

33-
`npm install react-structured-data --save`
33+
`npm install @researchgate/react-structured-data --save`
3434

3535
## Code Example
3636

src/JSONLDAbstractNode.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class JSONLDAbstractNode extends React.Component {
1111
};
1212

1313
getChildJSON(child) {
14-
if (!child) return "";
14+
if (!child) return null;
1515

1616
const ChildClass = child.type;
1717
const { children, type, id, ...schema } = child.props;
@@ -34,7 +34,10 @@ class JSONLDAbstractNode extends React.Component {
3434
* If a component has multiple children, this.props.children is an array of Child objects.
3535
*/
3636
if (this.props.children.length > 0) {
37-
return this.props.children.map(child => this.getChildJSON(child));
37+
// Children that are null will get filtered out
38+
return this.props.children
39+
.map(child => this.getChildJSON(child))
40+
.filter(child => child);
3841
}
3942
return [this.getChildJSON(this.props.children)];
4043
}

src/core/__tests__/GenericNode.test.js

+27
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,31 @@ describe("GenerticNode", () => {
3030
)
3131
).toMatchSnapshot();
3232
});
33+
it("filters out null and false child nodes", () => {
34+
expect(
35+
renderer.create(
36+
<JSONLD>
37+
<GenericNode
38+
type="review"
39+
jsonldtype="Review"
40+
name="It is awesome"
41+
reviewBody="This is great!"
42+
>
43+
<GenericNode
44+
type="itemReviewed"
45+
jsonldtype="Product"
46+
id="product-x"
47+
/>
48+
{null}
49+
{false}
50+
<GenericNode
51+
type="locationCreated"
52+
jsonldtype="AdministrativeArea"
53+
name="Chicago, IL"
54+
/>
55+
</GenericNode>
56+
</JSONLD>
57+
)
58+
).toMatchSnapshot();
59+
});
3360
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from "react";
2+
import renderer from "react-test-renderer";
3+
import JSONLD from "../../JSONLD";
4+
import GenericNode from "../GenericNode";
5+
import GenericNodeCollection from "../GenericNodeCollection";
6+
7+
describe("GenerticNodeCollection", () => {
8+
it("filters out null and false nodes from a collection", () => {
9+
expect(
10+
renderer.create(
11+
<JSONLD>
12+
<GenericNodeCollection>
13+
<GenericNode type="author" jsonldtype="Person" name="Cool Carl" />
14+
{false}
15+
{null}
16+
<GenericNode jsonldtype="AdministrativeArea" name="Chicago, IL" />
17+
</GenericNodeCollection>
18+
</JSONLD>
19+
)
20+
).toMatchSnapshot();
21+
});
22+
});

src/core/__tests__/__snapshots__/GenericNode.test.js.snap

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`GenerticNode filters out null and false child nodes 1`] = `
4+
<script
5+
type="application/ld+json"
6+
>
7+
{"@context":"https://schema.org/","@type":"Review","name":"It is awesome","reviewBody":"This is great!","itemReviewed":{"@type":"Product","@id":"product-x"},"locationCreated":{"@type":"AdministrativeArea","name":"Chicago, IL"}}
8+
</script>
9+
`;
10+
311
exports[`GenerticNode renders question to QAPage mainEntity as Question type 1`] = `
412
<script
513
type="application/ld+json"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`GenerticNodeCollection filters out null and false nodes from a collection 1`] = `
4+
<script
5+
type="application/ld+json"
6+
>
7+
{"@context":"https://schema.org/","undefined":[{"author":{"@type":"Person","name":"Cool Carl"}},{"@type":"AdministrativeArea","name":"Chicago, IL"}]}
8+
</script>
9+
`;

0 commit comments

Comments
 (0)