Created
January 5, 2015 22:33
-
-
Save jhurliman/e0e94b3a6add945d1ee1 to your computer and use it in GitHub Desktop.
Generate Firebase IDs using the same approach as Firebase.push()
This file contains hidden or 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
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