Last active
January 12, 2021 15:14
-
-
Save Stas-Buzunko/b4f2ed1dc122cdb1867e13a731fc5dcf to your computer and use it in GitHub Desktop.
retrieve timestamp from firebase database key
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
// this is how firebase generates keys | |
// https://github.com/firebase/firebase-js-sdk/blob/master/packages/database/src/core/util/NextPushId.ts | |
const decodeFirebaseKey = key => { | |
// chars firebase use for generating keys | |
const CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'; | |
let timestamp = 0 | |
// we only need to decode first 8 symbols | |
for (let i = 0; i <= 7; i++) { | |
const index = CHARS.indexOf(key[i]) | |
timestamp = timestamp * 64 + index | |
} | |
return timestamp | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment