Skip to content

Commit

Permalink
Accept undefined from childrenFn
Browse files Browse the repository at this point in the history
  • Loading branch information
Jessica Kerr committed Jan 20, 2019
1 parent fe0ea57 commit 4985c12
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export function stringifyTree<T>(tn: T, nameFn: (t: T) => string, childrenFn: (t
});
}
function nodeToStrings(tn: T): string[] {
const children = [...childrenFn(tn)]; // copy the array
const origChildren = childrenFn(tn) || [];
const children = [...origChildren]; // copy the array
if (children.length === 0) {
return ["─ " + nameFn(tn)];
}
Expand Down
8 changes: 8 additions & 0 deletions test/stringifyTree.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/stringifyTree.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/stringifyTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ describe("stringify", () => {
console.log("tree:\n" + result);
assert.deepEqual(result, "─ hi");
});
it("treats undefined as empty children", () => {
type TN = { name: string, children: TN[] }
const tree = {
name: "hi", children: undefined,
} as unknown as TN;
const result = stringifyTree(tree, t => t.name, t => t.children);
console.log("tree:\n" + result);
assert.deepEqual(result, "─ hi");
});
it("prints a tree with a child", () => {
const tree = {
name: "hi", children: [
Expand Down

0 comments on commit 4985c12

Please sign in to comment.