Sha256: f245d634638e9e75e9d01b1846681ab72036e760df5116c09092d3bf8307f02f

Contents?: true

Size: 1.52 KB

Versions: 35

Compression:

Stored size: 1.52 KB

Contents

'use strict';

/**
 * Module exports.
 */

exports.parse = parse;
exports.format = format;

/**
 * Parse extensions header value
 */

function parse (value) {
  value = value || '';

  var extensions = {};

  value.split(',').forEach(function (v) {
    var params = v.split(';');
    var token = params.shift().trim();
    var paramsList = extensions[token] = extensions[token] || [];
    var parsedParams = {};

    params.forEach(function (param) {
      var parts = param.trim().split('=');
      var key = parts[0];
      var value = parts[1];
      if (value === undefined) {
        value = true;
      } else {
        // unquote value
        if (value[0] === '"') {
          value = value.slice(1);
        }
        if (value[value.length - 1] === '"') {
          value = value.slice(0, value.length - 1);
        }
      }
      (parsedParams[key] = parsedParams[key] || []).push(value);
    });

    paramsList.push(parsedParams);
  });

  return extensions;
}

/**
 * Format extensions header value
 */

function format (value) {
  return Object.keys(value).map(function (token) {
    var paramsList = value[token];
    if (!Array.isArray(paramsList)) {
      paramsList = [paramsList];
    }
    return paramsList.map(function (params) {
      return [token].concat(Object.keys(params).map(function (k) {
        var p = params[k];
        if (!Array.isArray(p)) p = [p];
        return p.map(function (v) {
          return v === true ? k : k + '=' + v;
        }).join('; ');
      })).join('; ');
    }).join(', ');
  }).join(', ');
}

Version data entries

35 entries across 35 versions & 2 rubygems

Version Path
opal-1.6.1 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.6.0 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.6.0.rc1 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.6.0.alpha1 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.5.1 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.5.0 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.5.0.rc1 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.4.1 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.4.0 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.4.0.alpha1 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.3.2 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.3.1 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.3.0 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.3.0.rc1 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.3.0.alpha1 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.2.0 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.2.0.beta1 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.1.1 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.1.1.rc1 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js
opal-1.1.0 lib/opal/cli_runners/node_modules/ws/lib/Extensions.js