Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

larvitutils

Package Overview
Dependencies
Maintainers
1
Versions
185
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

larvitutils - npm Package Compare versions

Comparing version 1.1.3 to 2.0.0

renovate.json

124

index.js
'use strict';
const topLogPrefix = 'larvitutils: index.js: ',
log = require('winston');
const topLogPrefix = 'larvitutils: index.js: ';
function Utils(options) {
const that = this;
if ( ! that) {
throw new Error('This library must be instanciated.');
}
that.options = options || {};
if ( ! that.options.log) {
that.options.log = new that.Log();
}
that.log = that.options.log;
}
/**

@@ -13,7 +28,7 @@ * Convert hrtime diff to milliseconds

*/
function hrtimeToMs(prevTime, precision) {
Utils.prototype.hrtimeToMs = function hrtimeToMs(prevTime, precision) {
const diff = process.hrtime(prevTime);
if (precision === undefined) {
precision = 2;
precision = 2;
}

@@ -24,5 +39,5 @@

function escapeRegExp(str) {
Utils.prototype.escapeRegExp = function escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');
}
};

@@ -36,7 +51,7 @@ /**

*/
function formatUuid(uuidStr) {
Utils.prototype.formatUuid = function formatUuid(uuidStr) {
// If a buffer, get the string representation
if (Buffer.isBuffer(uuidStr)) {
uuidStr = uuidStr.toString('hex');
uuidStr = uuidStr.toString('hex');
}

@@ -50,3 +65,3 @@

// Remove all but hex characters
uuidStr = uuidStr.replace(/[^A-Fa-f0-9]/g, '').toLowerCase();
uuidStr = uuidStr.replace(/[^A-Fa-f0-9]/g, '').toLowerCase();

@@ -59,3 +74,3 @@ // All uuid strings have exactly 32 hex characters!

// Add dashes in the right places
uuidStr = uuidStr.substring(0, 8) + '-' + uuidStr.substring(8, 12) + '-' + uuidStr.substring(12, 16) + '-' + uuidStr.substring(16, 20) + '-' + uuidStr.substring(20);
uuidStr = uuidStr.substring(0, 8) + '-' + uuidStr.substring(8, 12) + '-' + uuidStr.substring(12, 16) + '-' + uuidStr.substring(16, 20) + '-' + uuidStr.substring(20);

@@ -74,5 +89,6 @@ return uuidStr;

*/
function replaceAll(search, replace, str) {
return str.replace(new RegExp(escapeRegExp(search), 'g'), replace);
}
Utils.prototype.replaceAll = function replaceAll(search, replace, str) {
const that = this;
return str.replace(new RegExp(that.escapeRegExp(search), 'g'), replace);
};

@@ -85,9 +101,10 @@ /**

*/
function uuidToBuffer(uuidStr) {
const logPrefix = topLogPrefix + 'uuidToBuffer() - ';
Utils.prototype.uuidToBuffer = function uuidToBuffer(uuidStr) {
const logPrefix = topLogPrefix + 'uuidToBuffer() - ',
that = this;
if (typeof uuidStr !== 'string') {
const stack = new Error().stack;
log.warn(logPrefix + 'uuidStr is not a string, but a ' + (typeof uuidStr));
log.verbose(logPrefix + stack);
that.log.warn(logPrefix + 'uuidStr is not a string, but a ' + (typeof uuidStr));
that.log.verbose(logPrefix + stack);
return false;

@@ -102,4 +119,4 @@ }

const stack = new Error().stack;
log.warn(logPrefix + 'uuidStr should be exactly 32 characters after regex, but is: ' + uuidStr.length);
log.verbose(logPrefix + stack);
that.log.warn(logPrefix + 'uuidStr should be exactly 32 characters after regex, but is: ' + uuidStr.length);
that.log.verbose(logPrefix + stack);
return false;

@@ -117,4 +134,4 @@ }

*/
function isInt(value) {
const x = parseFloat(value);
Utils.prototype.isInt = function isInt(value) {
const x = parseFloat(value);

@@ -126,9 +143,60 @@ if (isNaN(value)) {

return (x | 0) === x;
}
};
exports.formatUuid = formatUuid;
exports.hrtimeToMs = hrtimeToMs;
exports.instances = {};
exports.replaceAll = replaceAll;
exports.uuidToBuffer = uuidToBuffer;
exports.isInt = isInt;
Utils.prototype.Log = function Log(options) {
const that = this;
that.options = options || {};
if (typeof that.options === 'string') {
that.options = {'level': that.options};
}
if ( ! that.options.level) {
that.options.level = 'info';
}
};
Utils.prototype.Log.prototype.stdout = function stdout(lvl, msg) {
console.log((new Date()).toISOString().substring(0, 19) + 'Z [' + lvl + '] ' + msg);
};
Utils.prototype.Log.prototype.stderr = function stderr(lvl, msg) {
console.error((new Date()).toISOString().substring(0, 19) + 'Z [' + lvl + '] ' + msg);
};
Utils.prototype.Log.prototype.silly = function silly(msg) {
if (this.options.level === 'silly') this.stdout('sil', msg);
};
Utils.prototype.Log.prototype.debug = function debug(msg) {
if (['silly', 'debug'].indexOf(this.options.level) !== - 1) {
this.stdout('deb', msg);
}
};
Utils.prototype.Log.prototype.verbose = function verbose(msg) {
if (['silly', 'debug', 'verbose'].indexOf(this.options.level) !== - 1) {
this.stdout('ver', msg);
}
};
Utils.prototype.Log.prototype.info = function info(msg) {
if (['silly', 'debug', 'verbose', 'info'].indexOf(this.options.level) !== - 1) {
this.stdout('inf', msg);
}
};
Utils.prototype.Log.prototype.warn = function warn(msg) {
if (['silly', 'debug', 'verbose', 'info', 'warn'].indexOf(this.options.level) !== - 1) {
this.stderr('war', msg);
}
};
Utils.prototype.Log.prototype.error = function error(msg) {
if (['silly', 'debug', 'verbose', 'info', 'error'].indexOf(this.options.level) !== - 1) {
this.stderr('err', msg);
}
};
exports = module.exports = Utils;
{
"name": "larvitutils",
"version": "1.1.3",
"version": "2.0.0",
"description": "Misc utils",
"main": "index.js",
"devDependencies": {
"mocha": "^5.2.0",
"mocha-eslint": "^4.1.0"
"mocha": "5.2.0",
"mocha-eslint": "4.1.0"
},

@@ -27,6 +27,3 @@ "scripts": {

},
"homepage": "https://github.com/larvit/larvitutils#readme",
"dependencies": {
"winston": "^2.3.0"
}
"homepage": "https://github.com/larvit/larvitutils#readme"
}

@@ -7,6 +7,24 @@ [![Build Status](https://travis-ci.org/larvit/larvitutils.svg)](https://travis-ci.org/larvit/larvitutils) [![Dependencies](https://david-dm.org/larvit/larvitutils.svg)](https://david-dm.org/larvit/larvitutils.svg)

## Loading of library
The library takes one parameter as option, "log". It is designed to take an instance of [winston](https://github.com/winstonjs/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):
```javascript
const utils = new (require('larvitutils'))();
```
Example of loading the library with an instance of [winston](https://github.com/winstonjs/winston) as logger:
```javascript
const winston = require('winston'),
log = winston.createLogger({'transports':[new winston.transprots.Console()]}),
utils = new (require('larvitutils'))({'log': log});
```
## Convert a buffer to an Uuid
```javascript
const utils = require('larvitutils'),
const utils = new (require('larvitutils'))(),
uuid = utils.formatUuid(new Buffer('f9684592b24542fa88c69f16b9236ac3', 'hex'));

@@ -22,3 +40,3 @@

```javascript
const utils = require('larvitutils'),
const utils = new (require('larvitutils'))(),
uuid = utils.formatUuid(' f9684592b24542fa88c69f16b9236ac3'); // Notice the starting space getting trimmed away

@@ -36,3 +54,3 @@

```javascript
const utils = require('larvitutils'),
const utils = new (require('larvitutils'))(),
startTime = process.hrtime();

@@ -49,3 +67,3 @@

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

@@ -59,3 +77,3 @@

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

@@ -69,3 +87,3 @@

```javascript
const utils = require('larvitutils'),
const utils = new (require('larvitutils'))(),
validUuid = 'f9684592-b245-42fa-88c6-9f16b9236ac3',

@@ -84,3 +102,3 @@ invalidUuid1 = false,

```javascript
const utils = require('larvitutils');
const utils = new (require('larvitutils'))();

@@ -93,26 +111,34 @@ utils.isInt(10); // true

## Instances
## Simple logger
Just a very simple object intended to keep instances of objects across moduels.
This is ment as a very simple replacement for winston
file 1:
```javascript
const utils = require('larvitutils');
const utils = new (require('larvitutils'))(),
log = new utils.Log();
let foo;
log.info('Hello'); // prints to stdout "2018-08-08T20:02:34Z [inf] Hello
log.error('Hello'); // prints to stderr "2018-08-08T20:02:48Z [err] Hello
```
function Foo() {}
By default only info, warn and error are printed to screen. Set minimum level by string, like this:
foo = new Foo();
foo.bar = 'baz';
```javascript
const utils = new (require('larvitutils'))(),
log = new utils.Log('debug');
utils.instances.foo = foo;
log.debug('Hello'); // prints to stdout "2018-08-08T20:02:34Z [deb] Debug
```
file 2:
Or disable output entirely
```javascript
const utils = require('larvitutils'),
foo = utils.instances.foo;
const utils = new (require('larvitutils'))(),
log = new utils.Log('none');
console.log(foo.bar); // 'baz'
log.error('Hello'); // prints nothing
```
All logging methods: silly, debug, verbose, info, warn and error.
'use strict';
require(
'mocha-eslint')([__dirname + '/..'],
require('mocha-eslint')(
[__dirname + '/..'],
{
// Increase the timeout of the test if linting takes to long
'timeout': 5000, // Defaults to the global mocha `timeout` option
'timeout': 5000, // Defaults to the global mocha `timeout` option
// Increase the time until a test is marked as slow
'slow': 1000, // Defaults to the global mocha `slow` option
'slow': 1000, // Defaults to the global mocha `slow` option
}
);
'use strict';
const assert = require('assert'),
utils = require(__dirname + '/../index.js');
utils = new (require(__dirname + '/../index.js'))();
describe('formatUuid', function() {
it('Should convert a binary buffer to Uuid string', function(done) {
describe('formatUuid', function () {
it('Should convert a binary buffer to Uuid string', function (done) {
const uuid = utils.formatUuid(new Buffer('f9684592b24542fa88c69f16b9236ac3', 'hex'));

@@ -15,3 +15,3 @@

it('Should trim a hex string and return its uuid value', function(done) {
it('Should trim a hex string and return its uuid value', function (done) {
const uuidStr = ' 0e7e26b7-f1804d65-9512-80b776a7509e ',

@@ -24,3 +24,3 @@ formatted = utils.formatUuid(uuidStr);

it('Should add dashes to a hex string', function(done) {
it('Should add dashes to a hex string', function (done) {
const uuidStr = '62be934b24c24944981c40d4163e3bc9',

@@ -33,3 +33,3 @@ formatted = utils.formatUuid(uuidStr);

it('Should fail to format a malformed string', function(done) {
it('Should fail to format a malformed string', function (done) {
const blaj = utils.formatUuid('blaj'),

@@ -46,3 +46,3 @@ toShortHex = utils.formatUuid('62be934b24c2494981c40d4163e3bc9'),

it('Should format a upper case string to lower case', function(done) {
it('Should format a upper case string to lower case', function (done) {
const formatted = utils.formatUuid('80D7B01D-E5D8-43A4-B5F1-E2703506860A');

@@ -55,3 +55,3 @@

it('Should fail on anything but a string', function(done) {
it('Should fail on anything but a string', function (done) {
const test1 = utils.formatUuid([3, 4]),

@@ -58,0 +58,0 @@ test2 = utils.formatUuid({'höhö': 'fippel'});

'use strict';
const assert = require('assert'),
utils = require(__dirname + '/../index.js');
utils = new (require(__dirname + '/../index.js'))();
describe('hrtimeToMs', function() {
it('Test default amount of decimals', function(done) {
describe('hrtimeToMs', function () {
it('Test default amount of decimals', function (done) {
const res = utils.hrtimeToMs(process.hrtime());

@@ -14,3 +14,3 @@ assert.strictEqual(typeof res, 'string', 'res should be a string, but returned as ' + typeof res + ' and its value is ' + res);

it('Test custom amount of decimals', function(done) {
it('Test custom amount of decimals', function (done) {
const res = utils.hrtimeToMs(process.hrtime(), 4);

@@ -17,0 +17,0 @@ assert.strictEqual(typeof res, 'string', 'res should be a string, but returned as ' + typeof res + ' and its value is ' + res);

'use strict';
const assert = require('assert'),
utils = require(__dirname + '/../index.js');
utils = new (require(__dirname + '/../index.js'))();
describe('isInt', function() {
it('Should return false if input is a string', function(done) {
describe('isInt', function () {
it('Should return false if input is a string', function (done) {
const input = 'string',

@@ -16,4 +16,4 @@ checkedInt = utils.isInt(input);

it('Should return false if input is a function', function(done) {
const input = function() {},
it('Should return false if input is a function', function (done) {
const input = function () {},
checkedInt = utils.isInt(input);

@@ -26,3 +26,3 @@

it('Should return false if input is a float', function(done) {
it('Should return false if input is a float', function (done) {
const input = 1.5,

@@ -36,3 +36,3 @@ checkedInt = utils.isInt(input);

it('Should return true if input is a float with a zero decimal', function(done) {
it('Should return true if input is a float with a zero decimal', function (done) {
const input = 1.0,

@@ -46,3 +46,3 @@ checkedInt = utils.isInt(input);

it('Should return true if input is a int', function(done) {
it('Should return true if input is a int', function (done) {
const input = 1,

@@ -49,0 +49,0 @@ checkedInt = utils.isInt(input);

'use strict';
const assert = require('assert'),
utils = require(__dirname + '/../index.js');
utils = new (require(__dirname + '/../index.js'))();
describe('replaceAll', function() {
it('Should replace all occurences of - to _ in an UUID', function(done) {
describe('replaceAll', function () {
it('Should replace all occurences of - to _ in an UUID', function (done) {
const uuidStr = 'f9684592-b245-42fa-88c6-9f16b9236ac3',

@@ -16,3 +16,3 @@ newStr = utils.replaceAll('-', '_', uuidStr);

it('Should leave a string unaltered if it have no occurences of the string to be replaced', function(done) {
it('Should leave a string unaltered if it have no occurences of the string to be replaced', function (done) {
const uuidStr = 'f9684592b24542fa88c69f16b9236ac3',

@@ -26,3 +26,3 @@ newStr = utils.replaceAll('-', '_', uuidStr);

it('Should be able to replace to nothing', function(done) {
it('Should be able to replace to nothing', function (done) {
const uuidStr = 'f9684592-b245-42fa-88c6-9f16b9236ac3',

@@ -36,3 +36,3 @@ newStr = utils.replaceAll('-', '', uuidStr);

it('Should not break if we pass RegExp unsafe char', function(done) {
it('Should not break if we pass RegExp unsafe char', function (done) {
const uuidStr = 'f9684592-b245-42fa.88c6.9f16b9236ac3',

@@ -39,0 +39,0 @@ newStr = utils.replaceAll('.', 'poo', uuidStr);

'use strict';
const assert = require('assert'),
utils = require(__dirname + '/../index.js'),
log = require('winston');
Utils = require(__dirname + '/../index.js'),
utils = new Utils({'log': new (new Utils()).Log('none')});
log.remove(log.transports.Console);
describe('uuidToBuffer', function() {
it('Should convert an Uuid string to a buffer', function(done) {
describe('uuidToBuffer', function () {
it('Should convert an Uuid string to a buffer', function (done) {
const uuidStr = 'f9684592-b245-42fa-88c6-9f16b9236ac3',

@@ -19,3 +17,3 @@ uuidBuf = utils.uuidToBuffer(uuidStr);

it('Should fail to convert an invalid Uuid string to buffer', function(done) {
it('Should fail to convert an invalid Uuid string to buffer', function (done) {
const uuidStr = 'f96845-42fa-88c6-9f16b9236ac3',

@@ -29,3 +27,3 @@ uuidBuf = utils.uuidToBuffer(uuidStr);

it('Should fail to convert a non-string', function(done) {
it('Should fail to convert a non-string', function (done) {
assert.strictEqual(utils.uuidToBuffer({'foo': 'bar'}), false);

@@ -32,0 +30,0 @@ assert.strictEqual(utils.uuidToBuffer(undefined), false);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc