Skip to content

Remove duplicate items in Algebra.prototype.getUnion()

Compare
Choose a tag to compare
@bmomberger-bitovi bmomberger-bitovi released this 17 Jan 19:51
· 14 commits to master since this release

Given an algebra:
set.props.id("id")

and these two sets:
{ type: 'a' }
{ area: 'x' }

And these two lists related to the respective sets:
[{ id: 1, type: 'a' area: 'x' }, { id: 2, type: 'a', area: 'y' }]
[{ id: 1, type: 'a' area: 'x' }, { id: 3, type: 'b', area: 'x' }]

Previously, the result of getUnion() would return a simple concatenation of these two lists:
[{ id: 1, type: 'a' area: 'x' }, { id: 2, type: 'a', area: 'y' }, { id: 1, type: 'a' area: 'x' }, { id: 3, type: 'b', area: 'x' }]

This is suboptimal in cases like in can-connect's cache-requests behavior, where the union of several sets may be used to build a query response for yet a different set. Each item should be represented at most once when creating the union. With this release, items in both aItems and bItems are removed from the bItems before concatenating into a union result. The example above becomes:

[{ id: 1, type: 'a' area: 'x' }, { id: 2, type: 'a', area: 'y' }, { id: 3, type: 'b', area: 'x' }]

Note: this doesn't remove all duplicates, just removes duplicates from bItems that are already in aItems -- if aItems already has a duplicate, or bItems has a duplicate not in aItems, it will be preserved.