New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

hashes

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hashes - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

92

hashes.js
//A key-value pair constructor function for internal use
var KeyValuePair = function (key, value) {

@@ -80,6 +81,11 @@ this._key = key;

return {
getHashCode: options.getHashCode,
equal: options.equal
};
var result = {};
if (options.hasOwnProperty("getHashCode")){
result.getHashCode = options.getHashCode;
}
if (options.hasOwnProperty("equal")) {
result.equal = options.equal;
}
return result;
}

@@ -137,3 +143,19 @@ };

///Creates a new HashTable which is a union of the first and second HashTables. You may specify an optional options parameter and
///an optional overwriteIfExists parameter. The options are used to create the result HashTable and all the key-value pairs are added accordingly.
//When overwriteIfExists is true and a key from the second HashTable already exists in the first HashTable the entire key-value pair will be overwritten in the result.
///If overwriteIfExists is false then the key-value pair is ignored.
HashTable.union = function (first, second, options, overwriteIfExists) {
var result = new HashTable(options),
keyValuePairs;
keyValuePairs = first.getKeyValuePairs();
result.addRange(keyValuePairs, overwriteIfExists);
keyValuePairs = second.getKeyValuePairs();
result.addRange(keyValuePairs, overwriteIfExists);
return result;
};
//Prototype functions

@@ -173,2 +195,29 @@

///Adds a collection of keys and values to the HashTable, returns the number of items added.
///There are two possible uses:
///1) addRange(keyValuePairs,[overwriteIfExists]) - provide a collection of objects that have key and value property and an optional overwriteIfExists argument (see add for details)
///2) addRange(keys, values, [overwriteIfExists]) - provide two collections (one of keys and the other of values) and an optional overwriteIfExists argument (see add for details)
HashTable.prototype.addRange = function (arg1, arg2, arg3) {
var i, keysLength, valuesLength, minLength, result = 0;
if (arguments.length === 1 || (arguments.length === 2 && (typeof (arguments[1]) === "boolean" || arguments[1] === undefined))) {
for (i = 0, keysLength = arg1.length; i < keysLength; i += 1) {
if (this.add(arg1[i].key, arg1[i].value, arg2)) {
result += 1;
}
}
} else {
keysLength = arg1.length;
valuesLength = arg2.length;
minLength = Math.min(keysLength, valuesLength);
for (i = 0; i < minLength; i += 1) {
if (this.add(arg1[i], arg2[i], arg3)) {
result += 1;
}
}
}
return result;
};
///Retrieves the value associated with the given key. If the key doesn't exist null is returned.

@@ -391,10 +440,24 @@ HashTable.prototype.get = function (key) {

///Creates a new HashSet which is a union of the first and second HashSet. You may specify an optional options parameter and
///an optional overwriteIfExists parameter. The options are used to create the result HashSet and all the keys added accordingly.
//When overwriteIfExists is true and a key from the second HashSet already exists in the first HashTable it will be overwritten in the result.
///If overwriteIfExists is false then the key is ignored.
HashSet.union = function (first, second, options, overwriteIfExists) {
var result = new HashSet(options),
keys;
keys = first.getKeys();
result.addRange(keys, overwriteIfExists);
keys = second.getKeys();
result.addRange(keys);
return result;
};
//Prototype functions
///Adds a key the HashSet, returns true if any item was added and false otherwise.
///Adds a key to the HashSet, returns true if any item was added and false otherwise.
///you may specify the overwriteIfExists flag. When overwriteIfExists is true the key will be replaced
///if this key already exists in the HashSet. When overwriteIfExists is false and the key already exists then nothing
///will happen but the function will return false (since nothing was added)
HashSet.prototype.add = function (key, overwriteIfExists) {

@@ -427,2 +490,17 @@ var hash, addedItem, bucket, itemIndex;

///Adds a collection of keys to the HashSet, returns the number of keys added.
///you may specify the overwriteIfExists flag. When overwriteIfExists is true the key will be replaced
///if this key already exists in the HashSet. When overwriteIfExists is false and the key already exists then nothing
///will happen.
HashSet.prototype.addRange = function (keys, overwriteIfExists) {
var i, length, result = 0;
for (i = 0, length = keys.length; i < length; i += 1) {
if (this.add(keys[i], overwriteIfExists)) {
result += 1;
}
}
return result;
};
///Retrieves the key which is equal to the given key. If the key doesn't exist null is returned.

@@ -440,3 +518,3 @@ HashSet.prototype.get = function (key) {

}
bucket = this._buckets[hash];

@@ -443,0 +521,0 @@ itemIndex = HashSet.getKeyIndex(bucket, key, this._options);

2

package.json
{
"name": "hashes",
"version" : "0.1.0",
"version" : "0.1.1",
"description": "A package implementing a HashTable and HashSet",

@@ -5,0 +5,0 @@ "author": "Boris Kozorovitzky",

@@ -54,4 +54,14 @@ jsHash

* **add(key, value, [overwriteIfExists])** - Adds the given key-value pair to the HashTable. _overwriteIfExists_ is an optional argument that is used when the given key already exists in the HashTable.
If _overwriteIfExists_ is truthy then the given key-value pair will overwrite the existing key-value pair, otherwise the given key-value pair will not be added to the HashTable.
If _overwriteIfExists_ is truthy then the given key-value pair will overwrite the existing key-value pair, otherwise the given key-value pair will not be added to the HashTable. Returns true if the HashTable was modified and false otherwise.
* **addRange(keys, values, [overwriteIfExists])** - Adds the given keys and values as key-value pairs to the HashTable. If the length of the two arguments is different, the minimum length is used.
_overwriteIfExists_ is an optional argument that is used when the given key already exists in the HashTable.
If _overwriteIfExists_ is truthy then the given key-value pair will overwrite the existing key-value pair, otherwise the given key-value pair will not be added to the HashTable.
Returns the number of key-value pairs that were added to the HashTable.
* **addRange(keyValuePairs, [overwriteIfExists])** - Adds the given key-value pairs to the HashTable (each object in the given collection must contain a _key_ and a _value_ property).
_overwriteIfExists_ is an optional argument that is used when the given key already exists in the HashTable.
If _overwriteIfExists_ is truthy then the given key-value pair will overwrite the existing key-value pair, otherwise the given key-value pair will not be added to the HashTable.
Returns the number of key-value pairs that were added to the HashTable.
* **get(key)** - Returns the key-value pair associated with the given key. If there is no key-value pair associated with the given key, null is returned. The returned object is ```{key:key, value:value}```.

@@ -76,2 +86,9 @@ (Note that a pair is returned because the key in the HashTable may differ from the provided key.)

#### Static Functions
* **[static] union(first, second, [options], [overwriteIfExists])** - Creates a new HashTable which is a union of the first and second HashTables.
You may specify an optional _options_ parameter and an optional _overwriteIfExists_ parameter. The options are used to create the result HashTable and all the key-value pairs are added accordingly.
When _overwriteIfExists_ is true and a key from the second HashTable already exists in the first HashTable the entire key-value pair is overwritten in the result.
If _overwriteIfExists_ is false then the key-value pair from the second HashTable is ignored.
###HashSet

@@ -89,2 +106,7 @@ To create a new HashSet use the _new_ keyword:

* **addRange(keys, [overwriteIfExists])** - Adds the given keys to the HashSet.
_overwriteIfExists_ is an optional argument that is used when the given key already exists in the HashSet.
If _overwriteIfExists_ is truthy then the given key will overwrite the existing key, otherwise the given key pair will not be added to the HashSet.
Returns the number of keys that were added to the HashSet.
* **get(key)** - Returns the key in the HashSet which is equal to the given key. If there is no key equal to the given key, null is returned.

@@ -108,2 +130,10 @@

#### Static Functions
* **[static] union(first, second, [options], [overwriteIfExists])** - Creates a new HashSet which is a union of the first and second HashSets.
You may specify an optional _options_ parameter and an optional _overwriteIfExists_ parameter. The options are used to create the result HashSet and all the keys added accordingly.
When _overwriteIfExists_ is true and a key from the second HashSet already exists in the first HashTable it will be overwritten in the result.
If _overwriteIfExists_ is false then the key is ignored.
###statics

@@ -124,3 +154,11 @@ The _statics_ object is used internally to control the behavior of **all** the HashTable and HashSet instances.

## ChangeLog
0.1.0 -> 0.1.1
* Added the addRange functions
* Added static union functions
* Minor documentation fixes
* Fixed a bug where options argument had to include both getHashCode and equal functions
0.0.2 -> 0.1.0
* **Breaking** - Renamed the class from Hashtable to HashTable (note the capital 'T')

@@ -127,0 +165,0 @@ * **Breaking** - Moved some static functions of HashTable class to the statics object to accommodate the new HashSet class

@@ -116,5 +116,50 @@ /*global describe:true, it:true*/

it('should add a range of elements', function () {
var count, data = ["a", "b", "c"],
special = {
getHashCode: function () { return "a"; },
equal: function (other) { if (other === "a") return true; }
},
hashset = new jsHash.HashSet()
count = hashset.addRange(data);
count.should.equal(3);
hashset.count().should.equal(3);
hashset.get("a").should.equal("a");
hashset.clear();
count = hashset.addRange(data, false);
count.should.equal(3);
hashset.count().should.equal(3);
hashset.get("a").should.equal("a");
data.push(special);
hashset.clear();
count = hashset.addRange(data, true);
count.should.equal(4);
hashset.count().should.equal(3);
});
it('should union two HashSets using the static method', function () {
var hashset1 = new jsHash.HashSet(),
hashset2 = new jsHash.HashSet(),
special = {
getHashCode: function () { return "a"; },
equal: function (other) { if (other === "a") return true; }
}, result;
hashset1.add("a");
hashset2.add("c");
result = jsHash.HashSet.union(hashset1, hashset2);
should.exist(result);
result.count().should.equal(2);
result.get("a").should.equal("a");
result.get("c").should.equal("c");
hashset2.add(special);
result = jsHash.HashSet.union(hashset1, hashset2, {}, true);
result.count().should.equal(2);
result.get("c").should.equal("c");
});
});

@@ -26,3 +26,21 @@ /*global describe:true, it:true*/

value1 = "Some string", value2 = "Another string",
keyValuePairs = [
{
key: "a",
value: "b"
},
{
key: "a",
value: "c"
},
{
key: "b",
value: "d"
}
], keys = ["a", "b", "c", "a", "k"],
values = ["d", "e", "f", "g"],
hashtable = new jsHash.HashTable();

@@ -55,3 +73,3 @@

it('should not add a key, value pair to the HashTable', function () {
hashtable.add(key1, value1, false);
hashtable.add(key1, value1, false).should.equal(false);
hashtable.count().should.equal(1);

@@ -61,3 +79,3 @@ });

it('should overwrite a give key', function () {
hashtable.add(key1, value2, true);
hashtable.add(key1, value2, true).should.equal(true);
hashtable.count().should.equal(1);

@@ -140,3 +158,61 @@ var value = hashtable.get(key1).value;

it('should add a range of elements', function () {
var count;
hashtable.clear();
count = hashtable.addRange(keyValuePairs);
count.should.equal(2);
hashtable.count().should.equal(2);
hashtable.get(keyValuePairs[0].key).value.should.equal("b");
hashtable.clear();
count = hashtable.addRange(keyValuePairs, false);
count.should.equal(2);
hashtable.count().should.equal(2);
hashtable.get(keyValuePairs[0].key).value.should.equal("b");
hashtable.clear();
count = hashtable.addRange(keyValuePairs, true);
count.should.equal(3);
hashtable.count().should.equal(2);
hashtable.get(keyValuePairs[0].key).value.should.equal("c");
});
it('should add a range of elements 2', function () {
var count;
hashtable.clear();
count = hashtable.addRange(keys, values);
count.should.equal(3);
hashtable.count().should.equal(3);
hashtable.get(keyValuePairs[0].key).value.should.equal("d");
hashtable.clear();
count = hashtable.addRange(keys, values, false);
count.should.equal(3);
hashtable.count().should.equal(3);
hashtable.get(keyValuePairs[0].key).value.should.equal("d");
hashtable.clear();
count = hashtable.addRange(keys, values, true);
count.should.equal(4);
hashtable.count().should.equal(3);
hashtable.get(keyValuePairs[0].key).value.should.equal("g");
});
it('should union two HashTables using the static method', function () {
var hashtable1 = new jsHash.HashTable(),
hashtable2 = new jsHash.HashTable(),
result;
hashtable1.add("a", "b");
hashtable2.add("c", "d");
result = jsHash.HashTable.union(hashtable1, hashtable2);
should.exist(result);
result.count().should.equal(2);
result.get("a").value.should.equal("b");
result.get("c").value.should.equal("d");
hashtable2.add("a", "e");
result = jsHash.HashTable.union(hashtable1, hashtable2, {}, true);
result.count().should.equal(2);
result.get("a").value.should.equal("e");
result.get("c").value.should.equal("d");
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc