Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Created May 15, 2021 21:30
Show Gist options
  • Select an option

  • Save DavidWells/f14c29cc5f45eaffcb8d9952be4dcf3f to your computer and use it in GitHub Desktop.

Select an option

Save DavidWells/f14c29cc5f45eaffcb8d9952be4dcf3f to your computer and use it in GitHub Desktop.
Base 64 utils
// via https://github.com/idleberg/web-porridge/blob/7d43f9cd01a2e7ad902b0ce37045e38a187fe118/src/util.ts
/**
* Checks whether input data requires deserialization after reading it from WebStorage
* @param {*} inputData
* @returns {boolean}
*/
function maybeDeserialize(inputData): boolean {
const serializables = [
'[object Array]',
'[object Boolean]',
'[object Null]',
'[object Object]'
];
try {
const result = JSON.parse(inputData);
const type: string = Object.prototype.toString.call(result);
return serializables.includes(type) || (type === '[object Number]' && isSerializableNumber(inputData));
} catch (error) {
return false;
}
}
/**
* Checks whether input data requires serialization prior to writing it to WebStorage
* @param {*} inputString
* @returns {boolean}
*/
function maybeSerialize(inputString: string | Object): boolean {
const serializables = [
'[object Array]',
'[object Boolean]',
'[object Null]',
'[object Number]',
'[object Object]'
];
const type: string = Object.prototype.toString.call(inputString);
return serializables.includes(type);
}
/**
* Base64-decodes input data if necessary. Supports deserialization
* @param {string} inputString
* @param {object} options
* @returns {string|Object}
*/
function maybeBase64Decode(inputString: string, options: WebPorridgeOptions = {}) {
const outputString: string = isString(inputString) && isBase64(inputString) ? base64Decode(inputString) : inputString;
return (outputString && maybeDeserialize(outputString) && options.json) ? JSON.parse(outputString) : outputString;
}
/**
* Base64-encodes input string. Supports serialization
* @param {*} inputString
* @returns {string}
*/
function base64Encode(inputString: string): string {
const outputString: string = (maybeSerialize(inputString)) ? JSON.stringify(inputString) : inputString;
return Buffer.from(outputString).toString('base64');
}
/**
* Base64-decodes input string
* @param {*} inputString
* @returns {string}
*/
function base64Decode(inputString: string): string {
return Buffer.from(inputString, 'base64').toString('binary');
}
/**
* Determines whether a string is Base64 encoded
* @param {*} inputString
* @returns {boolean}
*/
function isBase64(inputString: string) {
const base64RegEx = '(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\/]{3}=)?';
return new RegExp(`^${base64RegEx}$`, 'gi').test(inputString);
}
/**
* Checks for supported WebStorage methods
* @param {string} action
* @returns {boolean}
*/
function validateAction(action: string) {
if (![
'clear',
'getItem',
'getItems',
'key',
'length',
'removeItem',
'removeItems',
'setItem',
'setItems',
].includes(action)) {
throw 'Invalid action argument provided';
}
}
/**
* Detect whether input is of type Array
* @param {*} inputData
* @returns {boolean}
*/
function isArray(inputData) {
return Object.prototype.toString.call(inputData) === '[object Array]';
}
/**
* Detect whether input is of type Object
* @param {*} inputData
* @returns {boolean}
*/
function isObject(inputData) {
return Object.prototype.toString.call(inputData) === '[object Object]';
}
/**
* Detect whether input is of type String
* @param {*} inputData
* @returns {boolean}
*/
function isString(inputData) {
return Object.prototype.toString.call(inputData) === '[object String]';
}
/**
* Determines whether a floating-point number can be safely serialized
* @param {*} inputData
* @returns {boolean}
*/
function isSerializableNumber(inputData) {
return !isNaN(parseFloat(inputData)) && parseFloat(inputData.toString()).toString() === inputData.toString();
}
export {
base64Decode,
base64Encode,
isArray,
isObject,
isSerializableNumber,
maybeBase64Decode,
maybeDeserialize,
maybeSerialize,
validateAction
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment