X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fcommon.js;h=1d4223b1749d1cce37b729233f988098abc36b5a;hb=9932160785c38ea561d361ffacf1dd515f529a6b;hp=a47226f6e76fe355416380f3a77db24450481783;hpb=383899395bf599359d60337edb651af0592f82cb;p=squeep-mystery-box diff --git a/lib/common.js b/lib/common.js index a47226f..1d4223b 100644 --- a/lib/common.js +++ b/lib/common.js @@ -20,55 +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) => { - if (!input) { - return input; +const ensureArray = (x) => { + if (x === undefined) { + return []; } - return input - .replace(/=/g, '') - .replace(/\+/g, '-') - .replace(/\//g, '_'); -}; - - -/** - * Convert Base64URL to normal Base64. - * @param {String} input - * @returns {String} - */ -const base64URLToBase64 = (input) => { - if (!input) { - return 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); + 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