Created
July 2, 2024 06:01
-
-
Save anztrax/3d5bff82787651a2a89a46ab819abbf3 to your computer and use it in GitHub Desktop.
Encoder and Decoder Utils
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
import isEmpty from "lodash/isEmpty"; | |
class EncoderDecoderUtils{ | |
constructor() { | |
} | |
encodeBase64(data: string): string{ | |
return Buffer.from(data).toString('base64'); | |
} | |
decodeBase64(data: string): string{ | |
return Buffer.from(data, 'base64').toString('utf8'); | |
} | |
isValidBase64(data: string): boolean{ | |
if (isEmpty(data)){ | |
return false; | |
} | |
try { | |
const decoded1 = Buffer.from(data, 'base64').toString('utf8'); | |
const encoded2 = Buffer.from(decoded1, 'binary').toString('base64'); | |
return data === encoded2; | |
} catch (err) { | |
return false; | |
} | |
} | |
} | |
const encoderDecoderUtils = new EncoderDecoderUtils(); | |
export default encoderDecoderUtils; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment