Multi items
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* File: Adawim/helper.js
|
||||
* Author: Gerrit Linnemann
|
||||
*
|
||||
* Swiss-knife.
|
||||
*/
|
||||
|
||||
|
||||
// load the things we need
|
||||
var os = require('os')
|
||||
, dns = require('dns')
|
||||
, Log = require('./logging')
|
||||
, Crypto = require('crypto');
|
||||
|
||||
|
||||
exports.timeSince = function timeSince(date) {
|
||||
|
||||
var seconds = Math.floor((new Date() - date) / 1000);
|
||||
|
||||
var interval = Math.floor(seconds / 31536000);
|
||||
|
||||
if (interval > 1) {
|
||||
return interval + " years";
|
||||
}
|
||||
interval = Math.floor(seconds / 2592000);
|
||||
if (interval > 1) {
|
||||
return interval + " months";
|
||||
}
|
||||
interval = Math.floor(seconds / 86400);
|
||||
if (interval > 1) {
|
||||
return interval + " days";
|
||||
}
|
||||
interval = Math.floor(seconds / 3600);
|
||||
if (interval > 1) {
|
||||
return interval + " hours";
|
||||
}
|
||||
interval = Math.floor(seconds / 60);
|
||||
if (interval > 1) {
|
||||
return interval + " minutes";
|
||||
}
|
||||
return Math.floor(seconds) + " seconds";
|
||||
}
|
||||
|
||||
exports.isResultAvailable = function(res) {
|
||||
return (
|
||||
typeof res !== 'undefined' &&
|
||||
res != null &&
|
||||
typeof res.result !== 'undefined' &&
|
||||
res.result != null
|
||||
);
|
||||
}
|
||||
|
||||
exports.isTypeAvailable = function(res) {
|
||||
return (
|
||||
typeof res !== 'undefined' &&
|
||||
res != null &&
|
||||
typeof res.type !== 'undefined' &&
|
||||
res.type != null
|
||||
);
|
||||
}
|
||||
|
||||
exports.isJSON = function(toCheck) {
|
||||
try {
|
||||
JSON.parse(str);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
exports.isDefinedAndNotNull = function(toCheck) {
|
||||
return (
|
||||
typeof toCheck !== 'undefined' &&
|
||||
toCheck != null
|
||||
);
|
||||
}
|
||||
|
||||
exports.cnt = function(db) {
|
||||
var cnt = 0;
|
||||
|
||||
if(db.constructor === Array) { // do it the array-way
|
||||
cnt = db.length;
|
||||
|
||||
} else { // oh boy, no array ...
|
||||
var cnt_ = 0;
|
||||
for (var key_ in db) { cnt_++; }
|
||||
cnt = cnt_;
|
||||
}
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
exports.each = function(db, callback) {
|
||||
if(db.constructor === Array) { // do it the array-way
|
||||
for(i=0; i < db.length; i++) {
|
||||
var entry = db[i];
|
||||
var isLast = ((i+1) == db.length);
|
||||
|
||||
callback(entry, isLast);
|
||||
}
|
||||
|
||||
} else { // oh boy, no array ...
|
||||
var cnt_ = 0;
|
||||
for (var key_ in db) { cnt_++; }
|
||||
|
||||
var cnt = 0;
|
||||
for (var key in db) {
|
||||
cnt++;
|
||||
var entry = db[key];
|
||||
|
||||
callback(entry, (cnt_==cnt));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
exports.checkValueEquality = function(foo, bar) {
|
||||
return doValueEqualityCheck(foo, bar);
|
||||
}
|
||||
|
||||
exports.simpleHash = function(foo) {
|
||||
var hash = Crypto.createHash('md5').update(foo).digest('hex');
|
||||
return hash;
|
||||
}
|
||||
|
||||
/* private */
|
||||
function doValueEqualityCheck(foo, bar) {
|
||||
var key;
|
||||
for(key in foo) {
|
||||
Log.debug( "Compare \"" + foo[key] + "\" with \"" + bar[key] + "\" for key \"" + key + "\"" );
|
||||
|
||||
if(typeof foo[key] === 'object' && typeof bar[key] === 'object') {
|
||||
var isEquals = doValueEqualityCheck(foo, bar);
|
||||
|
||||
if(!isEquals) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if(foo[key].localeCompare(bar[key]) != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* File: Adawim/logging.js
|
||||
* Author: Gerrit Linnemann
|
||||
*
|
||||
* Logging with several log-level.
|
||||
* - debug: Printed only in development-state.
|
||||
* - log: Needed log-output.
|
||||
* - error: Prints log in error-level.
|
||||
* - inspect: Prints a message and details of an object.
|
||||
*/
|
||||
|
||||
|
||||
// load the things we need
|
||||
var DateFormat = require('dateformat')
|
||||
, Helper = require('./helper.js')
|
||||
, util = require('util')
|
||||
, Inspect = require('eyespect').inspector();
|
||||
|
||||
|
||||
var eyeFriendly = 'friendly';
|
||||
var isDebug = true;
|
||||
var isTrace = (isDebug && false);
|
||||
|
||||
|
||||
exports.isEyeFriendlyMode = function() {
|
||||
return eyeFriendly;
|
||||
}
|
||||
|
||||
exports.isTrace = function() {
|
||||
return isTrace;
|
||||
}
|
||||
|
||||
exports.isDebug = function() {
|
||||
return isDebug;
|
||||
}
|
||||
|
||||
exports.trace = function(msg) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " TRACE " + msg;
|
||||
if(isTrace) {
|
||||
consoleOut(msg);
|
||||
}
|
||||
}
|
||||
|
||||
exports.debug = function(msg) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " DEBUG " + msg;
|
||||
|
||||
if(isDebug) {
|
||||
consoleOut(msg);
|
||||
}
|
||||
}
|
||||
|
||||
exports.log = function(msg) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " INFO " + msg;
|
||||
consoleOut(msg);
|
||||
}
|
||||
|
||||
exports.error = function(msg) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " ERROR " + msg;
|
||||
consoleErr(msg);
|
||||
}
|
||||
|
||||
exports.inspect = function(msg, content) {
|
||||
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " INSPECT " + msg;
|
||||
|
||||
if(eyeFriendly) {
|
||||
Inspect(content, msg);
|
||||
} else {
|
||||
var msgOut = msg + (Helper.isDefinedAndNotNull(content) ? ": "+JSON.stringify(content) : "");
|
||||
consoleOut(msgOut);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* private */
|
||||
function consoleOut(msg, content) {
|
||||
var hasContent = Helper.isDefinedAndNotNull(content);
|
||||
|
||||
if(hasContent) {
|
||||
var msgAddon = (hasContent ? " | " + content : "");
|
||||
if(Helper.isDefinedAndNotNull(msg)) {
|
||||
console.log(msg + msgAddon);
|
||||
}
|
||||
} else if(Helper.isDefinedAndNotNull(msg)) {
|
||||
console.log(msg);
|
||||
}
|
||||
}
|
||||
|
||||
/* private */
|
||||
function consoleErr(msg, content) {
|
||||
var hasContent = Helper.isDefinedAndNotNull(content);
|
||||
|
||||
if(hasContent) {
|
||||
var msgAddon = (hasContent ? " | " + content : "");
|
||||
if(Helper.isDefinedAndNotNull(msg)) {
|
||||
console.error(msg + msgAddon);
|
||||
}
|
||||
} else if(Helper.isDefinedAndNotNull(msg)) {
|
||||
console.error(msg);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user