Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: larvit/larvitutils
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 933e0198b5123c29089c94301c7ba023aa082ae4
Choose a base ref
...
head repository: larvit/larvitutils
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 92c70803e6e0c0da52c02cad274c32c3772713d5
Choose a head ref
  • 4 commits
  • 7 files changed
  • 2 contributors

Commits on Jul 10, 2019

  1. Added coverage

    lillem4n committed Jul 10, 2019
    Copy the full SHA
    bae0d81 View commit details
  2. Fixed gitignore

    lillem4n committed Jul 10, 2019
    Copy the full SHA
    805821c View commit details

Commits on Sep 10, 2019

  1. Copy the full SHA
    a5a5128 View commit details
  2. Copy the full SHA
    92c7080 View commit details
Showing with 120 additions and 6 deletions.
  1. +2 −0 .gitignore
  2. +8 −4 .travis.yml
  3. +3 −1 README.md
  4. +17 −0 index.js
  5. +1 −1 package.json
  6. +12 −0 test/hashUnsignedIntFromString.js
  7. +77 −0 test/log.js
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -26,3 +26,5 @@ node_modules

# Users Environment Variables
.lock-wscript

.nyc_output
12 changes: 8 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
language: node_js

node_js:
- 10
- 12
- 10
- 12

script: node_modules/.bin/nyc --reporter=lcov npm run test

after_script: "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"

notifications:
email:
- lilleman@larvit.se
email:
- lilleman@larvit.se
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[![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)
[![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)
[![Coverage Status](https://coveralls.io/repos/github/larvit/larvitutils/badge.svg)](https://coveralls.io/github/larvit/larvitutils)

# larvitutils

17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -158,6 +158,23 @@ Utils.prototype.Log = function Log(options) {
}
};

/**
* Genereates a string into an unsigned int
*
* @param {string} str The string to convert
* @return {number} the hashed value
*/
Utils.prototype.hashUnsignedIntFromString = function hashUnsignedIntFromString(str) {
let hash = 0;

if (str.length === 0) return hash;
for (let i = 0; i < str.length; i++) {
hash += (1 << str.charCodeAt(i)) >>> 0;
}

return hash;
};

Utils.prototype.Log.prototype.stdout = function stdout(lvl, msg) {
console.log((new Date()).toISOString().substring(0, 19) + 'Z [' + lvl + '] ' + msg);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "larvitutils",
"version": "2.3.0",
"version": "2.3.1",
"description": "Misc utils",
"main": "index.js",
"devDependencies": {
12 changes: 12 additions & 0 deletions test/hashUnsignedIntFromString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const test = require('tape');
const utils = new (require(__dirname + '/../index.js'))();

test('hashUnsignedIntFromString - Test to hash a strgin', t => {
const bah = utils.hashUnsignedIntFromString('hejsan');

t.equal(bah, 541986);

t.end();
});
77 changes: 77 additions & 0 deletions test/log.js
Original file line number Diff line number Diff line change
@@ -92,3 +92,80 @@ test('log - Use environment variable as default log level', t => {

t.end();
});

test('log - Print nothing, even on error, when no valid lvl is set', t => {
const oldStderr = process.stderr.write;
let outputMsg = 'SOMETHING';
const log = new utils.Log('none');
process.stderr.write = msg => outputMsg = msg;
log.error('kattbajs');
process.stderr.write = oldStderr;
t.equal(outputMsg.substring(19), '');
t.end();
});

test('log - Test silly', t => {
const oldStdout = process.stdout.write;
let outputMsg = '';
const log = new utils.Log('silly');
process.stdout.write = msg => outputMsg = msg;
log.silly('kattbajs');
process.stdout.write = oldStdout;
t.equal(outputMsg.substring(19), 'Z [\x1b[1;37msil\x1b[0m] kattbajs\n');
t.end();
});

test('log - Test debug', t => {
const oldStdout = process.stdout.write;
let outputMsg = '';
const log = new utils.Log('debug');
process.stdout.write = msg => outputMsg = msg;
log.debug('kattbajs');
process.stdout.write = oldStdout;
t.equal(outputMsg.substring(19), 'Z [\x1b[1;35mdeb\x1b[0m] kattbajs\n');
t.end();
});

test('log - Test verbose', t => {
const oldStdout = process.stdout.write;
let outputMsg = '';
const log = new utils.Log('verbose');
process.stdout.write = msg => outputMsg = msg;
log.verbose('kattbajs');
process.stdout.write = oldStdout;
t.equal(outputMsg.substring(19), 'Z [\x1b[1;34mver\x1b[0m] kattbajs\n');
t.end();
});

test('log - Test info', t => {
const oldStdout = process.stdout.write;
let outputMsg = '';
const log = new utils.Log('info');
process.stdout.write = msg => outputMsg = msg;
log.info('kattbajs');
process.stdout.write = oldStdout;
t.equal(outputMsg.substring(19), 'Z [\x1b[1;32minf\x1b[0m] kattbajs\n');
t.end();
});

test('log - Test warn', t => {
const oldStderr = process.stderr.write;
let outputMsg = '';
const log = new utils.Log('warn');
process.stderr.write = msg => outputMsg = msg;
log.warn('kattbajs');
process.stderr.write = oldStderr;
t.equal(outputMsg.substring(19), 'Z [\x1b[1;33mwar\x1b[0m] kattbajs\n');
t.end();
});

test('log - Test error', t => {
const oldStderr = process.stderr.write;
let outputMsg = '';
const log = new utils.Log('silly');
process.stderr.write = msg => outputMsg = msg;
log.error('kattbajs');
process.stderr.write = oldStderr;
t.equal(outputMsg.substring(19), 'Z [\x1b[1;31merr\x1b[0m] kattbajs\n');
t.end();
});