Defined index.js

Utilities for javascript, optimized for the server environment.

PropertyTypeDefault ValueDescription
bindAllpropertyhitchAll

Binds all methods or a specified array of function to the scope of the object

definepropertydefine

Defines a new class to be used

Class methods

  • as(module | object, name): exports the object to module or the object with the name
  • mixin(mixin) : mixes in an object

Instance methods
  • _super(argumnents, [?newargs]): calls the super of the current method

Instance properties
  • _static: use to reference class properties and methods

singletonpropertysingleton

Defines a singleton instance of a Class. See define

applyFirst Static Function Public


Defined base/functions.js

Binds a method to the scope of the first argument.

This is useful if you have async actions and you just want to run a method or retrieve a property on the object.

 var arr = [], push = comb.applyFirst("push"), length = comb.applyFirst("length");
 push(arr, 1, 2,3,4);
 console.log(length(arr)); //4
 console.log(arr); //1,2,3,4

Arguments Returns Source
function (method,args){
   args = Array.prototype.slice.call(arguments).slice(1);
   if (!isString(method) && !isFunction(method)) {
       throw new Error(method + " must be the name of a property or function to execute");
   }
   if (isString(method)) {
       return function () {
           var scopeArgs = Array.prototype.slice.call(arguments), scope = scopeArgs.shift();
           var func = scope[method];
           if (isFunction(func)) {
               scopeArgs = args.concat(scopeArgs);
               return spreadArgs(func, scopeArgs, scope);
           } else {
               return func;
           }
       };
   } else {
       return function () {
           var scopeArgs = Array.prototype.slice.call(arguments), scope = scopeArgs.shift();
           scopeArgs = args.concat(scopeArgs);
           return spreadArgs(method, scopeArgs, scope);
       };
   }
}
    

argsToArray Static Function Public


Defined base/misc.js

Converts an arguments object to an array

Example
function test(){
    return comb.argsToArray(arguments);
}

function testSlice(){
    return comb.argsToArray(arguments, 3);
}

console.log(test(1,2,3)); //[1,2,3]
console.log(test(1,2,3,4,5,6)); //[4,5,6]
        
Arguments Returns Source
function (args,slice){
   slice = slice || 0;
   return arraySlice.call(args, slice);
}
    

bind Static Function Public


Defined base/functions.js

Arguments Returns Source
hitch
    

bindIgnore Static Function Public


Defined base/functions.js

Arguments Returns Source
hitchIgnore
    

broadcast Static Function Public


Defined base/broadcast.js

Broadcasts an event to all listeners NOTE : the function takes a variable number of arguments i.e. all arguments after the topic will be passed to the listeners

Example
comb.broadcast("hello", "hello world");
//the args "hello" and "world" will be passed to any listener of the topic
//"hello"
comb.broadcast("hello", "hello", "world");
        
Arguments Source
function (){
   var args = Array.prototype.slice.call(arguments);
   var topic = args.splice(0, 1)[0];
   if (topic) {
       var list = listeners[topic];
       if (list) {
           for (var i = list.length - 1; i >= 0; i--) {
               var han = list[i], cb = han.cb;
               if (cb) {
                   cb.apply(this, args);
               }
           }
       }
   }
       
}
    

camelize Static Function Public


Defined base/inflections.js

Converts a string to camelcase

Example
comb.camelize('hello_world') => helloWorld
 comb.camelize('column_name') => columnName
 comb.camelize('columnName') => columnName
 comb.camelize(null) => null
 comb.camelize() => undefined
        
Arguments Returns Source
function (str){
   var ret = str;
   if (!misc.isUndefinedOrNull(str)) {
       ret = str.replace(CAMELIZE_CONVERT_REGEXP, function (a, b) {
           return b.toUpperCase();
       });
   }
   return ret;
}
    

chain Static Function Public


Defined promise.js

Works just like comb.Promise#chain method, allowing you to propogate results from one funciton to another. This is different than comb.serial in that it propogates results from one promise to the next, where comb.serial does not.

Example
function asyncAction(add, timeout) {
     return function (num) {
         num = num || 0;
         var ret = new comb.Promise();
         setTimeout(function () {
              ret.callback(num + add);
         }, timeout);
         return ret;
     }
}

comb.chain([
     asyncAction(1, 100),
     asyncAction(2, 100),
     asyncAction(3, 100),
     asyncAction(4, 100),
     asyncAction(5, 100),
]).then(function(results){
     console.log(results); //15
});
        
Arguments Returns Source
function (list){
   if (base.isArray(list)) {
       return callNext(list, [], true);
   } else {
       throw new Error("When calling comb.serial the first argument must be an array");
   }
}
    

classify Static Function Public


Defined base/inflections.js

Singularizes and camelizes the string. Also strips out all characters preceding and including a period (".").

Example
comb.classify('egg_and_hams') => "eggAndHam"
  comb.classify('post') => "post"
  comb.classify('schema.post') => "post"
        
Arguments Returns Source
function (str){
   var ret = str;
   if (!misc.isUndefinedOrNull(str)) {
       ret = comb.camelize(comb.singularize(str.replace(/.*\./g, '')));
   }
   return ret;
}
    

connect Static Function Public


Defined base/broadcast.js

Function to listen when other functions are called

Example
comb.connect(obj, "event", myfunc);
 comb.connect(obj, "event", "log", console);
        
Arguments Returns Source
function (obj,method,cb,scope){
   var index,
       listeners;
   if (typeof method !== "string") {
       throw new Error("When calling connect the method must be string");
   }
   if (!func.isFunction(cb)) {
       throw new Error("When calling connect callback must be a string");
   }
   scope = obj || global;
   if (typeof scope[method] === "function") {
       listeners = scope[method].__listeners;
       if (!listeners) {
           var newMethod = wrapper();
           newMethod.func = obj[method];
           listeners = (newMethod.__listeners = []);
           scope[method] = newMethod;
       }
       index = listeners.push(cb);
   } else {
       throw new Error("unknow method " + method + " in object " + obj);
   }
   return [obj, method, index];
       
}
    

curry Static Function Public


Defined base/functions.js

Curries a function

Example
var curried = comb.curry(4, function(a,b,c,d){
    return [a,b,c,d].join(",");
}
 curried("a");
 curried("b");
 curried("c");
 curried("d") => "a,b,c,d"

 //OR

 curried("a")("b")("c")("d") => "a,b,c,d"
        
Arguments Returns Source
function (depth,cb,scope){
   var f;
   if (scope) {
       f = hitch(scope, cb);
   } else {
       f = cb;
   }
   if (depth) {
       var len = depth - 1;
       for (var i = len; i >= 0; i--) {
           f = curryFunc(f, i === len);
       }
   }
   return f;
}
    

daysAgo Static Function Public


Defined base/date/index.js

Subtracts the specified day/s from the current date.

Arguments Returns Source
function (val){
   return date.add(new Date(), "days", -val);
}
    

daysFromNow Static Function Public


Defined base/date/index.js

Adds the specified day/s to the current date.

Arguments Returns Source
function (val){
   return date.add(new Date(), "days", val);
}
    

deepEqual Static Function Public


Defined base/object.js

Determines if two things are deep equal.

Example
comb.deepEqual({a : 1, b : 2}, {a : 1, b : 2}) => true
comb.deepEqual({a : 1}, {a : 1, b : 2}) => false
        
Arguments Returns Source
function (o1,o2){
   return _deepEqual(o1, o2);
}
    

deepMerge Static Function Public


Defined base/object.js

Merges objects together only overriding properties that are different. NOTE: this function takes a variable number of objects to merge

Example
var myObj = {my : {cool : {property1 : 1, property2 : 2}}};
comb.deepMerge(myObj, {my : {cool : {property3 : 3}}});

myObj.my.cool.property1 => 1
myObj.my.cool.property2 => 2
myObj.my.cool.property3 => 3
        
Arguments Returns Source
function (obj,props){
   if (!obj) {
       obj = {};
   }
   for (var i = 1, l = arguments.length; i < l; i++) {
       deepMerge(obj, arguments[i]);
   }
   return obj; // Object
}
    

disconnect Static Function Public


Defined base/broadcast.js

Disconnects a listener to a function

Arguments Source
function (handle){
   if (handle && handle.length === 3) {
       var obj = handle[0], method = handle[1], cb = handle[2];
       if (typeof method !== "string") {
           throw "comb.disconnect : When calling disconnect the method must be string";
       }
       var scope = obj || global, ls;
       if (typeof scope[method] === "function") {
           ls = scope[method].__listeners;
           if (ls && cb-- > 0) {
               //we dont want to splice it because our indexing will get off
               ls[cb] = null;
           }
       } else {
           throw new Error("unknown method " + method + " in object " + obj);
       }
   } else {
       throw new Error("comb.disconnect : invalid handle");
   }
       
}
    

extend Static Function Public


Defined base/object.js

Extends the prototype of an object if it exists otherwise it extends the object.

Example
var MyObj = function(){};
 MyObj.prototype.test = true;
 comb.extend(MyObj, {test2 : false, test3 : "hello", test4 : "world"});

 var myObj = new MyObj();

 myObj.test => true
 myObj.test2 => false
 myObj.test3 => "hello"
 myObj.test4 => "world"

 var myObj2 = {};
 myObj2.test = true;
 comb.extend(myObj2, {test2 : false, test3 : "hello", test4 : "world"});

 myObj2.test => true
 myObj2.test2 => false
 myObj2.test3 => "hello"
 myObj2.test4 => "world"
        
Arguments Returns Source
function (parent,ext){
   var proto = parent.prototype || parent;
   merge(proto, ext);
   return parent;
}
    

hitch Static Function Public


Defined base/functions.js

Binds a method to a particular scope

Arguments Returns Source
function (scope,method,args){
   args = Array.prototype.slice.call(arguments).slice(2);
   if ((isString(method) && !(method in scope))) {
       throw new Error(method + " property not defined in scope");
   } else if (!isString(method) && !isFunction(method)) {
       throw new Error(method + " is not a function");
   }
   if (isString(method)) {
       return function () {
           var func = scope[method];
           if (isFunction(func)) {
               var scopeArgs = args.concat(Array.prototype.slice.call(arguments));
               return spreadArgs(func, scopeArgs, scope);
           } else {
               return func;
           }
       };
   } else {
       return function () {
           var scopeArgs = args.concat(Array.prototype.slice.call(arguments));
           return spreadArgs(method, scopeArgs, scope);
       };
   }
}
    

hitchAll Static Function Public


Defined base/functions.js

Binds all methods or a specified array of function to the scope of the object

Example
var f = {
    a: function(){
        return "a";
    },
    b: function(){
        return "b";
    }
}

comb.hitchAll(f, "a", "b");

//or

comb.hitchAll(f);
        
Arguments Returns Source
function (scope){
   var funcs = Array.prototype.slice.call(arguments, 1);
   if (!isObject(scope) && !isFunction(scope)) {
       throw new TypeError("scope must be an object");
   }
   if (funcs.length === 1 && isArray(funcs[0])) {
       funcs = funcs[0];
   }
   if (!funcs.length) {
       funcs = [];
       for (var k in scope) {
           if (scope.hasOwnProperty(k) && isFunction(scope[k])) {
               funcs.push(k);
           }
       }
   }
   for (var i = 0, l = funcs.length; i < l; i++) {
       scope[funcs[i]] = hitch(scope, scope[funcs[i]]);
   }
   return scope;
}
    

hitchIgnore Static Function Public


Defined base/functions.js

Binds a method to a particular scope ignoring any new arguments passed into the function. This is useful if you want to force particular arguments and ignore any new ones

Arguments Returns Source
function (scope,method,args){
   args = Array.prototype.slice.call(arguments).slice(2);
   if ((isString(method) && !(method in scope))) {
       throw new Error(method + " property not defined in scope");
   } else if (!isString(method) && !isFunction(method)) {
       throw new Error(method + " is not a function");
   }
   if (isString(method)) {
       return function () {
           var func = scope[method];
           if (isFunction(func)) {
               return spreadArgs(func, args, scope);
           } else {
               return func;
           }
       };
   } else {
       return function () {
           return spreadArgs(method, args, scope);
       };
   }
}
    

hoursAgo Static Function Public


Defined base/date/index.js

Subtracts the specified hour/s from the current date.

Arguments Returns Source
function (val){
   return date.add(new Date(), "hours", -val);
}
    

hoursFromNow Static Function Public


Defined base/date/index.js

Adds the specified hour/s to the current date.

Arguments Returns Source
function (val){
   return date.add(new Date(), "hours", val);
}
    

isArguments Static Function Public


Defined base/misc.js

Determines if obj is an Arguments object;

Arguments Returns Source
function (object){
   return !isUndefinedOrNull(object) && Object.prototype.toString.call(object) === '[object Arguments]';
}
    

isBoolean Static Function Public


Defined base/misc.js

Determines if obj is a boolean

Arguments Returns Source
function (obj){
   var undef, type = typeof obj;
   return obj !== undef && type === "boolean" || type === "Boolean";
}
    

isDate Static Function Public


Defined base/date/index.js

Determines if obj is a Date

Arguments Returns Source
function (obj){
   var undef;
   return (obj !== undef && typeof obj === "object" && obj instanceof Date);
}
    

isDefined Static Function Public


Defined base/misc.js

Determins if the obj is not undefined

Arguments Returns Source
function (obj){
   return !isUndefined(obj);
}
    

isEmpty Static Function Public


Defined base/object.js

Determines if an object is empty

Example
comb.isEmpty({}) => true
comb.isEmpty({a : 1}) => false
        
Arguments Returns Source
function (object){
   if (comb.isObject(object)) {
       for (var i in object) {
           if (object.hasOwnProperty(i)) {
               return false;
           }
       }
   }
   return true;
}
    

isFunction Static Function Public


Defined base/functions.js

Determines if something is a function

Arguments Returns Source
function (obj){
   return typeof obj === "function";
}
    

isHash Static Function Public


Defined base/object.js

Determines if an object is just a hash and not a qualified Object such as Number

Example
comb.isHash({}) => true
   comb.isHash({1 : 2, a : "b"}) => true
   comb.isHash(new Date()) => false
   comb.isHash(new String()) => false
   comb.isHash(new Number()) => false
   comb.isHash(new Boolean()) => false
   comb.isHash() => false
   comb.isHash("") => false
   comb.isHash(1) => false
   comb.isHash(false) => false
   comb.isHash(true) => false
        
Arguments Returns Source
function (obj){
   var ret = comb.isObject(obj);
   return ret && obj.constructor === Object;
}
    

isInstanceOf Static Function Public


Defined base/misc.js

Determines if obj is an instance of a particular class

Arguments Returns Source
function (obj,clazz){
   return argsToArray(arguments, 1).some(function (c) {
       return isInstance(obj, c);
   });
}
    

isNull Static Function Public


Defined base/misc.js

Determines if obj is null

Arguments Returns Source
function (obj){
   var undef;
   return obj !== undef && obj == null;
}
    

isNumber Static Function Public


Defined base/number.js

Determines if obj is a number

Arguments Returns Source
function (obj){
   var undef;
   return obj !== undef && obj != null && (typeof obj === "number" || obj instanceof Number);
}
    

isObject Static Function Public


Defined base/object.js

Determines if obj is an object

Arguments Returns Source
function (obj){
   var undef;
   return obj !== null && obj !== undef && typeof obj === "object";
}
    

isPromiseLike Static Function Public


Defined promise.js

Tests if an object is like a promise (i.e. it contains then, addCallback, addErrback)

Arguments Source
function (obj){
   return isObject(obj, Promise) && typeof obj.then === "function";
}
    

isRegExp Static Function Public


Defined base/regexp.js

Tests if something is a regular expression.

Example
comb.isRegExp(/hello/); //true
comb.isRegExp("hello"); //false
        
Arguments Returns Source
function (obj){
   var undef;
   return obj !== undef && obj != null && (obj instanceof RegExp);
}
    

isString Static Function Public


Defined base/string.js

Tests if something is a string.

Example
comb.isString("true") //true
comb.isString(true) //false
        
Arguments Returns Source
function (obj){
   return toStr.call(obj) === '[object String]';
}
    

isUndefined Static Function Public


Defined base/misc.js

Determines if obj is undefined

Arguments Returns Source
function (obj){
   var undef;
   return obj !== null && obj === undef;
}
    

isUndefinedOrNull Static Function Public


Defined base/misc.js

Determines if obj is undefined or null

Arguments Returns Source
function (obj){
   return isUndefined(obj) || isNull(obj);
}
    

listen Static Function Public


Defined base/broadcast.js

Listen for the broadcast of certain events

Example
comb.listen("hello", function(arg1, arg2){
    console.log(arg1);
    console.log(arg2);
 });
        
Arguments Returns Source
function (topic,callback){
   if (!func.isFunction(callback)) {
       throw new Error("callback must be a function");
   }
   var handle = {
       topic: topic,
       cb: callback,
       pos: null
   };
   var list = listeners[topic];
   if (!list) {
       list = (listeners[topic] = []);
   }
   list.push(handle);
   handle.pos = list.length - 1;
   return handle;
       
}
    

listenForExit Static Function Public


Defined base/misc.js

Adds listeners to process.exit without having to change setMaxListeners useful if you are writing a library and do not want to change core setting.

Arguments Source
function (cb){
   setupListener();
   listeners.push(cb);
       
}
    

merge Static Function Public


Defined base/object.js

Merges objects together NOTE: this function takes a variable number of objects to merge

Example
var myObj = {};
comb.merge(myObj, {test : true});

myObj.test => true

comb.merge(myObj, {test : false}, {test2 : false}, {test3 : "hello", test4 : "world"});
myObj.test => false
myObj.test2 => false
myObj.test3 => "hello"
myObj.test4 => "world"
        
Arguments Returns Source
function (obj,props){
   if (!obj) {
       obj = {};
   }
   for (var i = 1, l = arguments.length; i < l; i++) {
       merge(obj, arguments[i]);
   }
   return obj; // Object
}
    

minutesAgo Static Function Public


Defined base/date/index.js

Subtracts the specified minute/s from the current date.

Arguments Returns Source
function (val){
   return date.add(new Date(), "minutes", -val);
}
    

minutesFromNow Static Function Public


Defined base/date/index.js

Adds the specified minute/s to the current date.

Arguments Returns Source
function (val){
   return date.add(new Date(), "minutes", val);
}
    

monthsAgo Static Function Public


Defined base/date/index.js

Subtracts the specified month/s from the current date.

Arguments Returns Source
function (val){
   return date.add(new Date(), "months", -val);
}
    

monthsFromNow Static Function Public


Defined base/date/index.js

Adds the specified month/s to the current date.

Example
//assuming that current month is february
comb.yearsFromNow(2); //yyyy-04-dd hh:MM:ss
        
Arguments Returns Source
function (val){
   return date.add(new Date(), "months", val);
}
    

partial Static Function Public


Defined base/functions.js

Allows the passing of additional arguments to a function when it is called especially useful for callbacks that you want to provide additional parameters to

Arguments Returns Source
function (method,args){
   args = Array.prototype.slice.call(arguments).slice(1);
   if (!isString(method) && !isFunction(method)) {
       throw new Error(method + " must be the name of a property or function to execute");
   }
   if (isString(method)) {
       return function () {
           var func = this[method];
           if (isFunction(func)) {
               var scopeArgs = args.concat(Array.prototype.slice.call(arguments));
               return spreadArgs(func, scopeArgs, this);
           } else {
               return func;
           }
       };
   } else {
       return function () {
           var scopeArgs = args.concat(Array.prototype.slice.call(arguments));
           return spreadArgs(method, scopeArgs, this);
       };
   }
}
    

pluralize Static Function Public


Defined base/inflections.js

Returns the plural form of the word in the string.

Example
comb.pluralize("post") => "posts"
 comb.pluralize("octopus") => "octopi"
 comb.pluralize("sheep") => "sheep"
 comb.pluralize("words") => "words"
 comb.pluralize("the blue mailman") => "the blue mailmen"
 comb.pluralize("CamelOctopus") => "CamelOctopi"
        
Arguments Returns Source
function (str){
   var ret = str;
   if (!misc.isUndefinedOrNull(str)) {
       if (UNCOUNTABLES.indexOf(str) === -1) {
           for (var i in PLURALS) {
               var s = PLURALS[i], rule = s[0], replacement = s[1];
               if ((ret = ret.replace(rule, replacement)) !== str) {
                   break;
               }
           }
       }
   }
   return ret;
}
    

promisfyStream Static Function Public


Defined promise.js

Wraps a stream in a promise waiting for either the "end" or "error" event to be triggered.

comb.promisfyStream(fs.createdReadStream("my.file")).chain(function(){
     console.log("done reading!");
});

Arguments Returns Source
function (stream){
   var ret = new Promise(), called;
   function errorHandler() {
       if (!called) {
           called = true;
           spreadArgs(ret.errback, arguments, ret);
           stream.removeListener("error", endHandler);
           stream.removeListener("end", endHandler);
           stream = ret = null;
       }
   }
   function endHandler() {
       if (!called) {
           called = true;
           spreadArgs(ret.callback, arguments, ret);
           stream.removeListener("error", endHandler);
           stream.removeListener("end", endHandler);
           stream = ret = null;
       }
   }
   stream.on("error", errorHandler).on("end", endHandler);
   return ret.promise();
}
    

rejected Static Function Public


Defined promise.js

Creates a new comb.Promise that is rejected with the given value.

comb.resolved(new Error("error!")).chain(null, function(err){
     console.error(err.stack);
});

If you give it a promise it will wait for the promise to resolve or error but with reject either way.

comb.resolved(new comb.Promise().callback(1)).chain(null, function(err){
     //err === 1
});

Arguments Returns Source
function (val){
   var ret = new Promise();
   when(val).chain(ret.errback, ret.errback);
   return ret.promise();
}
    

resolved Static Function Public


Defined promise.js

Creates a new comb.Promise that is resolved with the given value.

comb.resolved(1).chain(function(val){
     //val === 1
});

If you give it a promise it will wait for the promise to resolve or error.

comb.resolved(new comb.Promise().callback(1)).chain(function(val){
     //val === 1
});

Arguments Returns Source
function (val){
   var ret = new Promise();
   when(val).chain(ret.callback, ret.errback);
   return ret.promise();
}
    

secondsAgo Static Function Public


Defined base/date/index.js

Subtracts the specified second/s from the current date.

Arguments Returns Source
function (val){
   return date.add(new Date(), "seconds", -val);
}
    

secondsFromNow Static Function Public


Defined base/date/index.js

Adds the specified second/s to the current date.

Arguments Returns Source
function (val){
   return date.add(new Date(), "seconds", val);
}
    

serial Static Function Public


Defined promise.js

Executes a list of items in a serial manner. If the list contains promises then each promise will be executed in a serial manner, if the list contains non async items then the next item in the list is called.

Example
var asyncAction = function(item, timeout){
   var ret = new comb.Promise();
   setTimeout(comb.hitchIgnore(ret, "callback", item), timeout);
   return ret.promise();
};

comb.serial([
    comb.partial(asyncAction, 1, 1000),
    comb.partial(asyncAction, 2, 900),
    comb.partial(asyncAction, 3, 800),
    comb.partial(asyncAction, 4, 700),
    comb.partial(asyncAction, 5, 600),
    comb.partial(asyncAction, 6, 500)
]).then(function(results){
    console.log(results); // [1,2,3,4,5,6];
});
        
Arguments Source
function (list,callback,errback){
   if (base.isArray(list)) {
       return callNext(list, [], false);
   } else {
       throw new Error("When calling comb.serial the first argument must be an array");
   }
}
    

singularize Static Function Public


Defined base/inflections.js

The reverse of pluralize, returns the singular form of a word in a string.

Example
comb.singularize("posts") => "post"
  comb.singularize("octopi")=> "octopus"
  comb.singularize("sheep") => "sheep"
  comb.singularize("word") => "word"
  comb.singularize("the blue mailmen") => "the blue mailman"
  comb.singularize("CamelOctopi") => "CamelOctopus"
        
Arguments Returns Source
function (str){
   var ret = str;
   if (!misc.isUndefinedOrNull(str)) {
       if (UNCOUNTABLES.indexOf(str) === -1) {
           for (var i in SINGULARS) {
               var s = SINGULARS[i], rule = s[0], replacement = s[1];
               if ((ret = ret.replace(rule, replacement)) !== str) {
                   break;
               }
           }
       }
   }
   return ret;
}
    

unListen Static Function Public


Defined base/broadcast.js

Disconnects a listener

Arguments Source
function (handle){
   if (handle) {
       var topic = handle.topic, list = listeners[topic];
       if (list) {
           for (var i = list.length - 1; i >= 0; i--) {
               if (list[i] === handle) {
                   list.splice(i, 1);
               }
           }
           if (!list.length) {
               delete listeners[topic];
           }
       }
   }
       
}
    

underscore Static Function Public


Defined base/inflections.js

The reverse of camelize. Makes an underscored form from the expression in the string.

Example
comb.underscore('helloWorld') => hello_world
 comb.underscore('column_name') => column_name
 comb.underscore('columnName') => column_name
 comb.underscore(null) => null
 comb.underscore() => undefined
        
Arguments Returns Source
function (str){
   var ret = str;
   if (!misc.isUndefinedOrNull(str)) {
       ret = str.replace(UNDERSCORE_CONVERT_REGEXP1, UNDERSCORE_CONVERT_REPLACE)
           .replace(UNDERSCORE_CONVERT_REGEXP2, UNDERSCORE_CONVERT_REPLACE)
           .replace(DASH, UNDERSCORE).toLowerCase();
   }
   return ret;
}
    

wait Static Function Public


Defined promise.js

Ensures that a promise is resolved before a the function can be run.

For example suppose you have to ensure that you are connected to a database before you execute a function.

var findUser = comb.wait(connect(), function findUser(id){
     //this wont execute until we are connected
     return User.findById(id);
});

 comb.when(findUser(1), findUser(2)).then(function(users){
     var user1 = users[0], user2 = users[1];
 });

Arguments Returns Source
function (args,fn){
   var resolved = false;
   args = argsToArray(arguments);
   fn = args.pop();
   var p = when(args);
   return function waiter() {
       if (!resolved) {
           var args = arguments;
           return p.chain(function () {
               resolved = true;
               p = null;
               return fn.apply(this, args);
           }.bind(this));
       } else {
           return when(fn.apply(this, arguments));
       }
   };
}
    

weekDaysAgo Static Function Public


Defined base/date/index.js

Subtracts the specified weekday/s from the current date.

Arguments Returns Source
function (val){
   return date.add(new Date(), "weekdays", -val);
}
    

weekDaysFromNow Static Function Public


Defined base/date/index.js

Adds the specified weekday/s to the current date.

Arguments Returns Source
function (val){
   return date.add(new Date(), "weekdays", val);
}
    

when Static Function Public


Defined promise.js

Waits for promise and non promise values to resolve and fires callback or errback appropriately. If you pass in an array of promises then it will wait for all promises in the list to resolve.

Example
var a = "hello";
 var b = new comb.Promise().callback(world);
 comb.when(a, b) => called back with ["hello", "world"];
        
Arguments Returns Source
function (args,cb,eb){
   var p;
   args = argsToArray(arguments);
   eb = base.isFunction(args[args.length - 1]) ? args.pop() : null;
   cb = base.isFunction(args[args.length - 1]) ? args.pop() : null;
   if (eb && !cb) {
       cb = eb;
       eb = null;
   }
   if (!args.length) {
       p = new Promise().callback(args);
   } else if (args.length === 1) {
       args = args.pop();
       if (isPromiseLike(args)) {
           p = args;
       } else if (isArray(args) && array.every(args, isPromiseLike)) {
           p = new PromiseList(args, true);
       } else {
           p = new Promise().callback(args);
       }
   } else {
       p = new PromiseList(args.map(function (a) {
           return isPromiseLike(a) ? a : new Promise().callback(a);
       }), true);
   }
   if (cb) {
       p.addCallback(cb);
   }
   if (eb) {
       p.addErrback(eb);
   }
   return p.promise();
}
    

wrap Static Function Public


Defined promise.js

Wraps traditional node style functions with a promise.

Example
var fs = require("fs");
var readFile = comb.wrap(fs.readFile, fs);
readFile(__dirname + "/test.json").then(
     function(buffer){
         console.log(contents);
     },
     function(err){

     }  console.error(err);
);
        
Arguments Returns Source
function (fn,scope){
   return function () {
       var ret = new Promise();
       var args = argsToArray(arguments);
       args.push(ret.resolve.bind(ret));
       fn.apply(scope || this, args);
       return ret.promise();
   };
}
    

yearsAgo Static Function Public


Defined base/date/index.js

Subtracts the specified year/s from the current date.

Arguments Returns Source
function (val){
   return date.add(new Date(), "years", -val);
}
    

yearsFromNow Static Function Public


Defined base/date/index.js

Adds the specified year/s to the current date.

Example
//assuming that current year is 2012
comb.yearsFromNow(1); //2013-mm-dd hh:MM:ss
        
Arguments Returns Source
function (val){
   return date.add(new Date(), "years", val);
}
    

License

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

Meta