<commit_before>import $RefParser from 'json-schema-ref-parser';

/* Redux forms with complex/nested data structures require
 * a specialized fields array to handle objects and arrays.
 * Eg: ['surveyId', 'surveys[].title', 'defaults.thankYouMessage']
 */
function processProperties(propsObj) {
    let formFields = [];

    Object.keys(propsObj).forEach(key => {
      switch (propsObj[key].type) {
        case "string":
          formFields = formFields.concat([key]);
          break;
        case "integer":
          formFields = formFields.concat([key]);
          break;
        case "object":
          let objectItems = processProperties(propsObj[key].properties);
          objectItems = objectItems.map(objectField => {
            return `${key}.${objectField}`
          });

          formFields = formFields.concat(objectItems);
          break;
        case "array":
          let arrayItems = propsObj[key].items.allOf;
          let arrayFields = [];
          arrayItems.forEach(item => {
            arrayFields = arrayFields.concat(processProperties(item.properties).map(arrayField => {
              return `${key}[].${arrayField}`;
            }));
          });

          formFields = formFields.concat(arrayFields);
          break;
      }
    });

    return formFields;
  };

export function parseJsonSchema(jsonSchema) {
  return $RefParser.dereference(jsonSchema);
}

export function generateFieldsFromSchema(jsonSchema) {
  return processProperties(jsonSchema.properties);
}

export {
  parseJsonSchema,
  generateFieldsFromSchema
}
<commit_msg>Order form fields based on propertyOrder value<commit_after>import $RefParser from 'json-schema-ref-parser';
import { _ } from 'underscore';

/* Redux forms with complex/nested data structures require
 * a specialized fields array to handle objects and arrays.
 * Eg: ['surveyId', 'surveys[].title', 'defaults.thankYouMessage']
 */
function processProperties(orderedPropNames, propsObj) {
  let reduxFormFields = [];
  let props = {};

  orderedPropNames.forEach(key => {
    switch (propsObj[key].type) {
      case "object":
        props = propsObj[key].properties;

        let objectFields = processProperties(orderPropNames(props), props)
          .map(objectField => `${key}.${objectField}`);

        reduxFormFields = reduxFormFields.concat(objectFields);
        break;

      case "array":
        (propsObj[key].items.allOf).forEach(item => {
          props = { ...item.properties, ...props };
        });

        let arrayFields = processProperties(orderPropNames(props), props)
          .map(arrayField => `${key}[].${arrayField}`);

        reduxFormFields = reduxFormFields.concat(arrayFields);
        break;

      default:
        reduxFormFields = reduxFormFields.concat([key]);
    }
  });

  return reduxFormFields;
};

function orderPropNames(propsObj) {
  return _.sortBy(Object.keys(propsObj), key => {
    return propsObj[key].propertyOrder;
  });
}

export function parseJsonSchema(jsonSchema) {
  return $RefParser.dereference(jsonSchema);
}

export function generateFieldsFromSchema(jsonSchema) {
  let props = jsonSchema.properties;
  return processProperties(orderPropNames(props), props);
}

export {
  parseJsonSchema,
  generateFieldsFromSchema
}

{'fraction_non_alphanumeric': 0.12415695892090742, 'fraction_numerical': 0.0, 'mean_word_length': 3.135614702154626, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 12, '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': '2495088', 'n_tokens_mistral': 899, 'n_tokens_neox': 890, 'n_words': 183}