Skip to content

Instantly share code, notes, and snippets.

@anztrax
Created July 2, 2024 06:01
Show Gist options
  • Save anztrax/3d5bff82787651a2a89a46ab819abbf3 to your computer and use it in GitHub Desktop.
Save anztrax/3d5bff82787651a2a89a46ab819abbf3 to your computer and use it in GitHub Desktop.
Encoder and Decoder Utils
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