<commit_before>'use strict';
/**
 * Suggest transformer.
 *
 * Wraps entitysuggest backends, and popsuggest backend.
 *
 *
 */
function validateParams(params) {
  if (!params.q) {
    throw ('missing q parameter');
  }
  if (!params.type) {
    throw ('missing type parameter');
  }
}

let fMap = {creator: creatorSuggest,
            library: librarySuggest,
            subject: subjectSuggest,
            title: titleSuggest};


export default (params, context) => {

  try {
    validateParams(params);
  } catch (err) { // eslint-disable-line brace-style
    return new Promise(resolve => {
      return resolve({statusCode: 400,
                      error: err});
    });
  }

  if (!fMap[params.type]) {
    return new Promise(resolve => {
      return resolve({statusCode: 400,
                      error: `Unsupported suggestion type ${params.type}.` +
                      ` Supported types are: ${Object.keys(fMap)}`});
    });
  }

  return fMap[params.type](params, context).then(result => {
    return result;
  });
};


function creatorSuggest(params, context) {
  let localParams = {query: params.q};
  if (params.limit) {
    localParams.n = params.limit;
  }
  return context.call('creatorsuggest', localParams)
    .then(body => {
      return {statusCode: 200, data: body.data.response.suggestions.map((obj) => {
        return {str: obj.suggestion};
      })};
    });
}


function librarySuggest(params, context) {
  let localParams = {query: params.q, lt: context.data.librarysuggest.librarytype};
  if (params.limit) {
    localParams.n = params.limit;
  }
  return context.call('librarysuggest', localParams)
    .then(body => {
      return {statusCode: 200, data: body.data.response.suggestions.map((obj) => {
        return {str: obj.suggestion};
      })};
    });
}


function subjectSuggest(params, context) {
  let localParams = {query: params.q, rs: 3};
  if (params.limit) {
    localParams.n = params.limit;
  }
  return context.call('subjectsuggest', localParams)
    .then(body => {
      return {statusCode: 200, data: body.data.response.suggestions.map((obj) => {
        return {str: obj.suggestion};
      })};
    });
}


function mapTitleKeys(obj) {
  let retObj = {str: obj['display.title'][0], id: obj.fedoraPid};
  if (obj.hasOwnProperty('display.creator')) {
    retObj.creator = obj['display.creator'][0];
  }
  if (obj.hasOwnProperty('display.workType')) {
    retObj.type = obj['display.workType'][0];
  }
  return retObj;
}


function titleSuggest(params, context) {<commit_msg>Suggest error handling (#260)<commit_after>'use strict';
/**
 * Suggest transformer.
 *
 * Wraps entitysuggest backends, and popsuggest backend.
 *
 *
 */
function validateParams(params) {
  if (!params.q) {
    throw ('missing q parameter');
  }
  if (!params.type) {
    throw ('missing type parameter');
  }
  if (typeof params.q !== 'string') {
    throw ('q parameter must be a string');
  }
  if (typeof params.limit !== 'number') {
    throw ('limit parameter must be a number');
  }
}

let fMap = {creator: creatorSuggest,
            library: librarySuggest,
            subject: subjectSuggest,
            title: titleSuggest};


export default (params, context) => {

  try {
    validateParams(params);
  } catch (err) { // eslint-disable-line brace-style
    return new Promise(resolve => {
      return resolve({statusCode: 400,
                      error: err});
    });
  }

  if (!fMap[params.type]) {
    return new Promise(resolve => {
      return resolve({statusCode: 400,
                      error: `Unsupported suggestion type ${params.type}.` +
                      ` Supported types are: ${Object.keys(fMap)}`});
    });
  }

  return fMap[params.type](params, context).then(result => {
    return result;
  });
};


function creatorSuggest(params, context) {
  let localParams = {query: params.q};
  if (params.limit) {
    localParams.n = params.limit;
  }
  return context.call('creatorsuggest', localParams)
    .then(body => {
      return {statusCode: 200, data: body.data.response.suggestions.map((obj) => {
        return {str: obj.suggestion};
      })};
    });
}


function librarySuggest(params, context) {
  let localParams = {query: params.q, lt: context.data.librarysuggest.librarytype};
  if (params.limit) {
    localParams.n = params.limit;
  }
  return context.call('librarysuggest', localParams)
    .then(body => {
      return {statusCode: 200, data: body.data.response.suggestions.map((obj) => {
        return {str: obj.suggestion};
      })};
    });
}


function subjectSuggest(params, context) {
  let localParams = {query: params.q, rs: 3};

  if (params.limit) {
    localParams.n = params.limit;
  }

  return context.call('subjectsuggest', localParams)
    .then(body => {
      return {statusCode: 200, data: body.data.response.suggestions.map((obj) => {
        return {str: obj.suggestion};
      })};
    });
}


function mapTitleKeys(obj) {
  let retObj = {str: obj['display.title'][0], id: obj.fedoraPid};
  if (obj.hasOwnProperty('display.creator')) {
    retObj.creator = obj['display.creator'][0];
  }
  if (obj.hasOwnProperty('display.workType')) {
    retObj.type = obj['display.workType'][0];
  }
  return retObj;
}


function titleSuggest(params, context) {
{'fraction_non_alphanumeric': 0.1544452887537994, 'fraction_numerical': 0.0077887537993920975, 'mean_word_length': 2.946776611694153, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '1425306', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1630, 'n_words': 361}