simplesets
Advanced tools
Comparing version 1.1.6 to 1.2.0
@@ -130,8 +130,15 @@ // Set code for Node.js, which stores objects in arrays. All sets are | ||
// is changed by the callback, the results are undefined. | ||
each: function(callback) { | ||
// Callback takes the same parameters as the forEach method of | ||
// arrays: value, index, set | ||
// Takes an optional parameter that sets what this is bound to. | ||
each: function(callback, thisArg) { | ||
// If there's no callback, don't bother. | ||
if (!callback) return; | ||
if (thisArg) { | ||
callback = callback.bind(thisArg); | ||
} | ||
for (var i = 0; i < this._items.length; i++) | ||
callback(this._items[i]); | ||
callback(this._items[i], i, this); | ||
} | ||
@@ -296,8 +303,13 @@ }; | ||
// is changed by the callback, the results are undefined. | ||
each: function(callback) { | ||
// Callback takes the same parameters as the forEach method of | ||
// arrays: value, index, set | ||
// Takes an optional parameter that sets what this is bound to. | ||
each: function(callback, thisArg) { | ||
// If there's no callback, don't bother. | ||
if (!callback) return; | ||
if (thisArg) callback = callback.bind(thisArg); | ||
for (var x in this._items) | ||
callback(this._items[x]); | ||
callback(this._items[x], x, this); | ||
} | ||
@@ -304,0 +316,0 @@ } |
{ "name": "simplesets", | ||
"version": "1.1.6", | ||
"version": "1.2.0", | ||
"description": "Simple set data type, with API similar to Python's sets module.", | ||
@@ -11,2 +11,2 @@ "author": "Peter Scott <pjscott@iastate.edu>", | ||
} | ||
} | ||
} |
@@ -20,36 +20,38 @@ Simple set datatype for JavaScript | ||
var sets = require('simplesets'); | ||
```js | ||
var sets = require('simplesets'); | ||
var s1 = new sets.Set(['hello', 'world', 'how', 'are', 'you', 'today']); | ||
var s2 = new sets.Set(['say', 'hello', 'to', 'the', 'world']); | ||
// Print out both of the sets, as arrays of their elements. | ||
console.log('s1 =', s1.array()); | ||
console.log('s2 =', s2.array()); | ||
// Do some set operations. | ||
console.log('Intersection:', s1.intersection(s2).array()); | ||
console.log('s1 - s2:', s1.difference(s2).array()); | ||
console.log('s2 - s1:', s2.difference(s1).array()); | ||
console.log('Union:', s1.union(s2).array()); | ||
// Make a set with numbers and strings. | ||
var s3 = new sets.Set([1, 2, 3, 'a', 'b', 'c']); | ||
console.log('Mixing data types:', s3.array()); | ||
// Add in some more data types. | ||
var my_dict = {foo: 42, bar: 'bazaar'}; | ||
s3.add(my_dict); // This will add to the set... | ||
s3.add(my_dict); // ...but now this will do nothing. | ||
s3.remove(3); | ||
s3.remove('c'); | ||
console.log('New s3 =', s3.array()); | ||
// You can make shallow copies of sets. | ||
var s4 = new sets.Set([1, 2, 3]); | ||
var s5 = s4.copy(); | ||
s4.add(42); | ||
s5.remove(2); | ||
console.log('s4 =', s4.array()); | ||
console.log('s5 =', s5.array()); | ||
``` | ||
var s1 = new sets.Set(['hello', 'world', 'how', 'are', 'you', 'today']); | ||
var s2 = new sets.Set(['say', 'hello', 'to', 'the', 'world']); | ||
// Print out both of the sets, as arrays of their elements. | ||
console.log('s1 =', s1.array()); | ||
console.log('s2 =', s2.array()); | ||
// Do some set operations. | ||
console.log('Intersection:', s1.intersection(s2).array()); | ||
console.log('s1 - s2:', s1.difference(s2).array()); | ||
console.log('s2 - s1:', s2.difference(s1).array()); | ||
console.log('Union:', s1.union(s2).array()); | ||
// Make a set with numbers and strings. | ||
var s3 = new sets.Set([1, 2, 3, 'a', 'b', 'c']); | ||
console.log('Mixing data types:', s3.array()); | ||
// Add in some more data types. | ||
var my_dict = {foo: 42, bar: 'bazaar'}; | ||
s3.add(my_dict); // This will add to the set... | ||
s3.add(my_dict); // ...but now this will do nothing. | ||
s3.remove(3); | ||
s3.remove('c'); | ||
console.log('New s3 =', s3.array()); | ||
// You can make shallow copies of sets. | ||
var s4 = new sets.Set([1, 2, 3]); | ||
var s5 = s4.copy(); | ||
s4.add(42); | ||
s5.remove(2); | ||
console.log('s4 =', s4.array()); | ||
console.log('s5 =', s5.array()); | ||
The set data type has the simplest, stupidest implementation possible: an unordered array. This is because of how JavaScript's data types work. If it were possible to compute a hash value from any data type, or get its memory address, then we could do something more elaborate. If `<` and `>` operations were defined for all data types, we could use some kind of balanced tree representation, or sorted arrays. If JavaScript objects supported arbitrary data types as indices, this would all be too easy. But none of those things is true, so we're stuck relying only on the `===` operation, and unsorted arrays. For small sets, this is not a problem. For larger sets, if performance of set operations turns out to be problematic, you may want to use a specialized set data type. For example, if your set members are all strings, you could represent sets as objects with set members as keys, and it would be fast. For this, use the `StringSet` class, described below. | ||
@@ -94,3 +96,3 @@ | ||
* `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. | ||
* `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. Callback takes the same parameters as the forEach method of arrays: value, index, set. Takes an optional parameter that sets what this is bound to. | ||
@@ -97,0 +99,0 @@ 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 `===`. |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
22318
466
107
0