@rbxts/jsnatives
isSupersetOf
Determines whether the first set is a superset of the second set.
Signature
function isSupersetOf<T>(set1: Set<T>, set2: Set<T>): boolean
Description
The isSupersetOf
function checks if the first set is a superset of the second set. A set A is a superset of a set B if every element of B is also an element of A.
Parameters
set1
: The set to check if it's a superset.set2
: The set to check against.
Return value
true
if all elements ofset2
are present inset1
(meaningset1
is a superset ofset2
).false
otherwise.
Examples
Basic usage
// Create two setsconst largeSet = new Set([1, 2, 3, 4]);const smallSet = new Set([1, 2]);
// Check if largeSet is a superset of smallSetconst result = SetUtils.isSupersetOf(largeSet, smallSet);console.log(result); // Outputs: true
// Another exampleconst incompatibleSet = new Set([1, 5]);const result2 = SetUtils.isSupersetOf(largeSet, incompatibleSet);console.log(result2); // Outputs: false (5 is not in largeSet)
With string elements
// Sets with string elementsconst largeFruitSet = new Set(["apple", "banana", "orange", "grape"]);const smallFruitSet = new Set(["apple", "banana"]);
// Check if largeFruitSet is a superset of smallFruitSetconst isSuperset = SetUtils.isSupersetOf(largeFruitSet, smallFruitSet);console.log(isSuperset); // Outputs: true
Equal sets
// Two identical setsconst set1 = new Set([1, 2, 3]);const set2 = new Set([1, 2, 3]);
// A set is a superset of itselfconst result = SetUtils.isSupersetOf(set1, set2);console.log(result); // Outputs: true
Empty sets
// Any set is a superset of the empty setconst numbers = new Set([1, 2, 3]);const empty = new Set();
const numbersAreSuperset = SetUtils.isSupersetOf(numbers, empty);console.log(numbersAreSuperset); // Outputs: true
// Empty set is not a superset of non-empty setconst emptyIsSuperset = SetUtils.isSupersetOf(empty, numbers);console.log(emptyIsSuperset); // Outputs: false
Relationship with isSubsetOf
// isSupersetOf is the reverse of isSubsetOfconst set1 = new Set([1, 2, 3, 4]);const set2 = new Set([2, 3]);
// Using isSupersetOfconst supersetResult = SetUtils.isSupersetOf(set1, set2);console.log(supersetResult); // Outputs: true
// Equivalent check using isSubsetOf with reversed argumentsconst subsetResult = SetUtils.isSubsetOf(set2, set1);console.log(subsetResult); // Outputs: true
Practical application: Feature compatibility
// Check if a system provides all features needed by an applicationfunction supportsApplication(availableFeatures: Set<string>, requiredFeatures: Set<string>): boolean { return SetUtils.isSupersetOf(availableFeatures, requiredFeatures);}
// Features provided by a systemconst systemFeatures = new Set([ "bluetooth", "wifi", "gps", "camera", "accelerometer", "gyroscope"]);
// Features needed by app Aconst appARequirements = new Set([ "wifi", "camera", "gps"]);
// Features needed by app Bconst appBRequirements = new Set([ "wifi", "nfc", "camera"]);
// Check compatibilityconst appACompatible = supportsApplication(systemFeatures, appARequirements);console.log(appACompatible); // Outputs: true
const appBCompatible = supportsApplication(systemFeatures, appBRequirements);console.log(appBCompatible); // Outputs: false (missing "nfc")
Access control
// Check if a user has sufficient accessfunction hasAccess(userRoles: Set<string>, requiredRoles: Set<string>): boolean { return SetUtils.isSupersetOf(userRoles, requiredRoles);}
// User's rolesconst adminRoles = new Set([ "user", "editor", "moderator", "admin"]);
const editorRoles = new Set([ "user", "editor"]);
// Required roles for different operationsconst viewOperation = new Set(["user"]);const editOperation = new Set(["user", "editor"]);const deleteOperation = new Set(["moderator"]);
// Check accessconsole.log(hasAccess(adminRoles, viewOperation)); // Outputs: trueconsole.log(hasAccess(adminRoles, editOperation)); // Outputs: trueconsole.log(hasAccess(adminRoles, deleteOperation)); // Outputs: true
console.log(hasAccess(editorRoles, viewOperation)); // Outputs: trueconsole.log(hasAccess(editorRoles, editOperation)); // Outputs: trueconsole.log(hasAccess(editorRoles, deleteOperation)); // Outputs: false (missing "moderator")
Proper superset vs superset
// Check for proper superset (all elements in set2 are in set1, but set1 ≠ set2)function isProperSuperset<T>(set1: Set<T>, set2: Set<T>): boolean { return SetUtils.isSupersetOf(set1, set2) && set1.size > set2.size;}
const set1 = new Set([1, 2, 3]);const set2 = new Set([1, 2]);const set3 = new Set([1, 2, 3]);
// Regular superset checkconsole.log(SetUtils.isSupersetOf(set1, set2)); // Outputs: trueconsole.log(SetUtils.isSupersetOf(set1, set3)); // Outputs: true
// Proper superset checkconsole.log(isProperSuperset(set1, set2)); // Outputs: trueconsole.log(isProperSuperset(set1, set3)); // Outputs: false (equal sets)
Comparison with manual implementation
// Manual implementation of isSupersetOf operationfunction manualIsSupersetOf<T>(set1: Set<T>, set2: Set<T>): boolean { for (const item of set2) { if (!set1.has(item)) { return false; } } return true;}
// Using SetUtils.isSupersetOfconst largeSet = new Set([1, 2, 3, 4, 5]);const smallSet = new Set([1, 2]);const incompatibleSet = new Set([1, 6]);
const manualResult1 = manualIsSupersetOf(largeSet, smallSet);const utilResult1 = SetUtils.isSupersetOf(largeSet, smallSet);console.log(manualResult1); // Outputs: trueconsole.log(utilResult1); // Outputs: true
const manualResult2 = manualIsSupersetOf(largeSet, incompatibleSet);const utilResult2 = SetUtils.isSupersetOf(largeSet, incompatibleSet);console.log(manualResult2); // Outputs: falseconsole.log(utilResult2); // Outputs: false