mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-12-04 03:44:58 +00:00
New: for..of iteration for CPSet (#3114)
This commit is contained in:
1 parent
86d595b086
commit
85f7125f22
2 files changed
+67
No files matched your search
@@ -138,3 +138,29 @@ var hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
// This makes all CPSet instances iterable using for...of or the spread operator.
|
||||
//
|
||||
// This implementation is 'lazy' and efficient, iterating directly over the
|
||||
// internal _contents object rather than creating a temporary array.
|
||||
//
|
||||
|
||||
_CPConcreteMutableSet.allocator.prototype[Symbol.iterator] = function*()
|
||||
{
|
||||
// 'this._contents' is the internal JavaScript object used for storage.
|
||||
// The keys are object UIDs and the values are the objects themselves.
|
||||
const contents = this._contents;
|
||||
|
||||
// A 'for...in' loop iterates over the keys (UIDs) of the contents object.
|
||||
// This is a lazy operation and does not create an intermediate array.
|
||||
for (const key in contents)
|
||||
{
|
||||
// It's crucial to check that the key is an own property and not
|
||||
// one inherited from Object.prototype.
|
||||
if (Object.prototype.hasOwnProperty.call(contents, key))
|
||||
{
|
||||
// Yield the VALUE, which is the actual object stored in the set.
|
||||
yield contents[key];
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -396,4 +396,45 @@
|
||||
[self assert:2 equals:[output count] message:@"output when enumerating input1 and stopping after 2"];
|
||||
}
|
||||
|
||||
// Iteration tests using for...of and spread syntax.
|
||||
|
||||
- (void)testSetForOfIteration
|
||||
{
|
||||
// We include duplicate items to ensure the set creation works correctly.
|
||||
var sourceArray = [@"a", @"b", @"c", @"a", 1, 2, 1];
|
||||
var set = [CPSet setWithArray:sourceArray];
|
||||
|
||||
// Assert that the set has the correct number of unique items.
|
||||
[self assert:5 equals:[set count] message:@"Set should contain 5 unique items"];
|
||||
|
||||
var itemsSeen = [CPMutableSet set];
|
||||
|
||||
// 1. Test basic for...of iteration
|
||||
for (var item of set)
|
||||
{
|
||||
[itemsSeen addObject:item];
|
||||
}
|
||||
|
||||
[self assert:set equals:itemsSeen message:@"Set iterated with for...of should be equal to the original"];
|
||||
|
||||
|
||||
// 2. Test with spread syntax
|
||||
var spreadArray = [...set];
|
||||
[self assert:5 equals:spreadArray.length message:@"Spread syntax should produce an array with 5 items"];
|
||||
|
||||
// Convert the spread array back to a set to verify contents, since order is not guaranteed.
|
||||
var setFromSpread = [CPSet setWithArray:spreadArray];
|
||||
[self assert:set equals:setFromSpread message:@"Set rebuilt from spread array should be equal to the original"];
|
||||
|
||||
|
||||
// 3. Test on an empty set
|
||||
var emptySet = [CPSet set];
|
||||
var iterations = 0;
|
||||
for (var entry of emptySet)
|
||||
{
|
||||
iterations++;
|
||||
}
|
||||
[self assert:0 equals:iterations message:@"for...of on an empty set should not iterate"];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user