Skip to content

allow child to be undefined or null #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

FredericHeem
Copy link

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

Copy link
Member

@Tao-VanJS Tao-VanJS left a comment

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
Author

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

@Tao-VanJS
Copy link
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
Member

Thanks! This is great!

@@ -42,7 +42,7 @@ let state = initVal => ({
let toDom = v => v.nodeType ? v : new Text(v)

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(

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
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