larvitutils
Misc utilities
Convert a buffer to an Uuid
const utils = require('larvitutils'),
uuid = utils.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 utils = require('larvitutils'),
uuid = utils.formatUuid(' f9684592b24542fa88c69f16b9236ac3');
console.log(uuid);
hrtimeToMs()
Used to convert hrtime() calls to milliseconds, since hrtime() output is messy (seconrds + nanoseconrds)
Usage:
const utils = require('larvitutils'),
startTime = process.hrtime();
setTimeout(function() {
console.log('benchmark took %d ms', utils.hrtimeToMs(startTime, 4));
}, 34);
Uuid string to buffer
const utils = require('larvitutils'),
uuidStr = 'f9684592-b245-42fa-88c6-9f16b9236ac3';
utils.uuidToBuffer(uuidStr);
Replace all for strings
const utils = require('larvitutils'),
str = 'f9684592-b245-42fa-88c6-9f16b9236ac3';
utils.replaceAll('-', '_', str);
Validate an uuid string
const utils = require('larvitutils'),
validUuid = 'f9684592-b245-42fa-88c6-9f16b9236ac3',
invalidUuid1 = false,
invalidUuid2 = 'foobar',
invalidUuid3 = {'höhö': 'oveboll'};
utils.formatUuid(validUuid);
utils.formatUuid(invalidUuid1);
utils.formatUuid(invalidUuid2);
utils.formatUuid(invalidUuid3);
Instances
Just a very simple object intended to keep instances of objects across moduels.
file 1:
const utils = require('larvitutils');
let foo;
function Foo() {}
foo = new Foo();
foo.bar = 'baz';
utils.instances.foo = foo;
file 2:
const utils = require('larvitutils'),
foo = utils.instances.foo;
console.log(foo.bar);