Skip to content

feat: add array set operations ($union, $intersection, $difference, $symmetricDifference) - #829

Open
imshawan wants to merge 1 commit into
jsonata-js:masterfrom
imshawan:feature/array-set-operations
Open

feat: add array set operations ($union, $intersection, $difference, $symmetricDifference)#829
imshawan wants to merge 1 commit into
jsonata-js:masterfrom
imshawan:feature/array-set-operations

Conversation

@imshawan

@imshawan imshawan commented Aug 5, 2026

Copy link
Copy Markdown

Hola! 👋

This PR adds four standard array set operations to JSONata: $union, $intersection, $difference, and $symmetricDifference.

Right now, if you want to compare or combine two separate arrays and filter out the duplicates, it requires writing some pretty complex custom expressions. These new functions make basic set math super easy right out of the box. They naturally use JSONata's existing deep-equality checks, so nested objects work exactly as expected.

What these operations do:

  • $union(array1, array2): Combines two arrays into a single array, removing all duplicates.
  • $intersection(array1, array2): Returns only the items that exist in both arrays.
  • $difference(array1, array2): Subtracts the second array from the first, returning items that exist only in the first array.
  • $symmetricDifference(array1, array2): Returns items that exist in either array, but NOT in both.

Crucially, all of these functions rely entirely on JSONata's existing internal deep-equality checks. This means they don't just work on flat arrays of strings or numbers—they seamlessly handle complex, nested objects (e.g. [{"id": 1}, {"id": 2}]) identically to how $distinct currently operates.

What's inside:

  • The Functions: Implemented in src/functions.js. To keep things consistent, they rely on the exact same internal deep-equality logic that $distinct already uses.
  • Context Injection: Bound them with the <a-a:a> signature in jsonata.js so they play perfectly with context injection and path sequences.
  • Tests: Added full test suites for all four functions under test/test-suite/groups/. They test primitives, deep object equality, missing/undefined arguments, and sequence datasets (via dataset5).
  • Docs: Appended the signatures, descriptions, and examples to docs/array-functions.md using the existing formatting.

Sanity Checks:

  • Verified that npm run check-coverage still hits exactly 100% globally.
  • Verified that npm run lint passes without any errors.

Let me know if there's anything you'd like me to tweak or if you have any questions!

…symmetricDifference)

- Implemented array set logic using deep equality
- Added function bindings to static frame
- Added extensive test suites using standard primitive and sequence datasets
- Documented new functions in array-functions.md

Signed-off-by: Shawan Mandal <github@imshawan.dev>
@imshawan

Copy link
Copy Markdown
Author

Hi @andrew-coleman , tagging you here in case this got lost in the notifications shuffle. Let me know if this looks good to go or if you'd like any revisions!

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.

1 participant