@rbxts/jsnatives
union
Creates a new Set containing all elements from both sets.
Signature
function union<T>(set1: Set<T>, set2: Set<T>): Set<T>
Description
The union
function combines two sets together, creating a new Set that contains all the elements from both input sets. If an element is present in both sets, it will appear only once in the resulting Set.
Parameters
set1
: The first Set.set2
: The second Set.
Return value
- A new Set containing all elements from both
set1
andset2
.
Examples
Basic usage
// Create two sets with some numbersconst set1 = new Set([1, 2, 3, 4]);const set2 = new Set([3, 4, 5, 6]);
// Combine both sets using unionconst result = SetUtils.union(set1, set2);console.log([...result]); // Outputs: [1, 2, 3, 4, 5, 6]
With string elements
// Sets with string elementsconst fruits1 = new Set(["apple", "banana", "orange"]);const fruits2 = new Set(["banana", "grape", "kiwi"]);
// Combine the fruit setsconst allFruits = SetUtils.union(fruits1, fruits2);console.log([...allFruits]); // Outputs: ["apple", "banana", "orange", "grape", "kiwi"]
With mixed types
// Sets with mixed element typesconst mixedSet1 = new Set([1, "hello", true]);const mixedSet2 = new Set([2, "world", true]);
// Union of mixed type setsconst combinedMixed = SetUtils.union(mixedSet1, mixedSet2);console.log([...combinedMixed]); // Outputs: [1, "hello", true, 2, "world"]
Empty sets
// Union with an empty setconst numbers = new Set([1, 2, 3]);const empty = new Set();
// Union with an empty set returns a copy of the non-empty setconst result1 = SetUtils.union(numbers, empty);console.log([...result1]); // Outputs: [1, 2, 3]
// Union of two empty sets returns an empty setconst result2 = SetUtils.union(empty, empty);console.log([...result2]); // Outputs: []
Practical application
// Merging user permissionsfunction mergePermissions(basePermissions: Set<string>, rolePermissions: Set<string>): Set<string> { return SetUtils.union(basePermissions, rolePermissions);}
// User has base permissionsconst userPermissions = new Set(["read", "comment"]);
// User's role has additional permissionsconst adminPermissions = new Set(["write", "delete", "moderate"]);
// Combine permissionsconst effectivePermissions = mergePermissions(userPermissions, adminPermissions);console.log([...effectivePermissions]);// Outputs: ["read", "comment", "write", "delete", "moderate"]
Comparison with manual implementation
// Manual implementation of union operationfunction manualUnion<T>(set1: Set<T>, set2: Set<T>): Set<T> { const result = new Set<T>(set1); for (const item of set2) { result.add(item); } return result;}
// Using SetUtils.unionconst set1 = new Set([1, 2, 3]);const set2 = new Set([3, 4, 5]);
const manualResult = manualUnion(set1, set2);const utilResult = SetUtils.union(set1, set2);
console.log([...manualResult]); // Outputs: [1, 2, 3, 4, 5]console.log([...utilResult]); // Outputs: [1, 2, 3, 4, 5]