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 2.2.0 to 2.3.0

35

index.js

@@ -6,15 +6,13 @@ 'use strict';

function Utils(options) {
const that = this;
if (!that) {
if (!this) {
throw new Error('This library must be instanciated.');
}
that.options = options || {};
this.options = options || {};
if (!that.options.log) {
that.options.log = new that.Log();
if (!this.options.log) {
this.options.log = new this.Log();
}
that.log = that.options.log;
this.log = this.options.log;
}

@@ -86,4 +84,3 @@

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

@@ -146,13 +143,19 @@

/**
* Simple logging instance
*
* @param {object | string} options[=process.env.NODE_LOG_LVL] - Optional options object or minimum log level
* @param {string} options.level[=process.env.NODE_LOG_LVL] - log level
*/
Utils.prototype.Log = function Log(options) {
const that = this;
this.options = options || {};
that.options = options || {};
if (typeof that.options === 'string') {
that.options = {level: that.options};
if (typeof this.options === 'string') {
this.options = { level: this.options };
}
if (!that.options.level) {
that.options.level = 'info';
if (!this.options.level && process.env.NODE_LOG_LVL) {
this.options.level = process.env.NODE_LOG_LVL;
} else if (!this.options.level) {
this.options.level = 'info';
}

@@ -159,0 +162,0 @@ };

{
"name": "larvitutils",
"version": "2.2.0",
"version": "2.3.0",
"description": "Misc utils",
"main": "index.js",
"devDependencies": {
"mocha": "6.1.3",
"mocha-eslint": "5.0.0"
"coveralls": "3.0.4",
"eslint": "6.0.1",
"nyc": "14.1.1",
"tape": "4.11.0",
"tape-eslint": "1.2.1"
},
"scripts": {
"test": "mocha"
"test": "npx tape test/*"
},

@@ -13,0 +16,0 @@ "repository": {

@@ -142,2 +142,4 @@ [![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)

The default log level can be changed by setting environment variable NODE_LOG_LVL
All logging methods: silly, debug, verbose, info, warn and error.
'use strict';
require('mocha-eslint')(
[__dirname + '/..'],
{
// Increase the timeout of the test if linting takes to long
timeout: 5000, // Defaults to the global mocha `timeout` option
const test = require('tape');
// Increase the time until a test is marked as slow
slow: 1000 // Defaults to the global mocha `slow` option
}
);
test('eslint', require('tape-eslint')());
'use strict';
const assert = require('assert');
const test = require('tape');
const utils = new (require(__dirname + '/../index.js'))();
describe('formatUuid', () => {
it('Should convert a binary buffer to Uuid string', done => {
const uuid = utils.formatUuid(Buffer.from('f9684592b24542fa88c69f16b9236ac3', 'hex'));
test('formatuuid() - Should convert a binary buffer to Uuid string', t => {
const uuid = utils.formatUuid(Buffer.from('f9684592b24542fa88c69f16b9236ac3', 'hex'));
assert.strictEqual(uuid, 'f9684592-b245-42fa-88c6-9f16b9236ac3');
t.equal(uuid, 'f9684592-b245-42fa-88c6-9f16b9236ac3');
done();
});
t.end();
});
it('Should trim a hex string and return its uuid value', done => {
const uuidStr = ' 0e7e26b7-f1804d65-9512-80b776a7509e ';
const formatted = utils.formatUuid(uuidStr);
test('formatuuid() - Should trim a hex string and return its uuid value', t => {
const uuidStr = ' 0e7e26b7-f1804d65-9512-80b776a7509e ';
const formatted = utils.formatUuid(uuidStr);
assert.strictEqual(formatted, '0e7e26b7-f180-4d65-9512-80b776a7509e');
done();
});
t.equal(formatted, '0e7e26b7-f180-4d65-9512-80b776a7509e');
t.end();
});
it('Should add dashes to a hex string', done => {
const uuidStr = '62be934b24c24944981c40d4163e3bc9';
const formatted = utils.formatUuid(uuidStr);
test('formatuuid() - Should add dashes to a hex string', t => {
const uuidStr = '62be934b24c24944981c40d4163e3bc9';
const formatted = utils.formatUuid(uuidStr);
assert.strictEqual(formatted, '62be934b-24c2-4944-981c-40d4163e3bc9');
done();
});
t.equal(formatted, '62be934b-24c2-4944-981c-40d4163e3bc9');
t.end();
});
it('Should fail to format a malformed string', done => {
const blaj = utils.formatUuid('blaj');
const toShortHex = utils.formatUuid('62be934b24c2494981c40d4163e3bc9');
const toLongHex = utils.formatUuid('62be934b24c24944981c40d4163e3bc93');
test('formatuuid() - Should fail to format a malformed string', t => {
const blaj = utils.formatUuid('blaj');
const toShortHex = utils.formatUuid('62be934b24c2494981c40d4163e3bc9');
const toLongHex = utils.formatUuid('62be934b24c24944981c40d4163e3bc93');
assert.strictEqual(blaj, false);
assert.strictEqual(toShortHex, false);
assert.strictEqual(toLongHex, false);
t.equal(blaj, false);
t.equal(toShortHex, false);
t.equal(toLongHex, false);
done();
});
t.end();
});
it('Should format a upper case string to lower case', done => {
const formatted = utils.formatUuid('80D7B01D-E5D8-43A4-B5F1-E2703506860A');
test('formatuuid() - Should format a upper case string to lower case', t => {
const formatted = utils.formatUuid('80D7B01D-E5D8-43A4-B5F1-E2703506860A');
assert.strictEqual(formatted, '80d7b01d-e5d8-43a4-b5f1-e2703506860a');
t.equal(formatted, '80d7b01d-e5d8-43a4-b5f1-e2703506860a');
done();
});
t.end();
});
it('Should fail on anything but a string', done => {
const test1 = utils.formatUuid([3, 4]);
const test2 = utils.formatUuid({höhö: 'fippel'});
test('formatuuid() - Should fail on anything but a string', t => {
const test1 = utils.formatUuid([3, 4]);
const test2 = utils.formatUuid({höhö: 'fippel'});
assert.strictEqual(test1, false);
assert.strictEqual(test2, false);
t.equal(test1, false);
t.equal(test2, false);
done();
});
t.end();
});
'use strict';
const assert = require('assert');
const test = require('tape');
const utils = new (require(__dirname + '/../index.js'))();
describe('hrtimeToMs', function () {
it('Test default amount of decimals', done => {
const res = utils.hrtimeToMs(process.hrtime());
assert.strictEqual(typeof res, 'string', 'res should be a string, but returned as ' + typeof res + ' and its value is ' + res);
assert.strictEqual(res.split('.')[1].length, 2, 'The string length after the . should be 2, but is ' + res.split('.')[1].length);
done();
});
test('hrtimeToMs() - Test default amount of decimals', t => {
const res = utils.hrtimeToMs(process.hrtime());
t.equal(typeof res, 'string', 'res should be a string, but returned as ' + typeof res + ' and its value is ' + res);
t.equal(res.split('.')[1].length, 2, 'The string length after the . should be 2, but is ' + res.split('.')[1].length);
t.end();
});
it('Test custom amount of decimals', done => {
const res = utils.hrtimeToMs(process.hrtime(), 4);
assert.strictEqual(typeof res, 'string', 'res should be a string, but returned as ' + typeof res + ' and its value is ' + res);
assert.strictEqual(res.split('.')[1].length, 4, 'The string length after the . should be 4, but is ' + res.split('.')[1].length);
done();
});
test('hrtimeToMs() - Test custom amount of decimals', t => {
const res = utils.hrtimeToMs(process.hrtime(), 4);
t.equal(typeof res, 'string', 'res should be a string, but returned as ' + typeof res + ' and its value is ' + res);
t.equal(res.split('.')[1].length, 4, 'The string length after the . should be 4, but is ' + res.split('.')[1].length);
t.end();
});
'use strict';
const assert = require('assert');
const test = require('tape');
const utils = new (require(__dirname + '/../index.js'))();
describe('isInt', function () {
it('Should return false if input is a string', done => {
const input = 'string';
const checkedInt = utils.isInt(input);
test('isInt() - Should return false if input is a string', t => {
const input = 'string';
const checkedInt = utils.isInt(input);
assert.strictEqual(checkedInt, false, 'Should return false if input is a string, but returned true.');
t.equal(checkedInt, false, 'Should return false if input is a string, but returned true.');
done();
});
t.end();
});
it('Should return false if input is a function', done => {
function input() {};
const checkedInt = utils.isInt(input);
test('isInt() - Should return false if input is a function', t => {
function input() {};
const checkedInt = utils.isInt(input);
assert.strictEqual(checkedInt, false, 'Should return false if input is a function, but returned true.');
t.equal(checkedInt, false, 'Should return false if input is a function, but returned true.');
done();
});
t.end();
});
it('Should return false if input is a float', done => {
const input = 1.5;
const checkedInt = utils.isInt(input);
test('isInt() - Should return false if input is a float', t => {
const input = 1.5;
const checkedInt = utils.isInt(input);
assert.strictEqual(checkedInt, false, 'Should return false if input is a float, but returned true.');
t.equal(checkedInt, false, 'Should return false if input is a float, but returned true.');
done();
});
t.end();
});
it('Should return true if input is a float with a zero decimal', done => {
const input = 1.0;
const checkedInt = utils.isInt(input);
test('isInt() - Should return true if input is a float with a zero decimal', t => {
const input = 1.0;
const checkedInt = utils.isInt(input);
assert.strictEqual(checkedInt, true, 'Should return true if input is a float with zero decimal, but returned false.');
t.equal(checkedInt, true, 'Should return true if input is a float with zero decimal, but returned false.');
done();
});
t.end();
});
it('Should return true if input is a int', done => {
const input = 1;
const checkedInt = utils.isInt(input);
test('isInt() - Should return true if input is a int', t => {
const input = 1;
const checkedInt = utils.isInt(input);
assert.strictEqual(checkedInt, true, 'Should return true if input is a int, but returned false.');
t.equal(checkedInt, true, 'Should return true if input is a int, but returned false.');
done();
});
t.end();
});
'use strict';
const assert = require('assert');
const test = require('tape');
const utils = new (require(__dirname + '/../index.js'))();
describe('log', function () {
it('should log to info', done => {
const oldStdout = process.stdout.write;
const log = new utils.Log();
test('log - should log to info', t => {
const oldStdout = process.stdout.write;
const log = new utils.Log();
let outputMsg;
let outputMsg;
process.stdout.write = function (msg) {
outputMsg = msg;
};
process.stdout.write = msg => outputMsg = msg;
log.info('flurp');
log.info('flurp');
process.stdout.write = oldStdout;
process.stdout.write = oldStdout;
assert.strictEqual(outputMsg.substring(19), 'Z [\u001b[1;32minf\u001b[0m] flurp\n');
t.equal(outputMsg.substring(19), 'Z [\u001b[1;32minf\u001b[0m] flurp\n');
done();
});
t.end();
});
it('should log to error', done => {
const oldStderr = process.stderr.write;
const log = new utils.Log();
test('log - should log to error', t => {
const oldStderr = process.stderr.write;
const log = new utils.Log();
let outputMsg;
let outputMsg;
process.stderr.write = function (msg) {
outputMsg = msg;
};
process.stderr.write = msg => outputMsg = msg;
log.error('burp');
log.error('burp');
process.stderr.write = oldStderr;
process.stderr.write = oldStderr;
assert.strictEqual(outputMsg.substring(19), 'Z [\u001b[1;31merr\u001b[0m] burp\n');
t.equal(outputMsg.substring(19), 'Z [\u001b[1;31merr\u001b[0m] burp\n');
done();
});
t.end();
});
it('should not print debug by default', done => {
const oldStdout = process.stdout.write;
const log = new utils.Log();
test('log - should not print debug by default', t => {
const oldStdout = process.stdout.write;
const log = new utils.Log();
let outputMsg = 'yay';
let outputMsg = 'yay';
process.stdout.write = function (msg) {
outputMsg = msg;
};
process.stdout.write = msg => outputMsg = msg;
log.debug('nai');
log.debug('nai');
process.stdout.write = oldStdout;
process.stdout.write = oldStdout;
assert.strictEqual(outputMsg, 'yay');
t.equal(outputMsg, 'yay');
done();
});
t.end();
});
it('should print debug when given "silly" as level', done => {
const oldStdout = process.stdout.write;
const log = new utils.Log('silly');
test('log - should print debug when given "silly" as level', t => {
const oldStdout = process.stdout.write;
const log = new utils.Log('silly');
let outputMsg = 'woof';
let outputMsg = 'woof';
process.stdout.write = function (msg) {
outputMsg = msg;
};
process.stdout.write = msg => outputMsg = msg;
log.debug('wapp');
log.debug('wapp');
process.stdout.write = oldStdout;
process.stdout.write = oldStdout;
assert.strictEqual(outputMsg.substring(19), 'Z [\u001b[1;35mdeb\u001b[0m] wapp\n');
t.equal(outputMsg.substring(19), 'Z [\u001b[1;35mdeb\u001b[0m] wapp\n');
done();
});
t.end();
});
test('log - Use environment variable as default log level', t => {
const oldEnv = process.env.NODE_LOG_LVL;
const oldStdout = process.stdout.write;
let outputMsg = 'fail';
process.env.NODE_LOG_LVL = 'debug';
const log = new utils.Log();
process.stdout.write = msg => outputMsg = msg;
log.debug('tepp');
process.stdout.write = oldStdout;
process.env.NODE_LOG_LVL = oldEnv;
t.equal(outputMsg.substring(19), 'Z [\u001b[1;35mdeb\u001b[0m] tepp\n');
t.end();
});
'use strict';
const assert = require('assert');
const test = require('tape');
const utils = new (require(__dirname + '/../index.js'))();
describe('replaceAll', function () {
it('Should replace all occurences of - to _ in an UUID', done => {
const uuidStr = 'f9684592-b245-42fa-88c6-9f16b9236ac3';
const newStr = utils.replaceAll('-', '_', uuidStr);
test('replaceAll() - Should replace all occurences of - to _ in an UUID', t => {
const uuidStr = 'f9684592-b245-42fa-88c6-9f16b9236ac3';
const newStr = utils.replaceAll('-', '_', uuidStr);
assert.strictEqual(newStr, 'f9684592_b245_42fa_88c6_9f16b9236ac3');
t.equal(newStr, 'f9684592_b245_42fa_88c6_9f16b9236ac3');
done();
});
t.end();
});
it('Should leave a string unaltered if it have no occurences of the string to be replaced', done => {
const uuidStr = 'f9684592b24542fa88c69f16b9236ac3';
const newStr = utils.replaceAll('-', '_', uuidStr);
test('replaceAll() - Should leave a string unaltered if it have no occurences of the string to be replaced', t => {
const uuidStr = 'f9684592b24542fa88c69f16b9236ac3';
const newStr = utils.replaceAll('-', '_', uuidStr);
assert.strictEqual(newStr, 'f9684592b24542fa88c69f16b9236ac3');
t.equal(newStr, 'f9684592b24542fa88c69f16b9236ac3');
done();
});
t.end();
});
it('Should be able to replace to nothing', done => {
const uuidStr = 'f9684592-b245-42fa-88c6-9f16b9236ac3';
const newStr = utils.replaceAll('-', '', uuidStr);
test('replaceAll() - Should be able to replace to nothing', t => {
const uuidStr = 'f9684592-b245-42fa-88c6-9f16b9236ac3';
const newStr = utils.replaceAll('-', '', uuidStr);
assert.strictEqual(newStr, 'f9684592b24542fa88c69f16b9236ac3');
t.equal(newStr, 'f9684592b24542fa88c69f16b9236ac3');
done();
});
t.end();
});
it('Should not break if we pass RegExp unsafe char', done => {
const uuidStr = 'f9684592-b245-42fa.88c6.9f16b9236ac3';
const newStr = utils.replaceAll('.', 'poo', uuidStr);
test('replaceAll() - Should not break if we pass RegExp unsafe char', t => {
const uuidStr = 'f9684592-b245-42fa.88c6.9f16b9236ac3';
const newStr = utils.replaceAll('.', 'poo', uuidStr);
assert.strictEqual(newStr, 'f9684592-b245-42fapoo88c6poo9f16b9236ac3');
t.equal(newStr, 'f9684592-b245-42fapoo88c6poo9f16b9236ac3');
done();
});
t.end();
});
'use strict';
const assert = require('assert');
const test = require('tape');
const lUtils = new (require(__dirname + '/../index.js'))();
describe('setTimeout', function () {
it('Wait about 100ms', done => {
const start = new Date();
lUtils.setTimeout(100).then(() => {
const passedTime = new Date() - start;
assert.strictEqual((passedTime - 100) >= 0, true);
assert.strictEqual((passedTime - 110) < 0, true);
done();
});
test('setTimeout() - Wait about 100ms', t => {
const start = new Date();
lUtils.setTimeout(100).then(() => {
const passedTime = new Date() - start;
t.equal((passedTime - 100) >= 0, true);
t.equal((passedTime - 110) < 0, true);
t.end();
});
});
it('Wait about 1ms', done => {
const start = new Date();
lUtils.setTimeout(1).then(() => {
const passedTime = new Date() - start;
assert.strictEqual((passedTime - 1) >= 0, true);
assert.strictEqual((passedTime - 10) < 0, true);
done();
});
test('setTimeout() - Wait about 1ms', t => {
const start = new Date();
lUtils.setTimeout(1).then(() => {
const passedTime = new Date() - start;
t.equal((passedTime - 1) >= 0, true);
t.equal((passedTime - 10) < 0, true);
t.end();
});
});
'use strict';
const assert = require('assert');
const test = require('tape');
const Utils = require(__dirname + '/../index.js');
const utils = new Utils({log: new (new Utils()).Log('none')});
describe('uuidToBuffer', function () {
it('Should convert an Uuid string to a buffer', done => {
const uuidStr = 'f9684592-b245-42fa-88c6-9f16b9236ac3';
const uuidBuf = utils.uuidToBuffer(uuidStr);
test('uuidToBuffer() - Should convert an Uuid string to a buffer', t => {
const uuidStr = 'f9684592-b245-42fa-88c6-9f16b9236ac3';
const uuidBuf = utils.uuidToBuffer(uuidStr);
assert(uuidBuf.toString('hex') === 'f9684592b24542fa88c69f16b9236ac3', 'Uuid string is wrong, expected "f9684592b24542fa88c69f16b9236ac3" but got "' + uuidBuf.toString('hex') + '"');
t.equal(uuidBuf.toString('hex'), 'f9684592b24542fa88c69f16b9236ac3', 'Uuid string checked, expected "f9684592b24542fa88c69f16b9236ac3", got "' + uuidBuf.toString('hex') + '"');
done();
});
t.end();
});
it('Should fail to convert an invalid Uuid string to buffer', done => {
const uuidStr = 'f96845-42fa-88c6-9f16b9236ac3';
const uuidBuf = utils.uuidToBuffer(uuidStr);
test('uuidToBuffer() - Should fail to convert an invalid Uuid string to buffer', t => {
const uuidStr = 'f96845-42fa-88c6-9f16b9236ac3';
const uuidBuf = utils.uuidToBuffer(uuidStr);
assert(uuidBuf === false, 'uuidBuf should be false, but is "' + uuidBuf + '"');
t.equal(uuidBuf, false, 'uuidBuf should be false, is "' + uuidBuf + '"');
done();
});
t.end();
});
it('Should fail to convert a non-string', done => {
assert.strictEqual(utils.uuidToBuffer({foo: 'bar'}), false);
assert.strictEqual(utils.uuidToBuffer(undefined), false);
assert.strictEqual(utils.uuidToBuffer(null), false);
test('uuidToBuffer() - Should fail to convert a non-string', t => {
t.equal(utils.uuidToBuffer({ foo: 'bar' }), false);
t.equal(utils.uuidToBuffer(undefined), false);
t.equal(utils.uuidToBuffer(null), false);
done();
});
t.end();
});

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