<commit_before>import API from 'src/util/api';

/**
 * (string|array) [categories='']:  categories
 * (object) [options]
 * (string) [options.variableName='templates']
 */

module.exports = async function loadTemplates(categories, options = {}) {
  const {
    variableName = 'templates'
  } = options;
  if (typeof categories === 'string') {
    categories = [categories];
  }
  var Roc = await API.require('vh/rest-on-couch/Roc');

  // we check if roc is already defined, in this case
  // we will check if the templates database exists
  var roc = API.cache('roc');

  var templateRoc;
  if (roc) {
    await fetch(`${roc.url}/db/templates/_query/template`).then(() => {
      templateRoc = new Roc({
        database: 'templates',
        url: roc.url,
        track: false
      });
    }).catch(() => { // no local templates database, we use the default one
      templateRoc = new Roc({
        database: 'templates',
        url: 'https://mydb.cheminfo.org',
        track: false
      });
    });
  } else {
    templateRoc = new Roc({
      database: 'templates',
      url: 'https://mydb.cheminfo.org',
      track: false
    });
  }

  var templates = [];
  for (let category of categories) {
    let currentTemplates = await templateRoc.query(
      'template', {
        startkey: category,
        endkey: category+'\uFFFF'
      }
    );
    templates.push(...currentTemplates);
  }

  templates.sort( (a,b) {
    if (a.value.title < b.value.title) return -1;
    if (a.value.title > b.value.title) return 1;
    return 0;<commit_msg>fix sort<commit_after>import API from 'src/util/api';

/**
 * (string|array) [categories='']:  categories
 * (object) [options]
 * (string) [options.variableName='templates']
 */

module.exports = async function loadTemplates(categories, options = {}) {
  const {
    variableName = 'templates'
  } = options;
  if (typeof categories === 'string') {
    categories = [categories];
  }
  var Roc = await API.require('vh/rest-on-couch/Roc');

  // we check if roc is already defined, in this case
  // we will check if the templates database exists
  var roc = API.cache('roc');

  var templateRoc;
  if (roc) {
    await fetch(`${roc.url}/db/templates/_query/template`).then(() => {
      templateRoc = new Roc({
        database: 'templates',
        url: roc.url,
        track: false
      });
    }).catch(() => { // no local templates database, we use the default one
      templateRoc = new Roc({
        database: 'templates',
        url: 'https://mydb.cheminfo.org',
        track: false
      });
    });
  } else {
    templateRoc = new Roc({
      database: 'templates',
      url: 'https://mydb.cheminfo.org',
      track: false
    });
  }

  var templates = [];
  for (let category of categories) {
    let currentTemplates = await templateRoc.query(
      'template', {
        startkey: category,
        endkey: category+'\uFFFF'
      }
    );
    templates.push(...currentTemplates);
  }

  templates.sort( (a,b) => {
    if (a.value.title < b.value.title) return -1;
    if (a.value.title > b.value.title) return 1;
    return 0;
{'fraction_non_alphanumeric': 0.15548387096774194, 'fraction_numerical': 0.001935483870967742, 'mean_word_length': 2.544, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 4, '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': '314374', 'n_tokens_mistral': 985, 'n_tokens_neox': 975, 'n_words': 252}