Skip to content

Commit a9b8a49

Browse files
committed
add es6-map compatibility layer
- also cleaned up the formatting for my personal tastes
1 parent b132584 commit a9b8a49

File tree

8 files changed

+355
-176
lines changed

8 files changed

+355
-176
lines changed

README.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
HashTable
22
=========
33

4-
[<img src="https://travis-ci.org/isaacbw/node-hashtable.png?branch=master" />](https://travis-ci.org/isaacbw/node-hashtable)
5-
64
Sometimes you need to store so much data in memory that V8 can get a bit clogged up. This Node.js module provides an interface to a native hashmap data structure that exists outside of V8's memory constraints.
75

86
To install, simply:
@@ -54,9 +52,9 @@ Removes all key/value pairs from the hash table.
5452

5553
Returns the number of key/value pairs in the hash table.
5654

57-
### `forEach ( cb )`
55+
### `forEach ( cb, context )`
5856

59-
`cb` is an iterator function that will be called with each key/value pair like `cb(value, key)`.
57+
`cb` is an iterator function that will be called with each key/value pair like `cb.call(c, key, value)`, if context is not provided, the global.
6058

6159
### `keys ()`
6260
Will return an array of the keys stored in the hashtable.
@@ -72,3 +70,25 @@ Gives a hint to the implementation which may cause a rehash to the most appropri
7270
### `max_load_factor ()` or `max_load_factor ( factor )`
7371

7472
Either returns or sets the max load factor of the hash table implementation. This value determines when the hash map is rehashed with a new bucket count. By default it is `1.0`. See [unordered_map#max_load_factor](http://www.cplusplus.com/reference/unordered_map/unordered_map/max_load_factor/)
73+
74+
75+
"But Chad, what if I want a super fast version of ES6's Map? Isn't this really close?"
76+
---
77+
78+
You're right anonymous internet user! Just install HashTable like above, but then use like this:
79+
80+
var Map = require('hashtable/es6-map');
81+
82+
var map = new Map();
83+
map.set('key', {value: 'value'});
84+
map.set('something', 'else');
85+
86+
console.log('There are', map.size, 'item(s) in the map');
87+
88+
iterator = map.entries();
89+
while (!iterator.done) {
90+
console.log(iterator.key, '=', iterator.value);
91+
iterator.next();
92+
}
93+
94+
See the official []ES6 Map documentation](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-map-objects)

es6-iterator.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'use strict';
2+
3+
var ES6Iterator = function (native_map, type) {
4+
var keys = [];
5+
var values = [];
6+
native_map.forEach(function (key, value) {
7+
keys.push(key);
8+
values.push(value);
9+
});
10+
11+
var idx = 0;
12+
var iterator = Object.defineProperties({}, {
13+
done: {
14+
get: function () {
15+
return idx >= keys.length;
16+
},
17+
enumerable: true
18+
},
19+
next: {
20+
value: function () {
21+
idx += 1;
22+
return this;
23+
},
24+
enumerable: true
25+
}
26+
});
27+
28+
if (type !== 'key' && type !== 'value') {
29+
type = 'key+value';
30+
}
31+
32+
switch (type) {
33+
case 'key':
34+
Object.defineProperty(iterator, 'key', {
35+
get: function () {
36+
if (this.done) {
37+
return undefined;
38+
}
39+
return keys[idx];
40+
},
41+
enumerable: true
42+
});
43+
break;
44+
case 'value':
45+
Object.defineProperty(iterator, 'value', {
46+
get: function () {
47+
if (this.done) {
48+
return undefined;
49+
}
50+
return values[idx];
51+
},
52+
enumerable: true
53+
});
54+
break;
55+
case 'key+value': // default
56+
Object.defineProperties(iterator, {
57+
key: {
58+
get: function () {
59+
if (this.done) {
60+
return undefined;
61+
}
62+
return keys[idx];
63+
},
64+
enumerable: true
65+
},
66+
value: {
67+
get: function () {
68+
if (this.done) {
69+
return undefined;
70+
}
71+
return values[idx];
72+
},
73+
enumerable: true
74+
}
75+
});
76+
break;
77+
}
78+
79+
return iterator;
80+
};
81+
82+
module.exports = ES6Iterator;

es6-map.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
var HashTable = require('./');
4+
var makeIterator = require('./es6-iterator');
5+
6+
var ES6Map = function (iterator) {
7+
this._native_map = new HashTable();
8+
if (iterator && iterator.hasOwnProperty('done') && iterator.hasOwnProperty('next') && iterator.hasOwnProperty('key') && iterator.hasOwnProperty('value')) {
9+
while (!iterator.done) {
10+
this._native_map.put(iterator.key, iterator.value);
11+
iterator.next();
12+
}
13+
}
14+
};
15+
16+
ES6Map.prototype = {
17+
get: function (key) {
18+
return this._native_map.get(key);
19+
},
20+
has: function (key) {
21+
return this._native_map.get(key) !== undefined;
22+
},
23+
set: function (key, value) {
24+
return this._native_map.put(key, value);
25+
},
26+
delete: function (key) {
27+
return this._native_map.remove(key);
28+
},
29+
forEach: function (cb, ctx) {
30+
return this._native_map.forEach(function (key, value) {
31+
return cb.call(ctx, value, key, this);
32+
}, this);
33+
},
34+
clear: function () {
35+
return this._native_map.clear();
36+
},
37+
keys: function () {
38+
return makeIterator(this._native_map, 'key');
39+
},
40+
values: function () {
41+
return makeIterator(this._native_map, 'value');
42+
},
43+
entries: function () {
44+
return makeIterator(this._native_map, 'key+value');
45+
}
46+
};
47+
48+
Object.defineProperty(ES6Map.prototype, 'size', {
49+
get: function () {
50+
return this._native_map.size();
51+
},
52+
enumerable: true,
53+
configurable: true
54+
});
55+
56+
module.exports = ES6Map;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
22
"name": "hashtable",
3-
"version": "0.1.5",
3+
"version": "0.2.1",
44
"description": "Native hashtables for Node.js",
55
"main": "./index.js",
66
"keywords": [
7+
"es6-map",
8+
"es6",
79
"hash",
810
"map",
911
"hashmap",

0 commit comments

Comments
 (0)