Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Document MultiHash.

  • Loading branch information...
commit 66fddb8edaf1c6b0fd9f8664f6dbf2c54237f3f0 1 parent 29af157
@jaredhanson authored
Showing with 51 additions and 6 deletions.
  1. +45 −0 lib/multihash.js
  2. +6 −6 test/multihash-test.js
View
45 lib/multihash.js
@@ -1,16 +1,45 @@
+/**
+ * `MultiHash` constructor.
+ *
+ * @api private
+ */
function MultiHash() {
this._hash = {};
this.__defineGetter__('length', this._length);
}
+/**
+ * Test if `key` is set.
+ *
+ * @param {String} key
+ * @return {Boolean} _true_ if set, _false_ otherwise
+ * @api private
+ */
MultiHash.prototype.has = function(key) {
return (this._hash[key] !== undefined);
}
+/**
+ * Number of values set for `key`.
+ *
+ * @param {String} key
+ * @return {Number}
+ * @api private
+ */
MultiHash.prototype.count = function(key) {
return this.has(key) ? this._hash[key].length : 0;
}
+/**
+ * Put `value` for `key`.
+ *
+ * Multi-hashes can contain multiple values for the same key. Putting a value
+ * to a key will add a value, rather than replace an existing value.
+ *
+ * @param {String} key
+ * @param {Mixed} value
+ * @api private
+ */
MultiHash.prototype.put = function(key, value) {
if (this.has(key)) {
this._hash[key].push(value);
@@ -19,6 +48,16 @@ MultiHash.prototype.put = function(key, value) {
}
}
+/**
+ * Executes provided `fn` once per key-value pair.
+ *
+ * Options:
+ * - `flatten` iterate over a single (most recent) value per key, defaults to _false_
+ *
+ * @param {Function} fn
+ * @param {Object} options
+ * @api private
+ */
MultiHash.prototype.forEach = function(fn, options) {
options = options || {};
@@ -35,6 +74,12 @@ MultiHash.prototype.forEach = function(fn, options) {
});
}
+/**
+ * Number of keys in the multi-hash.
+ *
+ * @return {Number}
+ * @api private
+ */
MultiHash.prototype._length = function() {
return Object.keys(this._hash).length;
}
View
12 test/multihash-test.js
@@ -6,7 +6,7 @@ var MultiHash = require('multihash');
vows.describe('MultiHash').addBatch({
- 'multihash that is empty': {
+ 'multihash with no elements': {
topic: function() {
return new MultiHash();
},
@@ -17,7 +17,7 @@ vows.describe('MultiHash').addBatch({
'should not have key' : function(hash) {
assert.isFalse(hash.has('something'));
},
- 'should have zero count for key' : function(hash) {
+ 'should report count of zero values for key' : function(hash) {
assert.equal(hash.count('something'), 0);
},
'should not yield any key-value pairs when iterating' : function(hash) {
@@ -30,7 +30,7 @@ vows.describe('MultiHash').addBatch({
},
},
- 'multihash with single-value elements': {
+ 'multihash with two single-value elements': {
topic: function() {
var hash = new MultiHash();
hash.put('hello', 'world');
@@ -45,7 +45,7 @@ vows.describe('MultiHash').addBatch({
assert.isTrue(hash.has('hello'));
assert.isTrue(hash.has('foo'));
},
- 'should have one count for keys' : function(hash) {
+ 'should report count of one value for each key' : function(hash) {
assert.equal(hash.count('hello'), 1);
assert.equal(hash.count('foo'), 1);
},
@@ -63,7 +63,7 @@ vows.describe('MultiHash').addBatch({
},
},
- 'multihash with multi-value element': {
+ 'multihash with one multi-value element': {
topic: function() {
var hash = new MultiHash();
hash.put('foo', 'bar');
@@ -77,7 +77,7 @@ vows.describe('MultiHash').addBatch({
'should have key' : function(hash) {
assert.isTrue(hash.has('foo'));
},
- 'should have two count for key' : function(hash) {
+ 'should report count of two values for key' : function(hash) {
assert.equal(hash.count('foo'), 2);
},
'should yield key-value pairs when iterating' : function(hash) {
Please sign in to comment.
Something went wrong with that request. Please try again.