larvitutils
Misc utilities
Loading of library
The library takes one parameter as option, "log". It is designed to take an instance of winston, but more exactly it should be an object with the methods "silly", "debug", "verbose", "info", "warn" and "error". An example of this can be found in the VERY simplified logging utility built in to this library. See documentation below.
Example of loading the library with no configured logger (using the default):
const lUtils = new (require('larvitutils'))();
Example of loading the library with an instance of winston as logger:
const winston = require('winston');
const log = winston.createLogger({'transports':[new winston.transprots.Console()]});
const lUtils = new (require('larvitutils'))({log: log});
Changelog
Very summarized, see specific commits for more details
v3.0.1 - Cleaned out files in the npm package that is not needed
v3.0.0 - Rewrite to TypeScript, and the Log() now require exactly 'none' for no log output. Random strings will no longer be accepted
v2.0.0 - Rewrite to all tings being instanciated
Async setTimeout
const lUtils = new (require('larvitutils'))();
await lUtils.setTimeout(1000);
console.log('1000ms later');
Convert a buffer to an Uuid
const lUtils = new (require('larvitutils'))();
const uuid = lUtils.formatUuid(new Buffer('f9684592b24542fa88c69f16b9236ac3', 'hex'));
console.log(uuid);
Example usecase: fetch a binary column from a database and convert to a readable Uuid string
Format a hex string to uuid
const lUtils = new (require('larvitutils'))();
const uuid = lUtils.formatUuid(' f9684592b24542fa88c69f16b9236ac3');
console.log(uuid);
hrtimeToMs()
Used to convert hrtime() calls to milliseconds, since hrtime() output is messy (seconrds + nanoseconrds)
Usage:
const lUtils = new (require('larvitutils'))();
const startTime = process.hrtime();
setTimeout(function() {
console.log('benchmark took %d ms', lUtils.hrtimeToMs(startTime, 4));
}, 34);
Uuid string to buffer
const lUtils = new (require('larvitutils'))();
const uuidStr = 'f9684592-b245-42fa-88c6-9f16b9236ac3';
lUtils.uuidToBuffer(uuidStr);
Replace all for strings
const lUtils = new (require('larvitutils'))();
const str = 'f9684592-b245-42fa-88c6-9f16b9236ac3';
lUtils.replaceAll('-', '_', str);
Validate an uuid string
const lUtils = new (require('larvitutils'))();
const validUuid = 'f9684592-b245-42fa-88c6-9f16b9236ac3';
const invalidUuid1 = false;
const invalidUuid2 = 'foobar';
const invalidUuid3 = {höhö: 'oveboll'};
lUtils.formatUuid(validUuid);
lUtils.formatUuid(invalidUuid1);
lUtils.formatUuid(invalidUuid2);
lUtils.formatUuid(invalidUuid3);
Check if input is an int
const lUtils = new (require('larvitutils'))();
lUtils.isInt(10);
lUtils.isInt(10.0);
lUtils.isInt(10.5);
lUtils.isInt('oveboll');
Simple logger
This is ment as a very simple replacement for winston
const lUtils = new (require('larvitutils'))();
const log = new lUtils.Log();
log.info('Hello');
log.error('Hello');
By default only info, warn and error are printed to screen. Set minimum level by string, like this:
const lUtils = new (require('larvitutils'))();
const log = new lUtils.Log('debug');
log.debug('Hello');
Or disable output entirely
const lUtils = new (require('larvitutils'))();
const log = new lUtils.Log('none');
log.error('Hello');
The default log level can be changed by setting environment variable NODE_LOG_LVL
All logging methods: silly, debug, verbose, info, warn and error.