Utilz - Various Small Utility Functions for Node.js
Usage
npm install utilz
var utilz = require('utilz');
Examples
watchFile(fileName)
Watch the specificed .js file and quit the server to restart if it has changed. Best used with Supervisord, or Forever (when it matures).
fileName
: the file to watch, may be relative toprocess.cwd()
.
utilz; // watch this .jsutilz; // watch some other .js
In production (NODE_ENV === 'production')
, this will not restart imediatelly, but rather wait 2 seconds, to allow for all files to be replaced first, in the case of a full update.
timeSpan(timespanInMs)
Display time duration in human readable format, from number of days to milliseconds.
timespanInMs
: the time interval in milliseconds.
var t1 = Date;var t2 = 'Mon, 21 Dec 2012 21:20:12 GMT'; console;
This will display an interval in the form of:
368d 11h 41m 17s 57ms
formatNumber(number, fractionDigits)
Format a number to the number of decimal places specified, and add grouping commas.
number
: the number to format.fractionDigits
: the number of decimal places.
var n = 123456123456; console;console;
This will display:
123,456.123
123,456
sign(obj, secret, timed)
Sign an object using sha256, optionally add timestamp. The sig
and ts
are reserved property names in the object.
obj
: the object to signsecret
: the secret keytimed
: boolean, whether to add timestamp for later checkingreturn
: the signature, which needs to be assigned toobj.sig
See below for complete example.
verify(obj, secret, timeWindowMs)
Verify a sha256 signed object, optionally check if timestamp falls into the specified window.
obj
: the object to verifysecret
: the secret keytimeWindowMs
: optional, the time window tolerance for the timestamp, in millisecondsreturn
: error message or null on success
var utilz = ;var laeh = ;var _e = laeh_e;var _x = laeh_x; var obj = f: 'my func' a: 'data 1' b: 'data 2' ; console; objsig = utilz; console; ; console;
This will print:
{ f: 'my func', a: 'data 1', b: 'data 2' }
{ f: 'my func', a: 'data 1', b: 'data 2', ts: 1324557616927,
sig: '1adb63e40223fe95a543983de5c4b4d164a84c05af3effebd1157c50e5b1a533' }
success
mongodbInit(mongodb, dbName, srvHost, srvPort, config, cb)
Initialize mongodb (node-mongodb-native) indexes for a specific collection.
mongodb
: the mongodb moduledbName
: name of the database you want to usesrvHost
: the computer running mongodsrvPort
: the port mongod listens atconfig
: config object with collection names and their indexes, see belowcb
: finish callback in form offunction(err, db)
, where thedb
is the initialized mongo database
var mongo = ; var cfg = opts: safe: true // options for the mongodb driver col1: 'email' col2: 'created' 'name' 'age' ; utilz;
mongodbFAMCheck(err, msg)
Special treatment for mongodb's findAndModify(), when the object is not found.
When you use findAndModify with a query which finds no object, mongo reports this as object not found, which you may find misleading as with all other functions, the result is simply set to null. Additionaly, the message "object not found" is too generic to provide any value. This little function will simply isolate this special case and allows you attach your special message to it. Make sure to instruct _x
to not intercept the err
parameter, as in this case the mongodbFAMCheck
fully takes over.
col;
randomString(opts)
Generate a random string based on the passed options. Example options (the probabilities are checked from smallest to largest via less-than-or-equal):
{
length: 10,
ratios: [
{ type: 'numeric', probability: .2 },
{ type: 'upper', probability: .6 },
{ type: 'lower', probability: 1 }
]
}
More...
More functions will be added in the future.