all files / blackbird/modules/utils/ stringifyMediaValue.js

93.33% Statements 14/15
66.67% Branches 8/12
100% Functions 1/1
93.33% Lines 14/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42                          30×   30×   30× 28×     30× 30×   30×               30×      
/**
 * Creates a string from an object containing a media value. This object may
 * have properties containing the type, subtype, and parameters.
 *
 *   stringifyMediaValue({ type: 'text', subtype: 'html', params: { level: '2', q: '0.4' } }) =>
 *     "text/html;level=2;q=0.4"
 *
 *   stringifyMediaValue({ type: 'en', subtype: 'gb', params: { q: '0.8' } }, "-") =>
 *     "en-gb;q=0.8"
 *
 *   stringifyMediaValue({ type: 'unicode-1-1', params: { q: '0.8' } }) =>
 *     "unicode-1-1;q=0.8"
 */
const R = require("ramda");
function stringifyMediaValue(value, typeSeparator) {
    typeSeparator = typeSeparator || "/";
 
    let string = value.type || "*";
 
    if (value.subtype) {
        string += typeSeparator + value.subtype;
    }
 
    Eif (value.params) {
        const params = value.params;
 
        for (const paramName in params) {
            Eif (params.hasOwnProperty(paramName)) {
                string += `;${paramName}`;
 
                Iif (R.isNil(params[paramName])) {
                    string += `=${params[paramName]}`;
                }
            }
        }
    }
 
    return string;
}
 
module.exports = stringifyMediaValue;