<commit_before>'use strict';

const { underscored } = require('underscore.string');
const { singular, plural } = require('pluralize');

const { throwError } = require('../../../../error');

const {
  getTopLevelAction,
  isTopLevelAction,
  getModel,
  getActionConstant,
} = require('./utilities');

// Add `action.actionConstant` and `action.modelName`
const parseModels = function ({ actions, modelsMap }) {
  const topLevelAction = parseTopLevelAction({ actions, modelsMap });
  const nestedActions = parseNestedActions({
    actions,
    modelsMap,
    topLevelAction,
  });
  return [topLevelAction, ...nestedActions];
};

// Parse a GraphQL query top-level action name into tokens.
// E.g. `findMyModels` -> { actionType: 'find', modelName: 'my_models' }
const parseTopLevelAction = function ({ actions, modelsMap }) {
  const topLevelAction = getTopLevelAction({ actions });
  const { actionPath: [actionName] } = topLevelAction;

  const { actionType, modelName } = parseName({ actionName });

  const modelNameA = underscored(modelName);

  const singularName = singular(modelNameA);
  const pluralName = plural(modelNameA);
  const isArray = modelNameA === pluralName;

  const modelNameB = modelsMap[pluralName] ? pluralName : singularName;

  const actionConstant = getActionConstant({ actionType, isArray });

  validateTopLevel({ modelName: modelNameB, actionName });

  return { ...topLevelAction, actionConstant, modelName: modelNameB };
};

const parseName = function ({ actionName }) {
  const [, actionType, modelName = ''] = nameRegExp.exec(actionName) || [];
  return { actionType, modelName };
};

// Matches e.g. 'find_my_models' -> ['find', 'my_models'];
const nameRegExp = /^([a-z0-9]+)_([a-z0-9_]*)/;

const validateTopLevel = function ({ modelName, actionName }) {
  if (modelName) { return; }

  const message = `Action '${actionName}' is unknown`;
  throwError(message, { reason: 'INPUT_VALIDATION' });<commit_msg>Fix RegExp<commit_after>'use strict';

const { underscored } = require('underscore.string');
const { singular, plural } = require('pluralize');

const { throwError } = require('../../../../error');

const {
  getTopLevelAction,
  isTopLevelAction,
  getModel,
  getActionConstant,
} = require('./utilities');

// Add `action.actionConstant` and `action.modelName`
const parseModels = function ({ actions, modelsMap }) {
  const topLevelAction = parseTopLevelAction({ actions, modelsMap });
  const nestedActions = parseNestedActions({
    actions,
    modelsMap,
    topLevelAction,
  });
  return [topLevelAction, ...nestedActions];
};

// Parse a GraphQL query top-level action name into tokens.
// E.g. `findMyModels` -> { actionType: 'find', modelName: 'my_models' }
const parseTopLevelAction = function ({ actions, modelsMap }) {
  const topLevelAction = getTopLevelAction({ actions });
  const { actionPath: [actionName] } = topLevelAction;

  const { actionType, modelName } = parseName({ actionName });

  const modelNameA = underscored(modelName);

  const singularName = singular(modelNameA);
  const pluralName = plural(modelNameA);
  const isArray = modelNameA === pluralName;

  const modelNameB = modelsMap[pluralName] ? pluralName : singularName;

  const actionConstant = getActionConstant({ actionType, isArray });

  validateTopLevel({ modelName: modelNameB, actionName });

  return { ...topLevelAction, actionConstant, modelName: modelNameB };
};

const parseName = function ({ actionName }) {
  const [, actionType, modelName = ''] = nameRegExp.exec(actionName) || [];
  return { actionType, modelName };
};

// Matches e.g. 'find_my_models' -> ['find', 'my_models'];
const nameRegExp = /^([a-z0-9]+)_([a-z0-9_]*)$/;

const validateTopLevel = function ({ modelName, actionName }) {
  if (modelName) { return; }

  const message = `Action '${actionName}' is unknown`;
  throwError(message, { reason: 'INPUT_VALIDATION' });
{'fraction_non_alphanumeric': 0.1489963973237262, 'fraction_numerical': 0.002058672156459084, 'mean_word_length': 4.666180758017493, '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': '7583781', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1155, 'n_words': 285}