Skip to content

Commit

Permalink
.has with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Sep 9, 2010
1 parent 3487771 commit 02727cf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ function Hash (hash, xs) {
merge : function (h) {
return self.copy.update(h);
},
has : function (key) { // only operates on enumerables
return Array.isArray(key)
? key.every(function (k) { return self.has(k) })
: self.keys.indexOf(key.toString()) >= 0;
},
valuesAt : function (keys) {
return Array.isArray(keys)
? keys.map(function (key) { return hash[key] })
Expand Down Expand Up @@ -153,6 +158,10 @@ Hash.merge = function (a, b) {
return Hash(a).merge(b).items;
};

Hash.has = function (ref, key) {
return Hash(ref).has(key);
};

Hash.valuesAt = function (ref, keys) {
return Hash(ref).valuesAt(keys);
};
Expand Down
15 changes: 15 additions & 0 deletions test/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,18 @@ exports.zip = function (assert) {
assert.equal(zipped.b, 2);
assert.equal(zipped.c, 3);
};

exports.has = function (assert) {
var h = { a : 4, b : 5 };
var hh = Hash(h);

assert.ok(hh.has('a'));
assert.equal(hh.has('c'), false);
assert.ok(hh.has(['a','b']));
assert.equal(hh.has(['a','b','c']), false);

assert.ok(Hash.has(h, 'a'));
assert.equal(Hash.has(h, 'c'), false);
assert.ok(Hash.has(h, ['a','b']));
assert.equal(Hash.has(h, ['a','b','c']), false);
};

0 comments on commit 02727cf

Please sign in to comment.