JSPM

larvitutils

1.0.4
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3288
  • Score
    100M100P100Q107394F
  • License ISC

Misc utils

Package Exports

  • larvitutils

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (larvitutils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status Dependencies

larvitutils

Misc utilities

Convert a buffer to an Uuid

const	utils	= require('larvitutils'),
    uuid	= utils.formatUuid(new Buffer('f9684592b24542fa88c69f16b9236ac3', 'hex'));

console.log(uuid); // f9684592-b245-42fa-88c6-9f16b9236ac3

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'); // Notice the starting space getting trimmed away

console.log(uuid); // f9684592-b245-42fa-88c6-9f16b9236ac3

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));
    // benchmark took 34.0005 ms
}, 34);

Uuid string to buffer

const	utils	= require('larvitutils'),
    uuidStr	= 'f9684592-b245-42fa-88c6-9f16b9236ac3';

utils.uuidToBuffer(uuidStr); // Will return a buffer

Replace all for strings

const	utils	= require('larvitutils'),
    str	= 'f9684592-b245-42fa-88c6-9f16b9236ac3';

utils.replaceAll('-', '_', str); // f9684592_b245_42fa_88c6_9f16b9236ac3

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);	// true
utils.formatUuid(invalidUuid1);	// false
utils.formatUuid(invalidUuid2);	// false
utils.formatUuid(invalidUuid3);	// false

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); // 'baz'