Defined base/object.js

utilities for working with hases i.e. {}

filter Function Public


Defined base/object.js

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 Returns Source
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;
}
    

forEach Function Public


Defined base/object.js

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 Returns Source
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;
}
    

invert Function Public


Defined base/object.js

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 Returns Source
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;
}
    

omit Function Public


Defined base/object.js

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 Returns Source
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;
}
    

pick Function Public


Defined base/object.js

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 Returns Source
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;
}
    

toArray Function Public


Defined base/object.js

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 Returns Source
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;
}
    

values Function Public


Defined base/object.js

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 Returns Source
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;
}
    

License

MIT https://github.com/C2FO/comb/raw/master/LICENSE

Meta