-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James Halliday
committed
Aug 27, 2010
1 parent
1adf75a
commit 586124c
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module.exports = Hash; | ||
var Traverse = require('traverse'); | ||
|
||
function Hash (ref) { | ||
var hash = Traverse.clone(ref); | ||
return { | ||
map : function (f) { | ||
var acc = { __proto__ : hash.__proto__ }; | ||
Object.keys(hash).forEach(function (key) { | ||
acc[key] = f.call(hash, hash[key], key); | ||
}); | ||
return Hash(acc); | ||
}, | ||
forEach : function (f) { | ||
var acc = { __proto__ : hash.__proto__ }; | ||
Object.keys(hash).forEach(function (key) { | ||
f.call(hash, hash[key], key); | ||
}); | ||
return Hash(hash); | ||
}, | ||
filter : function (f) { | ||
var acc = { __proto__ : hash.__proto__ }; | ||
Object.keys(hash).forEach(function (key) { | ||
if (f.call(hash, hash[key], key)) { | ||
acc[key] = hash[key]; | ||
} | ||
}); | ||
return Hash(acc); | ||
}, | ||
reduce : function (f, acc) { | ||
var keys = Object.keys(hash); | ||
if (acc === undefined) acc = keys.shift(); | ||
keys.forEach(function (key) { | ||
acc = f.call(hash, acc, hash[key], key); | ||
}); | ||
return acc; | ||
}, | ||
end : hash, | ||
}; | ||
}; | ||
|
||
Hash.map = function (ref, f) { | ||
return Hash(ref).map(f).end; | ||
}; | ||
|
||
Hash.filter = function (ref, f) { | ||
return Hash(ref).filter(f).end; | ||
}; | ||
|
||
Hash.reduce = function (ref, f, acc) { | ||
return Hash(ref).reduce(f, acc); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,3 +95,8 @@ function Traverse (refObj) { | |
return acc; | ||
}; | ||
} | ||
|
||
Traverse.clone = function (obj) { | ||
return Traverse(obj).get(); | ||
}; | ||
|