Last active
April 23, 2025 11:56
-
-
Save daverickdunn/8e9ba9351e4496ba69efa9214821c785 to your computer and use it in GitHub Desktop.
Base64 URL Encode Decode Typescript
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
export function encode(unencoded: string) { | |
return Buffer.from(unencoded).toString('base64'); | |
}; | |
export function decode(encoded: string) { | |
return Buffer.from(encoded, 'base64').toString('utf8'); | |
}; | |
export function urlEncode(unencoded: string) { | |
let encoded = encode(unencoded); | |
return encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, ''); | |
}; | |
export function urlDecode(encoded: string) { | |
let decoded = encoded.replace(/-/g, '+').replace(/_/g, '/'); | |
while (decoded.length % 4) decoded += '='; | |
return decode(decoded); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment