Skip to content

Commit

Permalink
test for compact passes
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Sep 9, 2010
1 parent 0d8e1e6 commit ec171ba
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function Hash (hash, extra) {
} });

Object.defineProperty(self, 'compact', { get : function () {
return Hash(hash).filter(function (x) { return x !== undefined });
return self.filter(function (x) { return x !== undefined });
} });

Object.defineProperty(self, 'clone', { get : function () {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "traverse",
"version" : "0.1.3",
"version" : "0.1.4",
"description" : "Traverse and transform objects by visiting every node on a recursive walk.",
"author" : "James Halliday",
"license" : "MIT/X11",
Expand Down
19 changes: 19 additions & 0 deletions test/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,22 @@ exports.extract = function (assert) {
assert.equal(hash.valuesAt(['a','b']).join(' '), '1 2');
};

exports.compact = function (assert) {
var hash = {
a : 1,
b : undefined,
c : false,
d : 4,
e : [ undefined, 4 ],
f : null
};
var ins = sys.inspect(hash);
var compacted = Hash(hash).compact;
assert.equal(sys.inspect(hash), ins, 'compact modified the hash');
assert.equal(compacted.keys.sort().join(' '), 'a c d e f');
assert.equal(compacted.items.a, hash.a);
assert.equal(compacted.items.c, hash.c);
assert.equal(compacted.items.d, hash.d);
assert.equal(compacted.items.e.join(' '), hash.e.join(' '));
assert.equal(compacted.items.f, hash.f);
};

0 comments on commit ec171ba

Please sign in to comment.