Skip to content

Commit

Permalink
common: typo
Browse files Browse the repository at this point in the history
  • Loading branch information
josephjclark committed Jan 16, 2025
1 parent 70d389a commit 2bdd108
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/common/src/util/base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ import _ from 'lodash';
* @example <caption>To skip the JSON stringification step</caption>
* const encodedObject = encode('Hello World', {parseJson: false})
*/
export const encode = (data, options = {parseJson: true}) => {
export const encode = (data, options = { parseJson: true }) => {
let str = data;

if(typeof data !== "string" && options.parseJson){
if (typeof data !== 'string' && options.parseJson) {
try {
str = JSON.stringify(str);
} catch (e){
} catch (e) {
console.log(e.message);
}
}

return Buffer.from(str, 'utf-8').toString('base64');
}
};

/**4
/**
* Decodes a Base64 encoded string back to its original format.
* @function
* @public
Expand All @@ -47,10 +47,13 @@ export const encode = (data, options = {parseJson: true}) => {
* @example <caption>To skip the JSON stringification step</caption>
* const decodedString = decode('Hello World', {parseJson: false})
*/
export const decode = (base64Data, options = {parseJson: true}) =>{
export const decode = (base64Data, options = { parseJson: true }) => {
let decodedValue = Buffer.from(base64Data, 'base64').toString('utf-8');

if((_.startsWith(decodedValue, '[') || _.startsWith(decodedValue, '{')) && options.parseJson) {
if (
(_.startsWith(decodedValue, '[') || _.startsWith(decodedValue, '{')) &&
options.parseJson
) {
try {
decodedValue = JSON.parse(decodedValue);
} catch (e) {
Expand All @@ -59,5 +62,4 @@ export const decode = (base64Data, options = {parseJson: true}) =>{
}

return decodedValue;
}

};

0 comments on commit 2bdd108

Please sign in to comment.