<commit_before>import _ from 'lodash';
import {toUpperUnderscore} from './utilities';

function _generateActionTypes(hubName, methods, server) {
  const hubUpper = toUpperUnderscore(hubName);
  return methods.map(x => {
    const upper = toUpperUnderscore(x.Name);
    if(server) {
      return [
        `export const ${upper}_REQUEST = '${hubUpper}::${upper}_REQUEST';`,
        `export const ${upper}_RESPONSE = '${hubUpper}::${upper}_RESPONSE';`,
        `export const ${upper}_ERROR = '${hubUpper}::${upper}_ERROR';`
      ].join('\r\n');
    } else {
      return `export const ${upper} = '${hubUpper}::${upper}';`;
    }
  }).join('\r\n');
}

function _generateMethodComments(method, type) {
  let comments = [];
  let argumentComments = [];
  if(method.Comment !== null) {
    if(method.Comment.Summary !== null) {
      comments.push(`* ${method.Comment.Summary}`)
    }
    if(method.Returns) {
      if(method.Comment.Returns !== null) {
        comments.push(`* @returns {${typeof(method.Returns)}} ${method.Returns} ${method.Comment.Returns}`)
      } else {
        comments.push(`* @returns {${typeof(method.Returns)}} ${method.Returns} No comment found for this return statement.`)
      }
    }
    if(method.Arguments !== null || type !== null) {
        switch(type) {
          case 'response':
            argumentComments.push(`* @params {string} response ${method.Comment.Arguments || `No argument comment found.`}`);
            break;
          case 'error':
            argumentComments.push(`* @params {string} error ${method.Comment.Arguments || `No argument comment found.`}`);
            break;
          case 'request':
            argumentComments = ([].map.call(method.Arguments, arg => `* @params {${typeof(arg)}} ${arg} ${method.Comment.Arguments || `No argument comment found.`}`));
            break;
          case 'dispatch':
            argumentComments = ([].map.call(method.Arguments, arg => `* @params {${typeof(arg)}} ${arg} ${method.Comment.Arguments || `No argument comment found.`}`));
            break;
        }
      comments = comments.concat(argumentComments);
    }
  } else {
    comments.push(` *No comments defined for this method.`);
  }
  return comments.join('\r\n');
}

function _generateActionCreators(methods, server) {
  return methods.map(x => {
    const upperType = toUpperUnderscore(x.Name);
    const camelAction = _.camelCase(upperType);
    const args = x.Arguments.join(', ');
    const sep = (args.length ? ', ' : '');
    const comments = x.Comment;
    if(server) {
      return [
        `/**`,
        `${_generateMethodComments(x,'request')}`,
        `*/`,
        `export function ${camelAction}Request(${args}){`,
        `  return {type: ${upperType}_REQUEST${sep}${args}};`,
        `}`,
        `/**`,
        `${_generateMethodComments(x,'response')}`,
        `*/`,
        `export function ${camelAction}Response(response){`,
        `  return {type: ${upperType}_RESPONSE, response};`,
        `}`,
        `/**`,
        `${_generateMethodComments(x,'error')}`,
        `*/`,
        `export function ${camelAction}Error(error){`,
        `  return {type: ${upperType}_ERROR, error};`,
        `}`,
        `/**`,
        `${_generateMethodComments(x,'dispatch')}`,
        `*/`,
        `export function ${camelAction}(${args}){`,
        `  return (dispatch) => {`,
        `    const bound = {`,
        `      ${camelAction}Request: (${args}) => dispatch(${camelAction}Request(${args})),`,
        `      ${camelAction}Response: (response) => dispatch(${camelAction}Response(response)),`,
        `      ${camelAction}Error: (error) => dispatch(${camelAction}Error(error))`,
        `    };`,
        `    bound.${camelAction}Request(${args});`,
        `    server.${camelAction}(${args})`,
        `      .then(bound.${camelAction}Response)`,
        `      .fail(bound.${camelAction}Error);`,
        `  };`,
        `}`
      ].join('\r\n');
    } else {
      return [
        `export function ${camelAction}(${args}){`,
        `  return {type: ${upperType}${sep}${args}};`,
        `}`
      ].join('\r\n');
    }<commit_msg>wip jdocs for hubs<commit_after>import _ from 'lodash';
import {toUpperUnderscore} from './utilities';

function _generateActionTypes(hubName, methods, server) {
  const hubUpper = toUpperUnderscore(hubName);
  return methods.map(x => {
    const upper = toUpperUnderscore(x.Name);
    if(server) {
      return [
        `export const ${upper}_REQUEST = '${hubUpper}::${upper}_REQUEST';`,
        `export const ${upper}_RESPONSE = '${hubUpper}::${upper}_RESPONSE';`,
        `export const ${upper}_ERROR = '${hubUpper}::${upper}_ERROR';`
      ].join('\r\n');
    } else {
      return `export const ${upper} = '${hubUpper}::${upper}';`;
    }
  }).join('\r\n');
}

function _generateMethodComments(method, type) {
  let comments = [];
  let argumentComments = [];
    if(method.Comment.Summary !== null) {
      comments.push(`* ${method.Comment.Summary}`)
    }
    if(method.Returns) {
      if(method.Comment.Returns !== null) {
        comments.push(`* @returns {${typeof(method.Returns)}} ${method.Returns} ${method.Comment.Returns}`)
      } else {
        comments.push(`* @returns {${typeof(method.Returns)}} ${method.Returns} No comment found for this return statement.`)
      }
    }
    if(method.Arguments !== null || type !== null) {
        switch(type) {
          case 'response':
            argumentComments.push(`* @params {string} response ${method.Comment.Arguments}`);
            break;
          case 'error':
            argumentComments.push(`* @params {string} error ${method.Comment.Arguments}`);
            break;
          case 'request':
            argumentComments = ([].map.call(method.Arguments, arg => `* @params {${typeof(arg)}} ${arg} ${method.Comment.Arguments}`));
            break;
          case 'dispatch':
            argumentComments = ([].map.call(method.Arguments, arg => `* @params {${typeof(arg)}} ${arg} ${method.Comment.Arguments}`));
            break;
        }
      comments = comments.concat(argumentComments);
    }
  if(comments.isEmpty){
    comments.push('*');
  }
  return comments.join('\r\n');
}

function _generateActionCreators(methods, server) {
  return methods.map(x => {
    const upperType = toUpperUnderscore(x.Name);
    const camelAction = _.camelCase(upperType);
    const args = x.Arguments.join(', ');
    const sep = (args.length ? ', ' : '');
    const comments = x.Comment;
    if(server) {
      return [
        `/**`,
        `${_generateMethodComments(x, 'request')}`,
        `*/`,
        `export function ${camelAction}Request(${args}){`,
        `  return {type: ${upperType}_REQUEST${sep}${args}};`,
        `}`,
        `/**`,
        `${_generateMethodComments(x, 'response')}`,
        `*/`,
        `export function ${camelAction}Response(response){`,
        `  return {type: ${upperType}_RESPONSE, response};`,
        `}`,
        `/**`,
        `${_generateMethodComments(x, 'error')}`,
        `*/`,
        `export function ${camelAction}Error(error){`,
        `  return {type: ${upperType}_ERROR, error};`,
        `}`,
        `/**`,
        `${_generateMethodComments(x, 'dispatch')}`,
        `*/`,
        `export function ${camelAction}(${args}){`,
        `  return (dispatch) => {`,
        `    const bound = {`,
        `      ${camelAction}Request: (${args}) => dispatch(${camelAction}Request(${args})),`,
        `      ${camelAction}Response: (response) => dispatch(${camelAction}Response(response)),`,
        `      ${camelAction}Error: (error) => dispatch(${camelAction}Error(error))`,
        `    };`,
        `    bound.${camelAction}Request(${args});`,
        `    server.${camelAction}(${args})`,
        `      .then(bound.${camelAction}Response)`,
        `      .fail(bound.${camelAction}Error);`,
        `  };`,
        `}`
      ].join('\r\n');
    } else {
      return [
        `export function ${camelAction}(${args}){`,
        `  return {type: ${upperType}${sep}${args}};`,
        `}`
      ].join('\r\n');
    }
{'fraction_non_alphanumeric': 0.19908381824935, 'fraction_numerical': 0.0, 'mean_word_length': 2.676832043695949, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '2841512', 'n_tokens_mistral': 2472, 'n_tokens_neox': 2465, 'n_words': 421}