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: 92c70803e6e0c0da52c02cad274c32c3772713d5
Choose a base ref
...
head repository: larvit/larvitutils
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: cf356385880994571092852e8a309d9ee99f25a4
Choose a head ref
  • 1 commit
  • 5 files changed
  • 1 contributor

Commits on Sep 16, 2019

  1. Copy the full SHA
    cf35638 View commit details
Showing with 82 additions and 22 deletions.
  1. +31 −8 index.js
  2. +1 −1 package-lock.json
  3. +1 −1 package.json
  4. +49 −0 test/hashStringToString.js
  5. +0 −12 test/hashUnsignedIntFromString.js
39 changes: 31 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -159,20 +159,43 @@ Utils.prototype.Log = function Log(options) {
};

/**
* Genereates a string into an unsigned int
* Hash string into another string (A-Z, 0-9)
*
* @param {string} str The string to convert
* @return {number} the hashed value
* @param {string} str The string to hash
* @param {string} length Length of result string
* @return {string} The hashed result string
*/
Utils.prototype.hashUnsignedIntFromString = function hashUnsignedIntFromString(str) {
let hash = 0;
Utils.prototype.hashStringToString = function hashStringToString(str, length) {
if (length === 0) return '';

if (str.length === 0) return hash;
const base = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const resultIndices = new Array(length);

// Generate an index salt so that similar input strings will give different result strings
let indexSalt = 0;
for (let i = 0; i < str.length; i++) {
hash += (1 << str.charCodeAt(i)) >>> 0;
indexSalt += (1 << str.charCodeAt(i)) >>> 0;
}

if (str.length >= length) {
// Input string is longer or same length as desired result string length
for (let i = 0; i < str.length; ++i) {
const resultIndex = i % length;
const valueIndex = Math.abs(((resultIndices[resultIndex] ? resultIndices[resultIndex] : 0) + ((1 << str.charCodeAt(i))) + indexSalt + i) % base.length);
resultIndices[resultIndex] = valueIndex;
}
} else {
// Result string string is longer than the input string
for (let i = 0; i < resultIndices.length; ++i) {
const resultIndex = i;
const valueIndex = Math.abs((((1 << str.charCodeAt(i % str.length)) * i) + indexSalt) % base.length);
resultIndices[resultIndex] = valueIndex;
}
}

return hash;
const result = resultIndices.map(x => base[x]).join('');

return result;
};

Utils.prototype.Log.prototype.stdout = function stdout(lvl, msg) {
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.1",
"version": "2.3.2",
"description": "Misc utils",
"main": "index.js",
"devDependencies": {
49 changes: 49 additions & 0 deletions test/hashStringToString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

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

test('hashStringToString() - Hash string to string of length 0', t => {
t.equal(utils.hashStringToString('asdf', 0), '');
t.end();
});

test('hashStringToString() - Hash string to string of length 1', t => {
t.equal(utils.hashStringToString('asdf', 1).length, 1);
t.end();
});

test('hashStringToString() - Hash string to string of length 2', t => {
t.equal(utils.hashStringToString('asdf', 2).length, 2);
t.end();
});

test('hashStringToString() - Hash string to string give the same output when input is the same', t => {
const first = utils.hashStringToString('asdf', 1);

t.equal(utils.hashStringToString('asdf', 1), first);
t.equal(utils.hashStringToString('asdf', 1), first);
t.equal(utils.hashStringToString('asdf', 1), first);
t.equal(utils.hashStringToString('asdf', 1), first);
t.end();
});

test('hashStringToString() - Hash string to string where result string length is longer than input string length', t => {
t.equal(utils.hashStringToString('nisse', 20).length, 20);
t.end();
});

test('hashStringToString() - Hash similar string should give different results', t => {
t.notEqual(utils.hashStringToString('nisse1', 6), utils.hashStringToString('nisse2', 6));
utils.hashStringToString('pisse1', 6);
t.end();
});

test('hashStringToString() - Hash string where all characters are the same should result in string where all characters are not the same', t => {
const result = utils.hashStringToString('aaaaa', 6);
const resultAsArray = result.split('');

t.notEqual(resultAsArray.filter(x => x === resultAsArray[0]).length, resultAsArray.length);

t.end();
});
12 changes: 0 additions & 12 deletions test/hashUnsignedIntFromString.js

This file was deleted.