<commit_before>const isBodyTextual = require('./isBodyTextual');
const trimHeaders = require('./trimHeaders');

function trimMessage(messyObjOrError) {
  if (Object.prototype.toString.call(messyObjOrError) === '[object Error]') {
    return messyObjOrError;
  }

  const messyObj = messyObjOrError;
  const isTextualBody = isBodyTextual(messyObj, messyObj.body);
  const message = messyObjOrError.toJSON();
  const output = {};

  if (message.url && message.method) {
    output.url = `${message.method} ${message.url}`;
  }

  if (typeof message.statusCode === 'number' && message.statusCode !== 200) {
    output.statusCode = message.statusCode;
  }

  if (message.headers) {
    trimHeaders(message);
    if (Object.keys(message.headers).length > 0) {
      output.headers = message.headers;
    }
  }

  if (typeof message.body !== 'undefined') {
    if (isTextualBody) {
      output.body = message.body.toString('utf-8');

      if (
        /^application\/json(?:;|$)|\+json\b/.test(
          message.headers['Content-Type']
        ) &&
        /^\s*[[{]/.test(message.body)
      ) {
        try {
          output.body = JSON.parse(message.body);
          if (message.headers['Content-Type'] === 'application/json') {
            delete message.headers['Content-Type'];
          }
        } catch (e) {}
      }
    } else if (message.body.length > 0) {
      output.body = message.body;
    }
  }

  if (Object.keys(output).length === 1 && !output.body) {
    if (typeof message.url === 'string') {
      return message.url;
    }
    if (typeof message.statusCode === 'number') {
      return message.statusCode;
    }
  }

  if (messyObj.host) {
    output.host = messyObj.host;
  }

  if (messyObj.port) {
    output.port = messyObj.port;
  }

  return output;<commit_msg>Improve variable naming within the formatter.<commit_after>const isBodyTextual = require('./isBodyTextual');
const trimHeaders = require('./trimHeaders');

function trimMessage(messageOrError) {
  if (Object.prototype.toString.call(messageOrError) === '[object Error]') {
    return messageOrError;
  }

  const message = messageOrError;
  // NOTE: this must be checked first as further operations can affect the buffer
  const isTextualBody = isBodyTextual(message, message.body);
  const serialisedMessage = message.toJSON();
  const output = {};

  if (serialisedMessage.url && serialisedMessage.method) {
    output.url = `${serialisedMessage.method} ${serialisedMessage.url}`;
  }

  if (
    typeof serialisedMessage.statusCode === 'number' &&
    serialisedMessage.statusCode !== 200
  ) {
    output.statusCode = serialisedMessage.statusCode;
  }

  if (serialisedMessage.headers) {
    trimHeaders(serialisedMessage);
    if (Object.keys(serialisedMessage.headers).length > 0) {
      output.headers = serialisedMessage.headers;
    }
  }

  if (typeof serialisedMessage.body !== 'undefined') {
    if (isTextualBody) {
      output.body = serialisedMessage.body.toString('utf-8');

      if (
        /^application\/json(?:;|$)|\+json\b/.test(
          serialisedMessage.headers['Content-Type']
        ) &&
        /^\s*[[{]/.test(serialisedMessage.body)
      ) {
        try {
          output.body = JSON.parse(serialisedMessage.body);
          if (
            serialisedMessage.headers['Content-Type'] === 'application/json'
          ) {
            delete serialisedMessage.headers['Content-Type'];
          }
        } catch (e) {}
      }
    } else if (serialisedMessage.body.length > 0) {
      output.body = serialisedMessage.body;
    }
  }

  if (Object.keys(output).length === 1 && !output.body) {
    if (typeof serialisedMessage.url === 'string') {
      return serialisedMessage.url;
    }
    if (typeof serialisedMessage.statusCode === 'number') {
      return serialisedMessage.statusCode;
    }
  }

  if (message.host) {
    output.host = message.host;
  }

  if (message.port) {
    output.port = message.port;
  }

  return output;
{'fraction_non_alphanumeric': 0.14852235412983075, 'fraction_numerical': 0.0035362465269007325, 'mean_word_length': 2.8187078109932497, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '944142', 'n_tokens_mistral': 1245, 'n_tokens_neox': 1199, 'n_words': 221}