Skip to content

Instantly share code, notes, and snippets.

@midknightmare666
Created October 30, 2019 02:22
Show Gist options
  • Save midknightmare666/b32111b5976480518d4493ecc1b03b02 to your computer and use it in GitHub Desktop.
Save midknightmare666/b32111b5976480518d4493ecc1b03b02 to your computer and use it in GitHub Desktop.
/**
* Creates a short id from a string of numbers.
*
* @param {String} inputNumber The number to hash
* @return {String} hash The hashed string
*/
module.exports = (inputNumber) => {
let hash = '';
const alphabet = '23456789abcdefghijklmnopqrstuvwxyz';
const length = alphabet.length;
do {
hash = alphabet[inputNumber % length] + hash;
inputNumber = parseInt(inputNumber / length, 10);
} while (inputNumber);
return hash;
};
/* Usage And Expected Output: */
const generate = require('hash');
const id = generate('394540642987933707');
console.log(id)
// => 7n62wxauu926
/* append Date.now() to make the id more unique */
const input = '394540642987933707' + Date.now();
const idWithDate = generate(input);
console.log(idWithDate)
// => 7n63sv69jcce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment