Permalink
Browse files

Added each() function for easy iteration, and bumped version.

  • Loading branch information...
1 parent e18060b commit 10923d739ba3a95cd5998fa46ac25780548b5415 Peter Scott committed Sep 28, 2010
Showing with 53 additions and 1 deletion.
  1. +2 −0 README.md
  2. +20 −0 lib/simplesets.js
  3. +1 −1 package.json
  4. +30 −0 test/testsets.js
View
@@ -90,6 +90,8 @@ The `Set` class has the following methods:
* `pick()`: Return a random element of the set, or null if the set is empty. Unlike `pop`, does not remove the element from the set.
+* `each(callback)`: Call a callback function on each element of the set. If the set is changed by the callback, the results are undefined. The callback takes a single argument: the set element that it's being called on.
+
The condition for determining whether two values are equal is the `===` operator. Therefore sets can support any mix of data types, as long as the data types can be compared for equality in some meaningful sense with `===`.
Specialized sets
View
@@ -129,6 +129,16 @@ exports.Set = function(items) {
return true;
};
+ // Call a callback function on each element of the set. If the set
+ // is changed by the callback, the results are undefined.
+ this.each = function(callback) {
+ // If there's no callback, don't bother.
+ if (!callback) return;
+
+ for (var i = 0; i < this._items.length; i++)
+ callback(this._items[i]);
+ }
+
// If initial items were given, add them to the set.
if (typeof items !== "undefined")
for (var i = 0; i < items.length; i++)
@@ -287,6 +297,16 @@ exports.StringSet = function(items) {
return true;
};
+ // Call a callback function on each element of the set. If the set
+ // is changed by the callback, the results are undefined.
+ this.each = function(callback) {
+ // If there's no callback, don't bother.
+ if (!callback) return;
+
+ for (var x in this._items)
+ callback(this._items[x]);
+ }
+
// If initial items were given, add them to the set.
if (typeof items !== "undefined")
for (var i = 0; i < items.length; i++)
View
@@ -1,5 +1,5 @@
{ "name": "simplesets",
- "version": "1.1.2",
+ "version": "1.1.3",
"description": "Simple set data type, with API similar to Python's sets module.",
"author": "Peter Scott <pjscott@iastate.edu>",
"main": "./lib/simplesets.js",
View
@@ -74,6 +74,21 @@ var s5 = new sets.Set();
s5.add(3).add(4).remove(3);
assert.ok(s5.equals(new sets.Set([4])));
+// Test each() function
+function set_test_each(s) {
+ n = new sets.Set();
+ s.each(function(x) {
+ assert.ok(!n.has(x));
+ n.add(x);
+ });
+ assert.ok(n.equals(s));
+ assert.ok(s.equals(n));
+}
+set_test_each(new sets.Set([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]));
+set_test_each(new sets.Set([1, 2, 3, "1", "2", "3", "foo"]));
+set_test_each(new sets.Set());
+set_test_each(new sets.Set({foo: 42}));
+
///////////////////////
// sets.StringSet tests
///////////////////////
@@ -146,5 +161,20 @@ var s5 = new sets.StringSet();
s5.add(3).add(4).remove(3);
assert.ok(s5.equals(new sets.StringSet([4])));
+// Test each() function
+function stringset_test_each(s) {
+ n = new sets.StringSet();
+ s.each(function(x) {
+ assert.ok(!n.has(x));
+ n.add(x);
+ });
+ assert.ok(n.equals(s));
+ assert.ok(s.equals(n));
+}
+set_test_each(new sets.StringSet([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]));
+set_test_each(new sets.StringSet([1, 2, 3, "11", "22", "33", "foo"]));
+set_test_each(new sets.StringSet());
+set_test_each(new sets.StringSet({foo: 42}));
+
// If we got to here, then...
console.log('All tests passed!');

0 comments on commit 10923d7

Please sign in to comment.