hashmap
Advanced tools
Comparing version 0.9.0 to 0.9.1
/** | ||
* HashMap | ||
* @author Ariel Flesler <aflesler@gmail.com> | ||
* @version 0.1.2 | ||
* Date: 1/30/2012 | ||
* @version 0.9.1 | ||
* Date: 9/28/2012 | ||
* Homepage: https://github.com/flesler/hashmap | ||
@@ -12,3 +12,3 @@ */ | ||
function HashMap() { | ||
this._data = {}; // TODO: Would Object.create(null) make any difference | ||
this.clear(); | ||
}; | ||
@@ -47,2 +47,15 @@ | ||
count:function() { | ||
var n = 0; | ||
for (var key in this._data) { | ||
n++; | ||
} | ||
return n; | ||
}, | ||
clear:function() { | ||
// TODO: Would Object.create(null) make any difference | ||
this._data = {}; | ||
}, | ||
hash:function(key) { | ||
@@ -64,2 +77,7 @@ switch (this.type(key)) { | ||
case 'array': | ||
var hashes = []; | ||
for (var i = 0; i < key.length; i++) | ||
hashes[i] = this.hash(key[i]); | ||
return '[' + hashes.join('|'); | ||
case 'object': | ||
@@ -66,0 +84,0 @@ default: |
{ | ||
"name": "hashmap" | ||
, "author": "Ariel Flesler <aflesler@gmail.com>" | ||
, "version": "0.9.0" | ||
, "version": "0.9.1" | ||
, "description": "HashMap for Node" | ||
@@ -6,0 +6,0 @@ , "keywords": [ |
@@ -5,3 +5,3 @@ # HashMap Class for JavaScript | ||
This project provides a `HashMap` class that works both on NodeJS and the browser. | ||
This project provides a `HashMap` class that works both on __NodeJS__ and the __browser__. | ||
HashMap instances __store key/value pairs__ allowing __keys of any type__. | ||
@@ -16,2 +16,6 @@ | ||
var map = new HashMap(); | ||
If you're using this within Node, you first need to import the class | ||
var HashMap = require('hashmap').HashMap; | ||
@@ -48,3 +52,2 @@ ### Basic use case | ||
* (?) Allow extending the hashing function in a AOP way or by passing a service | ||
* (?) Arrays with the same elements are considered the same (currently not). | ||
* Implement iteration (map.each()) | ||
@@ -51,0 +54,0 @@ * Fix: The hashmap will expose an enumerable expando when `Object.defineProperty` doesn't exist maybe use a different hashing approach for this case like `Array.indexOf` |
@@ -7,8 +7,7 @@ // TODO: Use an actual test framework, this is just a first draft version | ||
var test = require('./lib/util.js'); | ||
var proto = HashMap.prototype; | ||
var map = new HashMap(); | ||
test.suite('Testing HashMap.type()', function(){ | ||
function assertType(data, expected) { | ||
test.assert(data, map.type(data), expected); | ||
test.assert(data, proto.type(data), expected); | ||
} | ||
@@ -36,3 +35,3 @@ | ||
function assertHash(data, expected) { | ||
test.assert(data, map.hash(data), expected); | ||
test.assert(data, proto.hash(data), expected); | ||
} | ||
@@ -53,7 +52,10 @@ | ||
assertHash(new Date(1986, 7, 15, 12, 5, 0, 0), ':524502300000'); | ||
// Arrays | ||
assertHash([], '['); | ||
assertHash([1, 2, 3], '[1|2|3'); | ||
// Unrecognized Objects | ||
assertHash([], '{1'); | ||
assertHash({}, '{2'); | ||
assertHash(HashMap, '{3'); | ||
assertHash(new HashMap, '{4'); | ||
assertHash({}, '{1'); | ||
assertHash(HashMap, '{2'); | ||
assertHash(new HashMap, '{3'); | ||
}); | ||
@@ -73,4 +75,6 @@ | ||
} | ||
} | ||
}; | ||
var map = new HashMap(); | ||
assertKey(null); | ||
@@ -101,4 +105,6 @@ assertKey(undefined); | ||
} | ||
} | ||
}; | ||
var map = new HashMap(); | ||
assertSameHash(null, null); | ||
@@ -112,2 +118,5 @@ assertSameHash(undefined, undefined); | ||
assertSameHash(new Date(1986, 7, 15, 12, 5, 0, 0), new Date(1986, 7, 15, 12, 5, 0, 0)); | ||
assertSameHash([], []); | ||
assertSameHash([1, 2, 'Q'], [1, 2, 'Q']); | ||
assertSameHash([null, /a/, NaN], [null, /a/, NaN]); | ||
}); | ||
@@ -127,4 +136,6 @@ | ||
} | ||
} | ||
}; | ||
var map = new HashMap(); | ||
assertDifferentHash(null, undefined); | ||
@@ -139,3 +150,2 @@ assertDifferentHash(null, false); | ||
assertDifferentHash({}, {}); | ||
assertDifferentHash([], []); | ||
}); | ||
@@ -145,3 +155,2 @@ | ||
test.suite('Testing hashing an object doesn\'t add enumerable keys (no logs for OK)', function(){ | ||
var obj = {}; | ||
@@ -155,3 +164,3 @@ var preset = []; | ||
map.hash(obj); | ||
proto.hash(obj); | ||
@@ -168,2 +177,39 @@ // Uncomment to generate an error | ||
test.suite('Testing count() method', function(){ | ||
var map = new HashMap(); | ||
test.assert("0 entries", map.count(), 0); | ||
map.set(1, "test"); | ||
test.assert("1 entry", map.count(), 1); | ||
map.set("1", "test"); | ||
test.assert("2 entries", map.count(), 2); | ||
map.set("1", "another_value"); | ||
test.assert("same entries after update", map.count(), 2); | ||
map.remove(1); | ||
test.assert("1 entry after remove()", map.count(), 1); | ||
map.remove("1"); | ||
test.assert("0 entries after remove()", map.count(), 0); | ||
}); | ||
test.suite('Testing clear() method', function(){ | ||
var map = new HashMap(); | ||
map.clear(); | ||
test.assert("0 entries after empty clear()", map.count(), 0); | ||
test.assert("0 entries", map.count(), 0); | ||
map.set(1, "test"); | ||
map.set(2, "test2"); | ||
test.assert("2 entries", map.count(), 2); | ||
map.clear(); | ||
test.assert("0 entries after clear()", map.count(), 0); | ||
}); | ||
test.results(); |
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
12903
298
79
1