Skip to content

Commit

Permalink
test for valuesAt and now takes a single non-array key too
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Sep 9, 2010
1 parent 35298be commit 3487771
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ function Hash (hash, xs) {
return self.copy.update(h);
},
valuesAt : function (keys) {
return keys.map(function (key) { return hash[key] });
return Array.isArray(keys)
? keys.map(function (key) { return hash[key] })
: hash[keys]
;
},
tap : function (f) {
f.call(self, hash);
Expand Down
17 changes: 16 additions & 1 deletion test/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ exports.compact = function (assert) {
assert.equal(compacted.items.f, hash.f);
};

exports.valuesAt = function (assert) {
var h = { a : 4, b : 5, c : 6 };
assert.equal(Hash(h).valuesAt('a'), 4);
assert.equal(Hash(h).valuesAt(['a'])[0], 4);
assert.equal(Hash(h).valuesAt(['a','b']).join(' '), '4 5');
assert.equal(Hash.valuesAt(h, 'a'), 4);
assert.equal(Hash.valuesAt(h, ['a'])[0], 4);
assert.equal(Hash.valuesAt(h, ['a','b']).join(' '), '4 5');
};

exports.zip = function (assert) {
var xs = ['a','b','c'];
var ys = [1,2,3,4];
Expand All @@ -126,5 +136,10 @@ exports.zip = function (assert) {
assert.equal(h.items.c, 3);
assert.equal(ys.join(' '), '1 2 3 4');
h.items.b += 10;
assert.equal(h.valuesAt(['b'])[0], 12);
assert.equal(h.valuesAt('b'), 12);

var zipped = Hash.zip(xs,ys);
assert.equal(zipped.a, 1);
assert.equal(zipped.b, 2);
assert.equal(zipped.c, 3);
};

0 comments on commit 3487771

Please sign in to comment.