X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=src%2Fcommon.js;h=55a0807b00757e2c2fa0fe8d50e7c22701b0bacf;hb=ad5e19413b7f003abc61d12a5ff9dbcf20065c65;hp=0cacc3bf086f2b57f0b10db1ef289c6be7961e15;hpb=38aba0869dc3ade99d439e74cbc6239b4fa1f632;p=websub-hub diff --git a/src/common.js b/src/common.js index 0cacc3b..55a0807 100644 --- a/src/common.js +++ b/src/common.js @@ -26,6 +26,22 @@ const validHash = (algorithm) => getHashes() .filter((h) => h.match(/^sha[0-9]+$/)) .includes(algorithm); + +/** + * Return an array containing x if x is not an array. + * @param {*} x + */ +const ensureArray = (x) => { + if (x === undefined) { + return []; + } + if (!Array.isArray(x)) { + return Array(x); + } + return x; +}; + + /** * Recursively freeze an object. * @param {Object} o @@ -140,6 +156,7 @@ module.exports = { arrayChunk, attemptRetrySeconds, axiosResponseLogData, + ensureArray, freezeDeep, logTruncate, randomBytesAsync,