<commit_before>'use strict';
/**
* @file
* requestType indentifier
*
* This file contains functions designed to find out which endpoints
* needs to be called, based on the provided filter fields.
*
* Example of usage:
*
*   let typeID = makeTypeID();
*
*   let retval = typeID.getType('isAnalysisOf');
*   let isval = typeID.isType('isAnalysisOf', 'relations');
*
*/
import fs from 'fs';
import {die, log} from './utils.js';

const workContext = JSON.parse(fs.readFileSync(__dirname + '/../doc/work-context.jsonld', 'utf8'));
const reverseContext = makeReverseContext();

/**
 * Returns field based on id and type
 * @param {string} xmlBaseName the name of the tag of requested field
 * @param {string} type the type of requested field
 * @returns {string} fieldname
 *
 * @api public
 */
function getField(xmlBaseName, type) {
  let xmlName = xmlBaseName;
  if (type) {
    xmlName = xmlBaseName + type.split(':')[1];
  }
  let jsonName = reverseContext[xmlName.toLowerCase()];
  if (!jsonName) {
    log.warn('invalid id/type, trying type=oth', {tag: xmlBaseName, type: type});
    xmlName = xmlBaseName + 'oth';
    jsonName = reverseContext[xmlName.toLowerCase()];
  }
  return jsonName;
}

/**
* requestType enum. Only these request types are supported.
*/
export let requestType = {
  RELATIONS: 'relations',
  MOREINFO: 'moreinfo',
  BRIEFDISPLAY: 'briefdisplay',
  COLLECTION: 'collection',
  DKABM: 'dkabm'
};

/**
* private function. validates type
*/
function isRequestType(type) {
  for (var key in requestType) {
    if (type === requestType[key]) {
      return true;
    }
  }
  return false;
}

/**
* TypeID class
*
* provides two functions:
*   getType: returns type for input field
*   isType: performs type test.
*
*/
export function TypeID() {
  this.namespaceMap = {
    dc: requestType.DKABM,
    dkdcplus: requestType.DKABM,
    dcterms: requestType.DKABM,
    ac: requestType.DKABM,
    bd: requestType.BRIEFDISPLAY,
    os: requestType.COLLECTION,
    mi: requestType.MOREINFO,
    dbcaddi: requestType.RELATIONS,
    dbcbib: requestType.RELATIONS
  };

  /**
   * Gets type of field
   * @param {string} field returns endpoint type for field
   * @throws {err} if fieldtype is unknown
   *
   * @api public
   */
  this.getType = function(field) {

    let val = workContext[field];
    if (typeof val === 'undefined' || typeof val === 'string') {
      die('key \'' + field + '\' is unknown');
    }

    let namespace = val['@id'].split(':')[0];

    if (typeof this.namespaceMap[namespace] === 'undefined') {
      die('unknown namespace \'' + namespace + '\'. Known namespaces are [' + Object.keys(this.namespaceMap) + ']');
    }
    return this.namespaceMap[namespace];
  };

  /**
   * Tests field type
   * @param {string} field fieldname to test
   * @param {string} type to test against
   * @throws {err} if field or type is unknown
   *
   * @api public
   */
  this.isType = function(field, type) {
    if (!isRequestType(type)) {
      die('type \'' + type + '\' is not a valid requestType. valid requestTypes are: [' + Object.keys(requestType).map(function(k) {
        return requestType[k];
      }) + ']');
    }

    if (this.getType(field) === type) {
      return true;
    }
    return false;
  };
  this.getField = getField;
}

/**
 * Creates and returns TypeID based on work-context file.
 *
 * @api public
 */
export function makeTypeID() {
  return new TypeID();
}


/**
 * Create a lookup table for finding the JSON-name of a tag/type.
 */
function makeReverseContext() {
  let result = {};

  for (let key in workContext) { // eslint-disable-line guard-for-in
    let elem = workContext[key];
    let id = elem['@id'];
    let type = elem['@type'];
    if (id) {
      if (type) {
        id += type.split(':')[1];
      }
      id = id.toLowerCase();
      result[id] = key;
    }
  }
  return result;
}

/**
 * Map a badgerfish xml tag to its name in the json object.
 * @param {object} o The xml element as badgerfish
 * @param {string} defaultPrefix The default prefix for the namespace for elements in the xml.
 */
export function workToJSON(o, defaultPrefix) {
  var result = {};
  for (let key in o) {  // eslint-disable-line guard-for-in
    let entries = o[key];
    if (!Array.isArray(entries)) {
      entries = [entries];
    }
    entries.forEach(entry => { // eslint-disable-line no-loop-func
      if (key === '@') {
        return;
      }
      let xmlBaseName = (entry['@'] || defaultPrefix) + ':' + key;
      let type = entry['@type'] ? entry['@type'].$ : undefined; // eslint-disable-line no-undefined

      let jsonName = getField(xmlBaseName, type);
<commit_msg>code cleanup<commit_after>'use strict';
/**
* @file
* requestType indentifier
*
* This file contains functions designed to find out which endpoints
* needs to be called, based on the provided filter fields.
*
* Example of usage:
*
*   let typeID = makeTypeID();
*
*   let retval = typeID.getType('isAnalysisOf');
*   let isval = typeID.isType('isAnalysisOf', 'relations');
*
*/
import fs from 'fs';
import {die, log} from './utils.js';

const workContext = JSON.parse(fs.readFileSync(__dirname + '/../doc/work-context.jsonld', 'utf8'));
const reverseContext = makeReverseContext();

/**
 * Returns field based on id and type
 * @param {string} tagName the name of the tag of requested field
 * @param {string} type the type of requested field
 * @returns {string} fieldname
 *
 * @api public
 */
function getField(tagName, type) {
  let xmlName = tagName;
  if (type) {
    xmlName = tagName + type.split(':')[1];
  }
  let jsonName = reverseContext[xmlName.toLowerCase()];
  if (!jsonName) {
    log.warn('invalid id/type, trying type=oth', {tag: tagName, type: type});
    xmlName = tagName + 'oth';
    jsonName = reverseContext[xmlName.toLowerCase()];
  }
  return jsonName;
}

/**
* requestType enum. Only these request types are supported.
*/
export let requestType = {
  RELATIONS: 'relations',
  MOREINFO: 'moreinfo',
  BRIEFDISPLAY: 'briefdisplay',
  COLLECTION: 'collection',
  DKABM: 'dkabm'
};

/**
* private function. validates type
*/
function isRequestType(type) {
  for (var key in requestType) {
    if (type === requestType[key]) {
      return true;
    }
  }
  return false;
}

/**
* TypeID class
*
* provides two functions:
*   getType: returns type for input field
*   isType: performs type test.
*
*/
export function TypeID() {
  this.namespaceMap = {
    dc: requestType.DKABM,
    dkdcplus: requestType.DKABM,
    dcterms: requestType.DKABM,
    ac: requestType.DKABM,
    bd: requestType.BRIEFDISPLAY,
    os: requestType.COLLECTION,
    mi: requestType.MOREINFO,
    dbcaddi: requestType.RELATIONS,
    dbcbib: requestType.RELATIONS
  };

  /**
   * Gets type of field
   * @param {string} field returns endpoint type for field
   * @throws {err} if fieldtype is unknown
   *
   * @api public
   */
  this.getType = function(field) {

    let val = workContext[field];
    if (typeof val === 'undefined' || typeof val === 'string') {
      die('key \'' + field + '\' is unknown');
    }

    let namespace = val['@id'].split(':')[0];

    if (typeof this.namespaceMap[namespace] === 'undefined') {
      die('unknown namespace \'' + namespace + '\'. Known namespaces are [' + Object.keys(this.namespaceMap) + ']');
    }
    return this.namespaceMap[namespace];
  };

  /**
   * Tests field type
   * @param {string} field fieldname to test
   * @param {string} type to test against
   * @throws {err} if field or type is unknown
   *
   * @api public
   */
  this.isType = function(field, type) {
    if (!isRequestType(type)) {
      die('type \'' + type + '\' is not a valid requestType. valid requestTypes are: [' + Object.keys(requestType).map(function(k) {
        return requestType[k];
      }) + ']');
    }

    return this.getType(field) === type;
  };
  this.getField = getField;
}

/**
 * Creates and returns TypeID based on work-context file.
 *
 * @api public
 */
export function makeTypeID() {
  return new TypeID();
}

/**
 * Create a lookup table for finding the JSON-name of a tag/type.
 */
function makeReverseContext() {
  let result = {};

  for (let key in workContext) { // eslint-disable-line guard-for-in
    let elem = workContext[key];
    let id = elem['@id'];
    let type = elem['@type'];
    if (id) {
      if (type) {
        id += type.split(':')[1];
      }
      id = id.toLowerCase();
      result[id] = key;
    }
  }
  return result;
}

/**
 * Map a badgerfish xml tag to its name in the json object.
 * @param {object} o The xml element as badgerfish
 * @param {string} defaultPrefix The default prefix for the namespace for elements in the xml.
 */
export function workToJSON(o, defaultPrefix) {
  var result = {};
  for (let key in o) {  // eslint-disable-line guard-for-in
    if (key === '@') {
      continue;
    }
    let entries = o[key];
    if (!Array.isArray(entries)) {
      entries = [entries];
    }
    entries.forEach(entry => { // eslint-disable-line no-loop-func
      let tagName = (entry['@'] || defaultPrefix) + ':' + key;
      let type = entry['@type'] ? entry['@type'].$ : undefined; // eslint-disable-line no-undefined
      let jsonName = getField(tagName, type);
{'fraction_non_alphanumeric': 0.14687975646879756, 'fraction_numerical': 0.0008697542944118286, 'mean_word_length': 3.090262338817252, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 24}, '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': '1050177', 'n_tokens_mistral': 2992, 'n_tokens_neox': 2961, 'n_words': 861}