X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fcommon.js;h=f199b4947ed1ba537a29350c617322cf64678512;hb=30851a8cb9f8823b1b395ace8f53d62c5c53abd8;hp=cfdf9db7e5c47feda5e05b52624a17a25949d853;hpb=1c4bb7e3bbdc1121ceba373c5be4459521197155;p=squeep-indieauth-helper diff --git a/lib/common.js b/lib/common.js index cfdf9db..f199b49 100644 --- a/lib/common.js +++ b/lib/common.js @@ -17,8 +17,8 @@ const fileScope = (filename) => { /** * Pick out useful axios response fields. - * @param {*} res - * @returns + * @param {AxiosResponse} res + * @returns {Object} */ const axiosResponseLogData = (res) => { const data = pick(res, [ @@ -64,9 +64,56 @@ const pick = (obj, props) => { }; +/** + * Return a set containing non-shared items between two sets. + * @param {Set} a + * @param {Set} b + * @returns {Set} + */ +const setSymmetricDifference = (a, b) => { + const d = new Set(a); + for (const x of b) { + if (d.has(x)) { + d.delete(x); + } else { + d.add(x); + } + } + return d; +}; + + +/** + * URL objects have weird names. + * @param {String} component + * @returns {String} + */ +const properURLComponentName = (component) => { + // eslint-disable-next-line security/detect-object-injection + return { + hash: 'fragment', + protocol: 'scheme', + }[component] || component; +} + + +/** + * Encodes single-level object as form data string. + * @param {Object} data + */ +const formData = (data) => { + const formData = new URLSearchParams(); + Object.entries(data).forEach(([name, value]) => formData.set(name, value)); + return formData.toString(); +}; + + module.exports = { fileScope, axiosResponseLogData, logTruncate, pick, + setSymmetricDifference, + properURLComponentName, + formData, }; \ No newline at end of file