Skip to content

Instantly share code, notes, and snippets.

@morellodev
Created February 20, 2021 10:52
Show Gist options
  • Select an option

  • Save morellodev/dbbd7573602ff14367424131e5416bf8 to your computer and use it in GitHub Desktop.

Select an option

Save morellodev/dbbd7573602ff14367424131e5416bf8 to your computer and use it in GitHub Desktop.
Functions to encode and decode URL safe Base64 strings
const objectToUrlSafeBase64 = (obj) => {
const stringifiedObject = JSON.stringify(obj);
const base64String = btoa(stringifiedObject);
return encodeURIComponent(base64String);
};
const urlSafeBase64ToObject = (urlSafeBase64String) => {
const base64String = decodeURIComponent(urlSafeBase64String);
const stringifiedObject = atob(base64String);
return JSON.parse(stringifiedObject);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment