Created
December 26, 2019 21:41
-
-
Save huntercaron/bb25d8bc2603108da583c03de3db0b4e to your computer and use it in GitHub Desktop.
Util functions from FirebaseData tutorial files
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
const hashCode = str => | |
str | |
.split("") | |
.reduce((hash, val) => ((hash << 5) - hash + val.charCodeAt(0)) | 0, 0) | |
export const isChrome = | |
/Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor) | |
export const isSafari = | |
/Safari/.test(navigator.userAgent) && | |
/Apple Computer/.test(navigator.vendor) | |
const storageKey = "$$FirebaseDataChat" | |
function generateUserID() { | |
//@ts-ignore | |
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, a => | |
(a ^ ((Math.random() * 16) >> (a / 4))).toString(16) | |
) | |
} | |
const createNewUserID = () => { | |
const newID = generateUserID() | |
localStorage.setItem(storageKey, JSON.stringify(newID)) | |
return newID | |
} | |
export const getUserID = () => | |
localStorage.getItem(storageKey) | |
? JSON.parse(localStorage.getItem(storageKey)) | |
: createNewUserID() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment