Created
October 30, 2019 02:22
-
-
Save midknightmare666/b32111b5976480518d4493ecc1b03b02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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