Skip to content

Instantly share code, notes, and snippets.

@Stas-Buzunko
Last active January 12, 2021 15:14
Show Gist options
  • Save Stas-Buzunko/b4f2ed1dc122cdb1867e13a731fc5dcf to your computer and use it in GitHub Desktop.
Save Stas-Buzunko/b4f2ed1dc122cdb1867e13a731fc5dcf to your computer and use it in GitHub Desktop.
retrieve timestamp from firebase database key
// 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