utilities for working with hases i.e. {}
Filters out key/value pairs in an object. Filters out key/value pairs that return a falsey value from the iterator.
var obj = {a : "b", c : "d", e : "f"};
comb(obj).filter(function(value, key){
return value == "b" || key === "e";
}); //{a : "b", e : "f"};
comb.hash.filter(obj, function(){
return value == "b" || key === "e";
}); //{a : "b", e : "f"};
Arguments
the hash to filter.
the interator function. Called with (key, value, hash).
hash] : the scope to invoke the interator in.
Object a new object with the values that returned true..
function (hash,iterator,scope){
if (!isHash(hash) || typeof iterator !== "function") {
throw new TypeError();
}
var keys = Object.keys(hash), key, value, ret = {};
for (var i = 0, len = keys.length; i < len; ++i) {
key = keys[i];
value = hash[key];
if (iterator.call(scope || hash, value, key, hash)) {
ret[key] = value;
}
}
return ret;
}
Loops through each k/v in a hash.
var obj = {a : "b", c : "d", e : "f"};
comb(obj).forEach(function(value, key){
console.log(value, key);
});
comb.hash.forEach(obj, function(){
console.log(value, key);
});
Arguments
the hash to iterate
the interator function. Called with (key, value, hash).
hash] : the scope to invoke the interator in.
Object the original hash.
function (hash,iterator,scope){
if (!isHash(hash) || typeof iterator !== "function") {
throw new TypeError();
}
var keys = Object.keys(hash), key;
for (var i = 0, len = keys.length; i < len; ++i) {
key = keys[i];
iterator.call(scope || hash, hash[key], key, hash);
}
return hash;
}
Returns a new hash that is the invert of the hash.
var obj = {a : "b", c : "d", e : "f"};
comb(obj).invert(); //{b : "a", d : "c", f : "e"}
comb.hash.invert(obj); //{b : "a", d : "c", f : "e"}
Arguments
the hash to invert.
Object A new hash that is the invert of hash.
function (hash){
if (!isHash(hash)) {
throw new TypeError();
}
var keys = Object.keys(hash), key, ret = {};
for (var i = 0, len = keys.length; i < len; ++i) {
key = keys[i];
ret[hash[key]] = key;
}
return ret;
}
Returns a new hash with the specified keys omitted from the hash.
var obj = {a: "a", b: "b", c: "c"};
//as a function
comb.hash.omit(obj, ["a", "b"]); //{c: "c"}
comb.hash.omit(obj, "c"); //{a: "a", b: "b"};
//as a monad
comb(obj).omit(["a", "b"]); //{c: "c"}
comb(obj).omit("c"); //{a: "a", b: "b"};
Arguments
The hash to omit values from
the keys to omit.
Object a new Object with the keys omitted
function (hash,omitted){
if (!isHash(hash)) {
throw new TypeError("expected object got ", pToString.call(hash));
}
if (baseString().isString(omitted)) {
omitted = [omitted];
}
var objKeys = baseArray().difference(Object.keys(hash), omitted), key, ret = {};
for (var i = 0, len = objKeys.length; i < len; ++i) {
key = objKeys[i];
ret[key] = hash[key];
}
return ret;
}
Returns a new hash with the specified keys omitted from the hash.
var obj = {a: "a", b: "b", c: "c"};
//as a function
comb.hash.pick(obj, ["a", "b"]); //{a: "a", b:'b'}
comb.hash.pick(obj, "c"); //{c: "c"};
//as a monad
comb(obj).pick(["a", "b"]); //{a: "a", b:'b'}
comb(obj).pick("c"); //{c: "c"};
Arguments
The hash to pick values from
the keys to pick.
Object a new Object with only the keys specified
function (hash,picked){
if (!isHash(hash)) {
throw new TypeError("expected object got ", pToString.call(hash));
}
if (baseString().isString(picked)) {
picked = [picked];
}
var objKeys = baseArray().intersect(Object.keys(hash), picked), key, ret = {};
for (var i = 0, len = objKeys.length; i < len; ++i) {
key = objKeys[i];
ret[key] = hash[key];
}
return ret;
}
Converts a hash to an array.
var obj = {a : "b", c : "d", e : "f"};
comb(obj).toArray(); //[["a", "b"], ["c", "d"], ["e", "f"]]
comb.hash.toArray(obj); //[["a", "b"], ["c", "d"], ["e", "f"]]
Arguments
the hash to convert to an array.
Array a two dimensional array representing the hash.
function (hash){
if (!isHash(hash)) {
throw new TypeError();
}
var keys = Object.keys(hash), key, ret = [];
for (var i = 0, len = keys.length; i < len; ++i) {
key = keys[i];
ret.push([key, hash[key]]);
}
return ret;
}
Returns the values of a hash.
var obj = {a : "b", c : "d", e : "f"};
comb(obj).values(); //["b", "d", "f"]
comb.hash.values(obj); //["b", "d", "f"]
Arguments
the object to retrieve the values of.
Array array of values.
function (hash){
if (!isHash(hash)) {
throw new TypeError();
}
var keys = Object.keys(hash), ret = [];
for (var i = 0, len = keys.length; i < len; ++i) {
ret.push(hash[keys[i]]);
}
return ret;
}
MIT https://github.com/C2FO/comb/raw/master/LICENSE
git clone git://github.com/C2FO/comb.git