@rbxts/jsnatives
isSubsetOf
Determines whether the first set is a subset of the second set.
Signature
function isSubsetOf<T>(set1: Set<T>, set2: Set<T>): boolean
Description
The isSubsetOf
function checks if the first set is a subset of the second set. A set A is a subset of a set B if every element of A is also an element of B.
Parameters
set1
: The set to check if it's a subset.set2
: The set to check against.
Return value
true
if all elements ofset1
are present inset2
(meaningset1
is a subset ofset2
).false
otherwise.
Examples
Basic usage
// Create two setsconst set1 = new Set([1, 2]);const set2 = new Set([1, 2, 3, 4]);
// Check if set1 is a subset of set2const result = SetUtils.isSubsetOf(set1, set2);console.log(result); // Outputs: true
// Another exampleconst set3 = new Set([1, 2, 5]);const result2 = SetUtils.isSubsetOf(set3, set2);console.log(result2); // Outputs: false (5 is not in set2)
With string elements
// Sets with string elementsconst smallFruitSet = new Set(["apple", "banana"]);const largeFruitSet = new Set(["apple", "banana", "orange", "grape"]);
// Check if smallFruitSet is a subset of largeFruitSetconst isSubset = SetUtils.isSubsetOf(smallFruitSet, largeFruitSet);console.log(isSubset); // Outputs: true
Equal sets
// Two identical setsconst set1 = new Set([1, 2, 3]);const set2 = new Set([1, 2, 3]);
// A set is a subset of itselfconst result = SetUtils.isSubsetOf(set1, set2);console.log(result); // Outputs: true
Empty sets
// Empty set is a subset of any setconst empty = new Set();const numbers = new Set([1, 2, 3]);
const emptyIsSubset = SetUtils.isSubsetOf(empty, numbers);console.log(emptyIsSubset); // Outputs: true
// Non-empty set is not a subset of empty setconst numbersAreSubset = SetUtils.isSubsetOf(numbers, empty);console.log(numbersAreSubset); // Outputs: false
Practical application: Permission checking
// Check if a user has all required permissionsfunction hasRequiredPermissions(userPermissions: Set<string>, requiredPermissions: Set<string>): boolean { return SetUtils.isSubsetOf(requiredPermissions, userPermissions);}
// User's permissionsconst userPermissions = new Set([ "read", "write", "edit", "delete", "admin"]);
// Permissions required for an operationconst requiredForOperation = new Set([ "read", "write", "edit"]);
// Check if user can perform the operationconst canPerformOperation = hasRequiredPermissions(userPermissions, requiredForOperation);console.log(canPerformOperation); // Outputs: true
// Permissions required for another operationconst requiredForAdminOperation = new Set([ "read", "write", "admin", "superuser"]);
// Check for admin operationconst canPerformAdminOp = hasRequiredPermissions(userPermissions, requiredForAdminOperation);console.log(canPerformAdminOp); // Outputs: false (missing "superuser")
Checking feature support
// Check if a system supports all required featuresfunction supportsAllFeatures(systemFeatures: Set<string>, requiredFeatures: Set<string>): boolean { return SetUtils.isSubsetOf(requiredFeatures, systemFeatures);}
// Features provided by a systemconst systemFeatures = new Set([ "bluetooth", "wifi", "gps", "camera", "accelerometer"]);
// Features needed by app Aconst appARequirements = new Set([ "wifi", "camera"]);
// Features needed by app Bconst appBRequirements = new Set([ "bluetooth", "nfc", "camera"]);
// Check compatibilityconst appACompatible = supportsAllFeatures(systemFeatures, appARequirements);console.log(appACompatible); // Outputs: true
const appBCompatible = supportsAllFeatures(systemFeatures, appBRequirements);console.log(appBCompatible); // Outputs: false (missing "nfc")
Proper subset vs subset
// Check for proper subset (all elements in set1 are in set2, but set1 ≠ set2)function isProperSubset<T>(set1: Set<T>, set2: Set<T>): boolean { return SetUtils.isSubsetOf(set1, set2) && set1.size < set2.size;}
const set1 = new Set([1, 2]);const set2 = new Set([1, 2, 3]);const set3 = new Set([1, 2]);
// Regular subset checkconsole.log(SetUtils.isSubsetOf(set1, set2)); // Outputs: trueconsole.log(SetUtils.isSubsetOf(set1, set3)); // Outputs: true
// Proper subset checkconsole.log(isProperSubset(set1, set2)); // Outputs: trueconsole.log(isProperSubset(set1, set3)); // Outputs: false (equal sets)
Comparison with manual implementation
// Manual implementation of isSubsetOf operationfunction manualIsSubsetOf<T>(set1: Set<T>, set2: Set<T>): boolean { for (const item of set1) { if (!set2.has(item)) { return false; } } return true;}
// Using SetUtils.isSubsetOfconst smallSet = new Set([1, 2]);const largeSet = new Set([1, 2, 3, 4, 5]);const incompatibleSet = new Set([1, 6]);
const manualResult1 = manualIsSubsetOf(smallSet, largeSet);const utilResult1 = SetUtils.isSubsetOf(smallSet, largeSet);console.log(manualResult1); // Outputs: trueconsole.log(utilResult1); // Outputs: true
const manualResult2 = manualIsSubsetOf(incompatibleSet, largeSet);const utilResult2 = SetUtils.isSubsetOf(incompatibleSet, largeSet);console.log(manualResult2); // Outputs: falseconsole.log(utilResult2); // Outputs: false