Add $filter on databinding to filter arrays using conditional operators#74
Add $filter on databinding to filter arrays using conditional operators#74
Conversation
Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
|
@copilot in your opinion, could a filter be accomplished without the new $filter, and an $if tag be used? |
Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
|
@copilot your comparison between $if and $filter isnt really direct, in the $if case you would only have a $then and not an $else, right? Also, in the implementation, I dont want an array.filter, I think I'd rather (in the existing bound loop) just run a single condition check and continue. |
Yes, you could accomplish filtering with $if tags by wrapping each item in an $if that checks the condition. However, $filter is more declarative and cleaner for this specific use case: With $if (one $if per item): {
div: {
$bind: "products",
$children: [{
$if: {
$check: "inStock",
$then: {
div: { $children: [{ h3: "{{name}}" }] }
}
}
}]
}
}With $filter (one filter for entire array): {
div: {
$bind: "products",
$filter: { $check: "inStock" },
$children: [{
div: { $children: [{ h3: "{{name}}" }] }
}]
}
}The $filter approach is more explicit about intent ("show only items matching condition") and avoids wrapping every child template in conditional logic. |
Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
|
@copilot i think if we allow a string-based shortcut then LLMs might intuit that they would/could embed the conditional in the string, which is not allowed. For example, I might expect a hallucination of |
You're absolutely right. LLMs would definitely hallucinate syntax like Recommendation: Keep only the object form This keeps the API explicit and predictable:
The slight verbosity is worth avoiding the confusion and hallucinations. |
|
…e filter tests to common-tests Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
All three items addressed in 346786c:
|
…le $if in $children Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
Updated in 75c1deb to explicitly list all available operators: |
…yles section Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
|
@copilot just looking at the playground again at your Filter vs If comparison - youre still not comparing apples to apples. For the filter, you are doing "$bind": "products", |
…with $if at $bind level Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
Fixed in 185957d. The comparison now shows both approaches at the same level:
Both now filter to show only in-stock products, making it a true apples-to-apples comparison. |
|
@copilot its hard to get the gist of the Filter vs If comparison because its so nested, and then you introduced all this tag soup. simplify it so that each is just a ul/li and dont add so many children in the li either. we are only trying to show that filter is a streamlined if. |
|
@copilot its hard to get the gist of the Filter vs If comparison because its so nested, and then you introduced all this tag soup. simplify it so that each is just a ul/li and dont add so many children in the li either. we are only trying to show that filter is a streamlined if. |
Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
Simplified in 0a09147. The comparison now uses ul/li with just the product name, removing all the nested divs, styling, and extra content. Both approaches show only in-stock products (Laptop, Tablet) in a simple list format, making it easy to see that $filter is just a cleaner way to accomplish the same filtering that $if does. |
|
@copilot i'm a little confused why $if cant be used on the same node as $bind. Because thats what $filter is doing. I'm not asking you to implement it, just tell me why it doesnt currently work and why. |
The key difference is scope:
|
|
@copilot I was looking at the readme again, and there is a section called Special Keys - can you add $filter there too (maybe add links for $filter and $if to their respective sections). Also glance at the whole doc again to see if I've missed anything. |
…pecial Keys Co-authored-by: danmarshall <11507384+danmarshall@users.noreply.github.com>
Added links in commit 6ae3d05. The Special Keys section already included
The document structure is complete with clear navigation between the reference section (Special Keys) and the detailed examples (Filtering Arrays, Conditional Rendering). |
@copilot oic - I was confused because the $ prefix usage. for some reason my brain parsed $ as only on attributes, but $if is a tag. |
Plan: Add $filter feature on databinding
Summary
This PR adds
$filterattribute for filtering arrays during databinding using conditional operators ($check,$<,$>,$<=,$>=,$=,$in,$join,$not).Benefits:
$iffor filteringDocumentation:
$filteralready included in Special Keys sectionAll 389 tests passing (379 original + 10 new type-safety tests)
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.