Comparing version 0.9.1 to 0.9.2
/** | ||
* HashMap | ||
* @author Ariel Flesler <aflesler@gmail.com> | ||
* @version 0.9.1 | ||
* Date: 9/28/2012 | ||
* @version 0.9.2 | ||
* Date: 12/9/2012 | ||
* Homepage: https://github.com/flesler/hashmap | ||
@@ -17,11 +17,11 @@ */ | ||
constructor:HashMap, | ||
// TODO: Implement a way to iterate (.each()?) | ||
get:function(key) { | ||
return this._data[this.hash(key)]; | ||
var data = this._data[this.hash(key)]; | ||
return data && data[1]; | ||
}, | ||
set:function(key, value) { | ||
this._data[this.hash(key)] = value; | ||
// Store original key as well (for iteration) | ||
this._data[this.hash(key)] = [key, value]; | ||
}, | ||
@@ -91,3 +91,10 @@ | ||
} | ||
} | ||
}, | ||
forEach:function(func) { | ||
for (var key in this._data) { | ||
var data = this._data[key]; | ||
func(data[1], data[0]); | ||
} | ||
}, | ||
}; | ||
@@ -94,0 +101,0 @@ |
{ | ||
"name": "hashmap" | ||
, "author": "Ariel Flesler <aflesler@gmail.com>" | ||
, "version": "0.9.1" | ||
, "version": "0.9.2" | ||
, "description": "HashMap for Node" | ||
@@ -6,0 +6,0 @@ , "keywords": [ |
@@ -43,2 +43,12 @@ # HashMap Class for JavaScript | ||
### Iterating | ||
map.set(1, "test 1"); | ||
map.set(2, "test 2"); | ||
map.set(3, "test 3"); | ||
map.forEach(function(value, key) { | ||
console.log(key + " : " + value); | ||
}); | ||
[Check the tests](https://github.com/flesler/hashmap/blob/master/test/all.js) for some more real code. | ||
@@ -51,3 +61,2 @@ | ||
* (?) Allow extending the hashing function in a AOP way or by passing a service | ||
* Implement iteration (map.each()) | ||
* 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` | ||
@@ -81,2 +90,2 @@ * Use a real test framework | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
// TODO: Use an actual test framework, this is just a first draft version | ||
// TODO: Make a test version that works on browsers | ||
// TODO: Test HashMap.has() and HashMap.remove() | ||
// TODO: Make a test version that works on browsers | ||
@@ -31,3 +30,2 @@ var HashMap = require('../hashmap').HashMap; | ||
test.suite('Testing HashMap.hash()', function(){ | ||
@@ -50,3 +48,3 @@ function assertHash(data, expected) { | ||
assertHash(/test/, '/test/'); | ||
assertHash(new Date(1986, 7, 15, 12, 5, 0, 0), ':524502300000'); | ||
assertHash(new Date(Date.parse("Fri, 15 Aug 1986 15:05:00 GMT")), ':524502300000'); | ||
// Arrays | ||
@@ -62,3 +60,16 @@ assertHash([], '['); | ||
test.suite('Testing HashMap.has()', function(){ | ||
var map = new HashMap(); | ||
map.set('key1', 'value1'); | ||
test.assert("key1 exists", map.has('key1'), true); | ||
test.assert("key2 does not exist", map.has('key2'), false); | ||
}); | ||
test.suite('Testing HashMap.remove()', function(){ | ||
var map = new HashMap(); | ||
map.set('key1', 'value1'); | ||
test.assert("key1 exists", map.has('key1'), true); | ||
map.remove('key1'); | ||
test.assert("key1 no longer exists", map.has('key1'), false); | ||
}); | ||
@@ -93,3 +104,2 @@ test.suite('Testing same key remains mapped to same hash', function(){ | ||
test.suite('Testing pair of keys are mapped to the same hash', function(){ | ||
@@ -122,3 +132,2 @@ function assertSameHash(data, data2) { | ||
test.suite('Testing pair of keys are not mapped to the same hash', function(){ | ||
@@ -150,3 +159,2 @@ function assertDifferentHash(data, data2) { | ||
test.suite('Testing hashing an object doesn\'t add enumerable keys (no logs for OK)', function(){ | ||
@@ -173,3 +181,3 @@ var obj = {}; | ||
test.suite('Testing count() method', function(){ | ||
test.suite('Testing HashMap.count() method', function(){ | ||
var map = new HashMap(); | ||
@@ -195,3 +203,3 @@ | ||
test.suite('Testing clear() method', function(){ | ||
test.suite('Testing HashMap.clear() method', function(){ | ||
var map = new HashMap(); | ||
@@ -212,2 +220,22 @@ | ||
test.suite('Testing HashMap.forEach()', function(){ | ||
var map = new HashMap(); | ||
var keys = []; | ||
var values = []; | ||
map.set(1, "1"); | ||
map.set(2, "2"); | ||
map.set(3, "3"); | ||
map.forEach(function(value, key) { | ||
keys.push(key); | ||
values.push(value); | ||
test.assert("Correct pair", key.toString(), value); | ||
}); | ||
test.assert("Correct keys", keys.length, 3); | ||
test.assert("Correct values", values.length, 3); | ||
}); | ||
test.results(); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
14528
332
89
0