Skip to content

Instantly share code, notes, and snippets.

@aralroca
Last active October 7, 2022 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aralroca/d3983fa0ca725db2042648189020fc1f to your computer and use it in GitHub Desktop.
Save aralroca/d3983fa0ca725db2042648189020fc1f to your computer and use it in GitHub Desktop.
const isNode = typeof window === 'undefined'
// Regex from: https://stackoverflow.com/a/475217/4467741
const encodedRegex = new RegExp('^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$')
export function decode(text: string) {
return isNode ? Buffer.from(text, 'base64').toString() : atob(text)
}
export function encode(text: string) {
return isNode ? Buffer.from(text).toString('base64') : btoa(text)
}
export function isEncoded(text: string) {
return encodedRegex.test(text)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment