dry-underscore
Advanced tools
Comparing version 0.4.13 to 0.4.16
@@ -309,2 +309,4 @@ | ||
context = context || null; | ||
var keys = _.keys(o); | ||
@@ -325,2 +327,12 @@ | ||
}, | ||
asyncMap : function(o, mapFunction, completedCallback, context){ | ||
var results = []; | ||
completedCallback = completedCallback || _.noop; | ||
context = context || null; | ||
_.eachAsync(o, function(val, key, next){ | ||
mapFunction.call(context, val, key, function(result){ results.push(result); next(); }); | ||
}, function(){ completedCallback(results); }); | ||
}, | ||
find : function(col, target, insensitive){ | ||
@@ -742,3 +754,3 @@ if(!insensitive && _.isArray(col)){ | ||
if (isNull(x) || !isObject(x)) { | ||
str += ' ' + x; | ||
str += '' + x; | ||
} else { | ||
@@ -745,0 +757,0 @@ str += ' ' + inspect(x); |
156
lib/log.js
exports.mixin = function(_){ | ||
var logNs = []; | ||
// var options = { level: 'info' }; | ||
var options = { level: 'debug' }; | ||
function makeLogger(options){ | ||
var log = function(){ | ||
options = options || {}; | ||
function print(){ | ||
var log = function(){ | ||
if(arguments.length){ writeToLog.apply(this, arguments); } | ||
return(makeLoggers()); | ||
}; | ||
log._ns = []; | ||
log.init = function(){ | ||
log.timestamp(options.timestamp); | ||
if(options.namespace){ log._ns.push(options.namespace); } | ||
if(options.level){ log.level(options.level); } | ||
if(options.parent){ log._parent = options.parent; } | ||
}; | ||
function makeLoggers(){ | ||
var loggers = {}; | ||
var priorities = log.priorities(); | ||
var ignore = false; | ||
_.each(priorities, function(logName){ | ||
if(ignore){ loggers[logName] = _.noop; } | ||
else{ loggers[logName] = log; } | ||
if(_.lc(log.level()) === _.lc(logName)){ ignore = true; } | ||
}); | ||
return(loggers); | ||
} | ||
log.namespace = function(){ | ||
var ns = ""; | ||
if(log.parent()){ | ||
ns += log.parent().ns() | ||
} | ||
if(log._ns.length){ | ||
ns += log._ns.join("."); | ||
} | ||
return(ns); | ||
}; | ||
function writeToLog(){ | ||
var args = _.toArray(arguments); | ||
var ns = logNs.join("."); | ||
var ns = log.namespace(); | ||
if(ns){ ns += ": "; } | ||
args.unshift(ns); | ||
console.log(_.format.apply(null, args)); | ||
var entry = _.format.apply(null, args); | ||
_.each(log.transports, function(f){ | ||
if(_.isFunction(f.writeEntry)){ | ||
f.writeEntry(_.timestamp(), entry); | ||
}else if(_.isFunction(f)){ | ||
f(_.timestamp(), entry); | ||
} | ||
}); | ||
} | ||
if(arguments.length){ | ||
var args = _.toArray(arguments); | ||
if(_.isObject(arguments[0])){ | ||
_.extend(options, args.unshift()); | ||
} | ||
print.apply(this, args); | ||
log.levels = function(){ return(log._levels); }; | ||
log.priorities = function(){ return(log._priorities); }; | ||
log._levels = ['debug', 'info', 'notice', 'warning', 'error', 'crit', 'alert', 'emerg']; | ||
log._priorities = ['emerg', 'alert', 'crit', 'error', 'warning', 'notice', 'info', 'debug']; | ||
function consoleTransport(timestamp, entry){ | ||
if(log.timestamp()){ console.log(_.moment(timestamp).format("YYYY-MM-DD HH:MM:SS") + ":", entry); } | ||
else{ console.log(entry); } | ||
} | ||
var loggers = {}; | ||
var logPriorities = ['emerg', 'alert', 'crit', 'error', 'warning', 'notice', 'info', 'debug']; | ||
log.transports = [consoleTransport]; | ||
var ignore = false; | ||
_.each(logPriorities, function(logName){ | ||
if(ignore){ loggers[logName] = _.noop; } | ||
else{ loggers[logName] = log; } | ||
if(options.level.toLowerCase() == logName.toLowerCase()){ ignore = true; } | ||
}); | ||
return(loggers); | ||
}; | ||
// you can revert to parent settings by nulling out child settings | ||
log.setting = function(setting, defaultValue){ | ||
if(log[setting] !== undefined && log[setting] !== null){ | ||
return(log[setting]); | ||
}else if(log._parent){ | ||
return(log._parent.getSetting(setting, defaultValue)); | ||
}else{ | ||
return(defaultValue); | ||
} | ||
}; | ||
log.push = function(ns){ logNs.push(ns); }; | ||
log.pop = function(){ return(logNs.pop()); }; | ||
log.wrap = function(ns, f){ | ||
return(function(){ | ||
log.push(ns); | ||
var val = f.apply(this, arguments); | ||
log.pop(); | ||
return(val); | ||
}); | ||
}; | ||
return(log); | ||
log.level = function(level){ | ||
if(level !== undefined){ | ||
log._level = level; | ||
return(log); | ||
}else{ | ||
return(log.setting("_level", _.last(log.priorities()))); | ||
} | ||
}; | ||
log.timestamp = function(enable){ | ||
if(enable !== undefined){ | ||
log._timestamp = enable; | ||
return(log); | ||
}else{ | ||
return(log.setting("_timestamp", false)); | ||
} | ||
}; | ||
log.make = function(options){ | ||
return(makeLogger(options)); | ||
}; | ||
log.child = function(ns){ return(log.make({ namespace: ns, parent: log })); }; | ||
log.parent = function(){ | ||
if(log._parent){ | ||
return(log._parent); | ||
}else{ | ||
return(null); | ||
} | ||
}; | ||
log.init(); | ||
return(log); | ||
} | ||
function mixin(root, options){ | ||
root = root || {}; | ||
root.log = makeLogger(options); | ||
return(root.log); | ||
} | ||
return(mixin()); | ||
}; | ||
@@ -132,3 +132,3 @@ | ||
if(_.isString(actual) && _.isString(expected)){ | ||
var err = new Error(actual + " not eq " + expected); | ||
var err = new Error('"' + actual + '"' + " not eq " + '"' + expected + '"'); | ||
err.actual = actual; | ||
@@ -135,0 +135,0 @@ err.expected = expected; |
{ | ||
"name":"dry-underscore", | ||
"version":"0.4.13", | ||
"version":"0.4.16", | ||
"main": "./lib/index.js", | ||
@@ -22,6 +22,2 @@ "description": "The DRY Undescore Library", | ||
}, | ||
"engines": { | ||
"node": "0.4 || 0.5 || 0.6 || 0.7 || 0.8 || 0.10", | ||
"npm": "1" | ||
}, | ||
"dependencies":{ | ||
@@ -28,0 +24,0 @@ "winston":"" |
@@ -18,2 +18,3 @@ | ||
exports.testHandleBars = testHandleBars; | ||
exports.testAsyncMap = testAsyncMap; | ||
//exports.hashTest = hashTest; | ||
@@ -30,2 +31,20 @@ //exports.testFatal = testFatal; | ||
function testAsyncMap(beforeExit){ | ||
var called = 0; | ||
var a = [1, 2, 3, 4, 5, 6]; | ||
_.asyncMap(a, function(val, i, next){ | ||
_.nextTick(function(){ next(val*2); }); | ||
}, function(result){ | ||
eq(result, [2, 4, 6, 8, 10, 12]); | ||
called++; | ||
}); | ||
beforeExit(function(){ eq(called, 1); }); | ||
} | ||
function testHandleBars(){ | ||
@@ -32,0 +51,0 @@ var data = {"person": { "name": "Alan" }, "company": {"name": "Rad, Inc." } }; |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1418429
64
11275