Skip to content

Instantly share code, notes, and snippets.

@MelodicCrypter
Last active August 8, 2019 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MelodicCrypter/5f8d669d70cca844d50724c69fde9844 to your computer and use it in GitHub Desktop.
Save MelodicCrypter/5f8d669d70cca844d50724c69fde9844 to your computer and use it in GitHub Desktop.
A small javascript utility that will generate unique (hashed-type) strings.
// Your unique variables here
const allCapsAlpha = [..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
const allLowerAlpha = [..."abcdefghijklmnopqrstuvwxyz"];
const allUniqueChars = [...'~!@#$%^&*()_+`-=[]\\{}|;:\'",./<>?'];
const allNumbers = [..."0123456789"];
// This is the 'pattern', alternate the order if you want.
// The final output (order of pattern) will not be the same though,
// but you can improve this utility to achieve that.
const base = [...allCapsAlpha, ...allNumbers, ...allLowerAlpha, ...allUniqueChars];
// The generator()
// base = the pattern you want to generate from
// len = the length of the output strings
const generator = (base, len) => {
return [...Array(len)]
.map(i => base[Math.random()*base.length|0])
.join('');
};
// Test
console.log('OUTPUT: ', generator(base, 28));
// OUTPUT: )ne]iGl'XBX%(FPCU:4JRnV'#mS=
// OUTPUT: kRY.}PZ~]Q|LQ1;qJ1Gnh>,zC{T@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment