Skip to content

allow child to be undefined or null#40

Closed
FredericHeem wants to merge 3 commits into
vanjs-org:mainfrom
FredericHeem:null-children
Closed

allow child to be undefined or null#40
FredericHeem wants to merge 3 commits into
vanjs-org:mainfrom
FredericHeem:null-children

Conversation

@FredericHeem

Copy link
Copy Markdown

This PR solves #16, allowing simpler client's code.

@Tao-VanJS Tao-VanJS left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is one issue with the implementation, if the value of child is 0 (number), the will be filtered out.

I'm thinking of the implementation like:

children.map(c => c ?? []).flat(Infinity).forEach(...)

Thanks so much for your contribution! I will do the merging this or next Friday, depending on my availability (currently only on Fridays I have convenient available personal time to work on this project).

@FredericHeem

Copy link
Copy Markdown
Author

Good catch, the filtering should be placed after the flattening, shoudn't it ?

@Tao-VanJS

Copy link
Copy Markdown
Member

After null or undefined is converted into [], calling to flat will produce nothing.

For example

[undefined, 1].map(c => c ?? []) // [[], 1]
[[], 1].flat(Infinity) // [1]

@Tao-VanJS

Copy link
Copy Markdown
Member

Thanks! This is great!

Comment thread src/van.js

let add = (dom, ...children) => (
children.flat(Infinity).map(c => c ?? []).forEach(child => dom.appendChild(
children.flat(Infinity).filter(c => c != undefined).forEach(child => dom.appendChild(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!= null is shorter and achieves the same thing.

@Tao-VanJS

Copy link
Copy Markdown
Member

This PR was manually merged in 0.12.1 release. However, @FredericHeem doesn't show up as the contributor of this repo despite I explicitly listed @FredericHeem as the co-author of the commit. Not sure why.

This method worked previously in another commit, though.

Nonetheless, I manually added @FredericHeem in the contributor list of the README.md file.

@Tao-VanJS Tao-VanJS closed this Jun 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants