-
Notifications
You must be signed in to change notification settings - Fork 0
/
superhash.js
190 lines (166 loc) · 4.38 KB
/
superhash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var mkhash = require('multikey-hash');
/**
* Creates a new SuperHash
*
* Example `entries` Array
*
* ```
* [[1,2,3],'foo'], [[{foo: 'bar', {blip:'blop'}],'bar']]
* ```
*
* @class
* @param {Array} entries two dimensional array with entries to prefill the map. Keys must be an array (even if just one)
* @api public
*/
function SuperHash(entries) {
var args;
this.store = {};
this.size = 0;
if (Object.defineProperty) {
Object.defineProperty(this, 'store', {enumerable:false});
}
if (!entries) {
return;
}
for (var i = 0; i < entries.length; i++) {
args = entries[i][0];
args.push(entries[i][1]);
this.set.apply(this, args);
}
}
/**
* Creates a hash from the keys if it doesn't exist and sets the last argument passed in as the value
*
* @param {...*} keys Used to generate hash
* @param {*} value to be associated with the key
* @return {Number} the hash associated with the keys
* @api public
*/
SuperHash.prototype.set = function set() {
var len = arguments.length;
var value = arguments[len-1];
var keys = new Array(len-1);
var hash;
for (var i = 0; i < len-1; i++) {
keys[i] = arguments[i];
}
hash = mkhash.apply(this, keys);
if (!this.store.hasOwnProperty(hash)) {
this.size++;
}
this.store[hash] = [keys, value];
return this;
};
/**
* Returns the value associated with the hash generated from the keys
*
* @param {...*} keys Used to generate a hash for lookup
* @return {Object} value associated with generated hash
* @api public
*/
SuperHash.prototype.get = function get() {
var hash = mkhash.apply(this, arguments);
return this.store[hash] ? this.store[hash][1] : undefined;
};
/**
* Tells whether or not value associated with the hash generated from the keys is in the map
*
* @param {...*} keys - Used to generate a hash for lookup
* @return {Boolean} true if generated hash is in the map
* @api public
*/
SuperHash.prototype.has = function has() {
var key = mkhash.apply(this, arguments);
return key in this.store;
};
/**
* Returns all keys from the hash map
* @return {Array} keys from the hash map
* @api public
*/
SuperHash.prototype.keys = function keys() {
return _getEntries(this.store, 0);
};
/**
* Returns all entries (key/value pairs) from the hash map
* @return {Array} entries from the hash map
* @api public
*/
SuperHash.prototype.entries = function entries() {
return _getEntries(this.store);
};
/**
* Returns all values from the hash map
* @return {Array} values from the hash map
* @api public
*/
SuperHash.prototype.values = function values() {
return _getEntries(this.store, 1);
};
/**
* Loops through each value in the hashmap passing it as the first argument in callack
*
* ```js
* hashMap.forEach(function(value){
*
* });
* ```
*
* @param {Function} cb callback function called with `(key, value)` for each entry in the map
* @param {*} context `this` context for the callback
* @api public
*/
SuperHash.prototype.forEach = function forEach(cb, thisArg) {
thisArg = thisArg || this;
for (var hash in this.store) {
if (this.store.hasOwnProperty(hash)) {
cb.call(thisArg, this.store[hash][1], this.store[hash][0]);
}
}
};
/**
* Removes the hash generated by the keys and the associated value
*
* @param {...*} keys - Used to generate a hash for lookup
* @return {Boolean} whether the hash existed or not
* @api public
*/
SuperHash.prototype.delete = function() {
var key = mkhash.apply(this, arguments);
if (!this.store.hasOwnProperty(key)) {
return false;
}
delete this.store[key];
this.size--;
return true;
};
/**
* Deletes all keys and values from hash map
* @api public
*/
SuperHash.prototype.clear = function clear() {
this.store = {};
this.size = 0;
};
/**
* Returns all keys, values, or entries from hash map depending on entryIndex
* @param {Object} store object of all hash/values
* @param {Number} entryIndex 0=key, 1=value, undefined=entire entry
* @return {Array} values from store
* @api private
*/
function _getEntries(store, entryIndex) {
var results = [];
var value;
var entry;
for (var hash in store) {
if (!store.hasOwnProperty(hash)){
continue;
}
entry = store[hash];
value = entryIndex === undefined ? entry : entry[entryIndex];
results.push(value);
}
return results;
}
module.exports = SuperHash;