<commit_before>/*jshint esversion: 6 */
/* global __dirname */

(() => {
  'use strict';

  const request = require('request');
  const vCard = require( 'vcf' );
  const Common = require(__dirname + '/../common');

  function translateCardAddress(adr) {
    const values = adr.valueOf().split(";");

    const streetAddresses = values[2] || "";
    const localities = values[3] || "";
    const postalCodes = values[5] || "";

    const postOfficeText = [ postalCodes, localities ].join('\u00A0');
    
    return [streetAddresses, postOfficeText].filter(i => !!i).join(', ');
  }
  
  function translateCardToContact(card) {
    const titles = Array.isArray(card.get("title")) ? card.get("title") : [ card.get("title") ];
    const orgs = Array.isArray(card.get("org")) ? card.get("org") : [ card.get("org") ];
    const adrs = Array.isArray(card.get("adr")) ? card.get("adr") : [ card.get("adr") ];
    const emails = Array.isArray(card.get("email")) ? card.get("email") : [ card.get("email") ];
    const tels = Array.isArray(card.get("tel")) ? card.get("tel") : [ card.get("tel") ];
    const fn = card.get("fn");
    
    return {
      displayName: fn ? fn.valueOf() : "",
      title: titles.filter(i => !!i).map(title => title.valueOf()).join(", "),
      emails: emails.filter(i => !!i).map(email => email.valueOf()),
      phoneNumbers: tels.filter(i => !!i).map(tel => tel.valueOf()),
      contactAddresses: adrs.filter(i => !!i).map(adr => translateCardAddress(adr)),
      organizationUnits: orgs.filter(i => !!i && i.valueOf() !== "null").map(org => org.valueOf())
    };
  }
  
  module.exports = (app, config, ModulesClass) => {
    
    app.get('/ajax/contactSearch', (req, res) => {
      const search = req.query.search;      
      const page = parseInt(req.query.page)||0;
      const perPage = Common.SEARCH_CONTACT_PER_TYPE;

      request(config.get("contacts"), (error, response, body) => {
        const cards = vCard.parse(body);
        const contacts = cards.map(card => translateCardToContact(card)).filter((contact) => {
          if (!search) {
            return true;
          }

          const haystack = `${contact.displayName || ""} ${contact.title || ""}`.toLocaleLowerCase();
          return haystack.indexOf((search || "").toLocaleLowerCase()) > -1;
        }).splice(page * perPage, perPage);

        const lastPage = contacts.length < perPage + 1;

        res.render('ajax/contacts-search.pug', {
          paged: page > 0 || !lastPage,
          page: page,
          lastPage: lastPage, 
          contacts: contacts
        });
      });
    });
    
  };

})();<commit_msg>Finalized contacts loading from url<commit_after>/*jshint esversion: 6 */
/* global __dirname */

(() => {
  'use strict';

  const request = require('request');
  const vCard = require( 'vcf' );
  const Common = require(__dirname + '/../common');

  function translateCardAddress(adr) {
    const values = adr.valueOf().split(";");

    const streetAddresses = values[2] || "";
    const localities = values[3] || "";
    const postalCodes = values[5] || "";

    const postOfficeText = [ postalCodes, localities ].join('\u00A0');
    
    return [streetAddresses, postOfficeText].filter(i => !!i).join(', ');
  }
  
  function translateCardToContact(card) {
    const titles = Array.isArray(card.get("title")) ? card.get("title") : [ card.get("title") ];
    const orgs = Array.isArray(card.get("org")) ? card.get("org") : [ card.get("org") ];
    const adrs = Array.isArray(card.get("adr")) ? card.get("adr") : [ card.get("adr") ];
    const emails = Array.isArray(card.get("email")) ? card.get("email") : [ card.get("email") ];
    const tels = Array.isArray(card.get("tel")) ? card.get("tel") : [ card.get("tel") ];
    const fn = card.get("fn");
    
    return {
      displayName: fn ? fn.valueOf() : "",
      title: titles.filter(i => !!i).map(title => title.valueOf()).join(", "),
      emails: emails.filter(i => !!i).map(email => email.valueOf()),
      phoneNumbers: tels.filter(i => !!i).map(tel => tel.valueOf()),
      contactAddresses: adrs.filter(i => !!i).map(adr => translateCardAddress(adr)),
      organizationUnits: orgs.filter(i => !!i && i.valueOf() !== "null").map(org => org.valueOf())
    };
  }
  
  module.exports = (app, config, ModulesClass) => {
    
    app.get('/ajax/contactSearch', (req, res) => {
      const search = req.query.search;      
      const page = parseInt(req.query.page)||0;
      const perPage = Common.SEARCH_CONTACT_PER_TYPE;

      request(config.get("contacts"), (error, response, body) => {
        const cards = vCard.parse(body);
        const allContacts = cards.map(card => translateCardToContact(card));
        const startIndex = page * perPage;        
        const filteredContacts = allContacts.filter((contact) => {
          if (!search) {
            return true;
          }

          const haystack = `${contact.displayName || ""} ${contact.title || ""}`.toLocaleLowerCase();
          return haystack.indexOf((search || "").toLocaleLowerCase()) > -1;
        });
        
        const lastPage = filteredContacts.length <= startIndex + perPage;
        const contacts = filteredContacts.splice(startIndex, perPage);    
        
        res.render('ajax/contacts-search.pug', {
          paged: page > 0 || !lastPage,
          page: page,
          lastPage: lastPage, 
          contacts: contacts
        });
      });
    });
    
  };

})();
{'fraction_non_alphanumeric': 0.1910442282987704, 'fraction_numerical': 0.003853918150119288, 'mean_word_length': 2.851590106007067, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, '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': '779820', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1717, 'n_words': 324}