Set Difference And Symmetric Difference

Express asymmetric difference and symmetric difference with Set operations so changed keys and both-sided drift fall out of the comparison.

JavascriptData Integrity

Code

javascriptsnippet
19 lines
const before = new Set(['auth', 'billing', 'search', 'admin']);
const after = new Set(['billing', 'search', 'reports']);

// difference: keys only present in 'before' — candidates to decommission.
const removed = before.difference(after);
console.log('removed:', [...removed]);

// The mirrored call answers the other direction: what was added.
const added = after.difference(before);
console.log('added:', [...added]);

// symmetricDifference: changed on either side — the deploy diff itself.
const changed = before.symmetricDifference(after);
console.log('changed:', [...changed]);

// Cross-check: union minus intersection equals the symmetric difference.
const union = before.union(after);
const shared = before.intersection(after);
console.log('agrees:', [...union.difference(shared)].join() === [...changed].join());

Output

Captured from a sandboxed run of this snippet in the build pipeline.

removed: [ 'auth', 'admin' ]
added: [ 'reports' ]
changed: [ 'auth', 'admin', 'reports' ]
agrees: true

Inside the snippet

Top-level bindings before, after, removed, added hold the demo state — nothing sits inside a function scope.

Points of care

  • No package installation, no environment variables, no setup steps.

Putting it to use

The file announces its steps in the output: removed:, added:, changed:.

Core lines

const before = new Set(['auth', 'billing', 'search', 'admin']);
const after = new Set(['billing', 'search', 'reports']);

Interactive Tools for This Topic

FAQ

Are there dependencies to manage?
No. There are no dependencies to declare or install. Its state lives in module-level bindings: `before`, `after`, `removed`.