Skip to content

Commit

Permalink
fix faulty null handling, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
pokornyd committed Aug 26, 2024
1 parent 97808ea commit e2357ad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/transformers/json-transformer/json-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ export type TransformDomNodeType = (
export const transformToJson = (
result: ParseResult,
customResolvers?: CustomResolversType,
) => customResolvers ? result.children.map(node => transformDomNode(node, customResolvers)) : result.children;
) => {
if (!customResolvers) {
return result.children;
}

return result.children.map(node => transformDomNode(node, customResolvers));
};

const nodeIdentity = (node: DomNode) => node;

Expand All @@ -26,9 +32,10 @@ const transformDomNode: TransformDomNodeType = (
{ resolveDomHtmlNode, resolveDomTextNode }: CustomResolversType,
) => {
if (isText(node)) {
return resolveDomTextNode?.(node) ?? nodeIdentity(node);
return resolveDomTextNode ? resolveDomTextNode(node) : nodeIdentity(node);
}

return resolveDomHtmlNode?.(node, (node) => transformDomNode(node, { resolveDomHtmlNode, resolveDomTextNode }))
?? nodeIdentity(node);
return resolveDomHtmlNode
? resolveDomHtmlNode(node, (node) => transformDomNode(node, { resolveDomHtmlNode, resolveDomTextNode }))
: nodeIdentity(node);
};
20 changes: 20 additions & 0 deletions tests/transfomers/json-transformer/json-transformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,26 @@ describe("Json Transfomer Tests", () => {
expect(output).toEqual(expectedOutput);
});

it("Test text transformer returning null", () => {
const testValue: ParseResult = {
children: [
{
type: "text",
content: "test value",
},
],
};

const result = transformToJson(testValue, {
resolveDomTextNode: () => null,
resolveDomHtmlNode: null,
});

const expected = [null];

expect(result).toEqual(expected);
});

it("Test RichText", () => {
const parsed = browserParse(dummy.value);
const transformed = transformJsonWithCustomResolvers(parsed);
Expand Down

0 comments on commit e2357ad

Please sign in to comment.