<commit_before>const isSupportedEngine = {
  gm: true,
  sharp: true,
  pngcrush: true,
  pngquant: true,
  jpegtran: true,
  optipng: true,
  svgfilter: true,
  inkscape: true
};

const resizeOptions = {
  ignoreAspectRatio: true,
  withoutEnlargement: true
};

const queryStringEngineAndArgsRegex = new RegExp(
  `^(${Object.keys(isSupportedEngine).join('|')})=(.*)`
);

module.exports = function prepareQueryString(queryString) {
  const keyValuePairs = queryString.split('&');
  const queryStringFragments = [];

  let hasResize = false;
  let optionToResize;

  for (const pair of keyValuePairs) {
    let m;

    if ((m = pair.match(queryStringEngineAndArgsRegex)) !== null) {
      const [, engineName, engineArgs] = m;
      const result = [engineName];
      const remaining = engineArgs.split(',');

      let isEngineOptions = false;
      let lastSeenOptionIndex = -1;
      let engineOptions;

      remaining.forEach((bit, index) => {
        if (engineName === 'svgfilter' && bit[0] === '-' && bit[1] === '-') {
          if (!isEngineOptions) {
            isEngineOptions = true;
            engineOptions = [];
          }
          engineOptions.push(bit.slice(2));
        } else if (bit[0] === '-') {
          result.push(bit.slice(1));
          lastSeenOptionIndex = index + 1; // account for the engine entry
        } else if (engineName === 'pngquant') {
          result.push(`speed=${bit}`);
        } else if (lastSeenOptionIndex > -1) {
          result[lastSeenOptionIndex] += `=${bit}`;
        }
      });

      if (isEngineOptions) {
        result[0] += `=${engineOptions.join('+')}`;
      }

      queryStringFragments.push(...result);
    } else {
      if (pair.startsWith('setFormat=')) {
        let format = pair.slice(10).toLowerCase();
        if (format === 'jpg') {
          format = 'jpeg';
        }
        queryStringFragments.push(format);
      } else if (pair in resizeOptions && !hasResize) {
        optionToResize = pair;
      } else {
        let fragment = pair;
        if (pair.startsWith('resize=') && pair.indexOf(',') === -1) {<commit_msg>Use for-of instead of .forEach() in newly added code.<commit_after>const isSupportedEngine = {
  gm: true,
  sharp: true,
  pngcrush: true,
  pngquant: true,
  jpegtran: true,
  optipng: true,
  svgfilter: true,
  inkscape: true
};

const resizeOptions = {
  ignoreAspectRatio: true,
  withoutEnlargement: true
};

const queryStringEngineAndArgsRegex = new RegExp(
  `^(${Object.keys(isSupportedEngine).join('|')})=(.*)`
);

module.exports = function prepareQueryString(queryString) {
  const keyValuePairs = queryString.split('&');
  const queryStringFragments = [];

  let hasResize = false;
  let optionToResize;

  for (const pair of keyValuePairs) {
    let m;

    if ((m = pair.match(queryStringEngineAndArgsRegex)) !== null) {
      const [, engineName, engineArgs] = m;
      const result = [engineName];
      const remaining = engineArgs.split(',');

      let isEngineOptions = false;
      let lastSeenOptionIndex = -1;
      let engineOptions;

      for (const [index, bit] of remaining.entries()) {
        if (engineName === 'svgfilter' && bit[0] === '-' && bit[1] === '-') {
          if (!isEngineOptions) {
            isEngineOptions = true;
            engineOptions = [];
          }
          engineOptions.push(bit.slice(2));
        } else if (bit[0] === '-') {
          result.push(bit.slice(1));
          lastSeenOptionIndex = index + 1; // account for the engine entry
        } else if (engineName === 'pngquant') {
          result.push(`speed=${bit}`);
        } else if (lastSeenOptionIndex > -1) {
          result[lastSeenOptionIndex] += `=${bit}`;
        }
      }

      if (isEngineOptions) {
        result[0] += `=${engineOptions.join('+')}`;
      }

      queryStringFragments.push(...result);
    } else {
      if (pair.startsWith('setFormat=')) {
        let format = pair.slice(10).toLowerCase();
        if (format === 'jpg') {
          format = 'jpeg';
        }
        queryStringFragments.push(format);
      } else if (pair in resizeOptions && !hasResize) {
        optionToResize = pair;
      } else {
        let fragment = pair;
        if (pair.startsWith('resize=') && pair.indexOf(',') === -1) {
{'fraction_non_alphanumeric': 0.15619136960600374, 'fraction_numerical': 0.005628517823639775, 'mean_word_length': 2.5512073272273104, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '5692754', 'n_tokens_mistral': 1343, 'n_tokens_neox': 1333, 'n_words': 292}