X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fcommon.js;h=1d4223b1749d1cce37b729233f988098abc36b5a;hb=9b31aca67862c4d3b5dd2811dfce97c51e4cdba5;hp=fb3ec979524e668bfc5b85dded22a42c56117f41;hpb=044615f53bacdc366b44941218d808c549607469;p=squeep-mystery-box diff --git a/lib/common.js b/lib/common.js index fb3ec97..1d4223b 100644 --- a/lib/common.js +++ b/lib/common.js @@ -20,49 +20,22 @@ const fileScope = (filename) => { /** - * Convert Base64 to Base64URL. - * @param {String} input - * @returns {String} + * Return an array containing x if x is something and not an array + * @param {*} x */ -const base64ToBase64URL = (input) => { - return input - .replace(/=/g, '') - .replace(/\+/g, '-') - .replace(/\//g, '_'); -}; - - -/** - * Convert Base64URL to normal Base64. - * @param {String} input - * @returns {String} - */ -const base64URLToBase64 = (input) => { - return base64RePad(input) - .replace(/-/g, '+') - .replace(/_/, '/'); -}; - - -/** - * Add any missing trailing padding which may have been removed from Base64URL encoding. - * @param {String} input - */ -const base64RePad = (input) => { - const blockSize = 4; - const lastBlockSize = input.length % blockSize; - if (lastBlockSize) { - const missing = blockSize - lastBlockSize; - return input + '='.repeat(missing); +const ensureArray = (x) => { + if (x === undefined) { + return []; + } + if (!Array.isArray(x)) { + return Array(x); } - return input; + return x; }; module.exports = { - base64ToBase64URL, - base64URLToBase64, - base64RePad, + ensureArray, fileScope, randomBytesAsync, }; \ No newline at end of file