Skip to content

Set beyond removing duplicates;

Set in Javascript is a very important to remove duplicates but we use it to model some mathematical operations like. Union, intersection, difference, symmetric difference which map directly to real-world problems like permissions, filters, inventory, and recommendations. In this case we are going to use Set to for a user-role-permissions module to;

  • Compute all permissions a user has (union)
  • Check common permissions between users (intersection)
  • Find permissions revoked (difference)
  • Detect conflicting rights (symmetric difference)

1. We create helper functions to mimic Set operations.

js
function union(a, b) {
  return new Set([...a, ...b]); // combine, no duplicates
}

function intersection(a, b) {
  return new Set([...a].filter(x => b.has(x))); // only common values
}

function exclusive(a, b) {
  return new Set([...a].filter(x => !b.has(x))); // values in A but not in B
}

function symmetricDifference(a, b) {
  return union(exclusive(a, b), exclusive(b, a)); // unique to either side
}

2. Lets map Roles to Permissions

Sample roles are admin, editor and viewer

js
const roles = {
  admin: new Set(["read", "write", "delete", "deploy"]),
  editor: new Set(["read", "write"]),
  viewer: new Set(["read"]),
};

Sample users

js
const koomeRoles = ["admin"];
const aliceRoles = ["editor", "viewer"];

3. Merging user permissions from each set into a final list

To do this, we pass the user’s roles array (e.g. ["editor", "viewer"]) into getUserPermissions. Inside, we use .reduce() starting with an empty Set. For each role in the array, the callback looks up that role’s permissions and merges them with the accumulated set. Because we’re using a Set, the result is a unique collection of permissions with no duplicates.

js
function getUserPermissions(roleList) {
  return roleList.reduce(
    (perms, role) => union(perms, roles[role]),
    new Set()
  );
}

4. Get users persmissions

js
const koomePerms = getUserPermissions(koomeRoles);
const alicePerms = getUserPermissions(aliceRoles);

console.log("Koome perms:", [...koomePerms]); 
// [ 'read', 'write', 'delete', 'deploy' ]

console.log("Alice perms:", [...alicePerms]);     
//  [ 'read', 'write' ]

5. We may require to fetch the common permissions for both users

To get shared capabilities we just do;

js
console.log("Common perms:", [...intersection(koomePerms, alicePerms)]);
// [ 'read', 'write' ]

6. Exclusive Permissions (Difference)

what if we wanted to know which permissions does koome has over Alice.

js
console.log("Permissions Koome has over Alice:", [...exclusive(koomePerms, alicePerms)]);
// [ 'delete', 'deploy' ]

7. Conflicting Permissions (Symmetric Difference)

To know the permissions they do not share we;

js
console.log("Conflicting perms:", [...symmetricDifference(koomePerms, alicePerms)]);
// [ 'delete', 'deploy' ]

8. Other use cases

Intersection can be used in

  • Mutual friends in social networks
  • Shared tags across datasets
  • Overlapping filters in e-commerce

Exclusive

  • Features enabled in staging but not production
  • Content left to recommend (what user hasn’t seen yet)
  • Inventory items missing from stock

Symmetric Difference

  • Detect config mismatches across servers
  • Spot conflicting permissions between teams
  • Find unique tags or categories

javascript book

If this interested you, check out my Javascript Book

Enjoy every byte.