Skip to content

Commit

Permalink
compact and size for Hash (can't do .length since the function protot…
Browse files Browse the repository at this point in the history
…ype has that)
  • Loading branch information
James Halliday committed Sep 9, 2010
1 parent a962ed8 commit b9e6db5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ function Hash (hash, xs) {
return Object.keys(hash).length;
} });

Object.defineProperty(self, 'size', { get : function () {
return self.length;
} });

return self;
};

Expand Down Expand Up @@ -183,3 +187,12 @@ Hash.concat = function (xs) {
Hash.zip = function (xs, ys) {
return Hash(xs, ys).items;
};

// .length is already defined for function prototypes
Hash.size = function (ref) {
return Hash(ref).size;
};

Hash.compact = function (ref) {
return Hash(ref).compact.items;
};
8 changes: 8 additions & 0 deletions test/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ exports['filter and values'] = function (assert) {
assert.equal(Hash(ref).length, 4);
};

exports.length = function (assert) {
assert.equal(Hash({ a : 1, b : [2,3], c : 4 }).length, 3);
assert.equal(Hash({ a : 1, b : [2,3], c : 4 }).size, 3);
assert.equal(Hash.size({ a : 1, b : [2,3], c : 4 }), 3);
};

exports.concat = function (assert) {
var ref1 = { a : 1, b : 2 };
var ref2 = { foo : 100, bar : 200 };
Expand Down Expand Up @@ -122,6 +128,8 @@ exports.compact = function (assert) {
f : null
}
);
var h = Hash.compact(hash);
assert.deepEqual(h, compacted.items);
};

exports.valuesAt = function (assert) {
Expand Down

0 comments on commit b9e6db5

Please sign in to comment.