Comparing version 2.0.4 to 2.0.5
{ | ||
"name": "hashmap", | ||
"version": "2.0.3", | ||
"version": "2.0.5", | ||
"description": "HashMap Class for JavaScript", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/flesler/hashmap", |
# Changelog | ||
## 2.0.5 | ||
- count() is now O(1), thanks @qbolec | ||
## 2.0.4 | ||
- hasOwnProperty() is used to check for the internal expando, thanks @psionski | ||
## 2.0.3 | ||
@@ -31,2 +37,2 @@ - forEach method accepts a context as 2nd argument (thanks mvayngrib) | ||
## 1.0.0 | ||
- First release | ||
- First release |
/** | ||
* HashMap - HashMap Class for JavaScript | ||
* @author Ariel Flesler <aflesler@gmail.com> | ||
* @version 2.0.3 | ||
* @version 2.0.5 | ||
* Homepage: https://github.com/flesler/hashmap | ||
@@ -42,3 +42,7 @@ */ | ||
// Store original key as well (for iteration) | ||
this._data[this.hash(key)] = [key, value]; | ||
var hash = this.hash(key); | ||
if ( !(hash in this._data) ) { | ||
this._count++; | ||
} | ||
this._data[hash] = [key, value]; | ||
}, | ||
@@ -51,4 +55,7 @@ | ||
copy:function(other) { | ||
for (var key in other._data) { | ||
this._data[key] = other._data[key]; | ||
for (var hash in other._data) { | ||
if ( !(hash in this._data) ) { | ||
this._count++; | ||
} | ||
this._data[hash] = other._data[hash]; | ||
} | ||
@@ -72,3 +79,7 @@ }, | ||
remove:function(key) { | ||
delete this._data[this.hash(key)]; | ||
var hash = this.hash(key); | ||
if ( hash in this._data ) { | ||
this._count--; | ||
delete this._data[hash]; | ||
} | ||
}, | ||
@@ -88,3 +99,3 @@ | ||
var keys = []; | ||
this.forEach(function(value, key) { keys.push(key); }); | ||
this.forEach(function(_, key) { keys.push(key); }); | ||
return keys; | ||
@@ -100,3 +111,3 @@ }, | ||
count:function() { | ||
return this.keys().length; | ||
return this._count; | ||
}, | ||
@@ -107,2 +118,3 @@ | ||
this._data = {}; | ||
this._count = 0; | ||
}, | ||
@@ -109,0 +121,0 @@ |
{ | ||
"name": "hashmap", | ||
"author": "Ariel Flesler <aflesler@gmail.com>", | ||
"version": "2.0.4", | ||
"description": "HashMap Class for JavaScript", | ||
"keywords": ["hashmap", "map", "object", "array", "associative", "javascript", "nodejs", "node", "browser"], | ||
"license": "MIT", | ||
"homepage": "https://github.com/flesler/hashmap", | ||
"bugs": "https://github.com/flesler/hashmap/issues", | ||
"repository": "git://github.com/flesler/hashmap", | ||
"main": "./hashmap.js", | ||
"scripts": { | ||
"test": "mocha test/*.js" | ||
}, | ||
"engines": { "node": "*" }, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"chai": "*" | ||
} | ||
"name": "hashmap", | ||
"author": "Ariel Flesler <aflesler@gmail.com>", | ||
"version": "2.0.5", | ||
"description": "HashMap Class for JavaScript", | ||
"keywords": [ | ||
"hashmap", | ||
"map", | ||
"object", | ||
"array", | ||
"associative", | ||
"javascript", | ||
"nodejs", | ||
"node", | ||
"browser" | ||
], | ||
"license": "MIT", | ||
"homepage": "https://github.com/flesler/hashmap", | ||
"bugs": "https://github.com/flesler/hashmap/issues", | ||
"repository": "git://github.com/flesler/hashmap", | ||
"main": "./hashmap.js", | ||
"scripts": { | ||
"test": "mocha test/*.js", | ||
"prepush": "npm test -- --reporter dot" | ||
}, | ||
"engines": { | ||
"node": "*" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"husky": "^0.11.4", | ||
"mocha": "^2.4.5" | ||
} | ||
} | ||
@@ -5,4 +5,6 @@ # HashMap Class for JavaScript | ||
Using npm: | ||
[![NPM](https://nodei.co/npm/hashmap.png?compact=true)](https://npmjs.org/package/hashmap) | ||
Using [npm](https://npmjs.org/package/hashmap): | ||
$ npm install hashmap | ||
@@ -60,18 +62,25 @@ | ||
var map = new HashMap(); | ||
```js | ||
var map = new HashMap(); | ||
``` | ||
If you're using this within Node, you first need to import the class | ||
var HashMap = require('hashmap'); | ||
```js | ||
var HashMap = require('hashmap'); | ||
``` | ||
### Basic use case | ||
map.set("some_key", "some value"); | ||
map.get("some_key"); // --> "some value" | ||
```js | ||
map.set("some_key", "some value"); | ||
map.get("some_key"); // --> "some value" | ||
``` | ||
### No stringification | ||
map.set("1", "string one"); | ||
map.set(1, "number one"); | ||
map.get("1"); // --> "string one" | ||
```js | ||
map.set("1", "string one"); | ||
map.set(1, "number one"); | ||
map.get("1"); // --> "string one" | ||
``` | ||
@@ -82,8 +91,9 @@ A regular `Object` used as a map would yield `"number one"` | ||
var key = {}; | ||
var key2 = {}; | ||
map.set(key, 123); | ||
map.set(key2, 321); | ||
map.get(key); // --> 123 | ||
```js | ||
var key = {}; | ||
var key2 = {}; | ||
map.set(key, 123); | ||
map.set(key2, 321); | ||
map.get(key); // --> 123 | ||
``` | ||
A regular `Object` used as a map would yield `321` | ||
@@ -93,19 +103,23 @@ | ||
map.set(1, "test 1"); | ||
map.set(2, "test 2"); | ||
map.set(3, "test 3"); | ||
map.forEach(function(value, key) { | ||
console.log(key + " : " + value); | ||
}); | ||
```js | ||
map.set(1, "test 1"); | ||
map.set(2, "test 2"); | ||
map.set(3, "test 3"); | ||
map.forEach(function(value, key) { | ||
console.log(key + " : " + value); | ||
}); | ||
``` | ||
### Method chaining | ||
map | ||
.set(1, "test 1") | ||
.set(2, "test 2") | ||
.set(3, "test 3") | ||
.forEach(function(value, key) { | ||
console.log(key + " : " + value); | ||
}); | ||
```js | ||
map | ||
.set(1, "test 1") | ||
.set(2, "test 2") | ||
.set(3, "test 3") | ||
.forEach(function(value, key) { | ||
console.log(key + " : " + value); | ||
}); | ||
``` | ||
@@ -135,5 +149,5 @@ ## LICENSE | ||
## TODO's | ||
## To-Do | ||
* (?) Allow extending the hashing function in a AOP way or by passing a service | ||
* Make tests work on the browser |
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
12152
196
149
3