Skip to content

Instantly share code, notes, and snippets.

@jhurliman
Created January 5, 2015 22:33
Show Gist options
  • Save jhurliman/e0e94b3a6add945d1ee1 to your computer and use it in GitHub Desktop.
Save jhurliman/e0e94b3a6add945d1ee1 to your computer and use it in GitHub Desktop.
Generate Firebase IDs using the same approach as Firebase.push()
var ALPHABET = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
function generateFirebaseID() {
var timestamp = +new Date();
var chars = Array(20);
for (var i = 7; 0 <= i; i--) {
chars[i] = ALPHABET.charAt(timestamp % 64);
timestamp = Math.floor(timestamp / 64);
}
for (var j = 0; 12 > j; j++) {
chars[8 + j] = ALPHABET.charAt(Math.floor(64 * Math.random()));
}
return chars.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment