Advertisement
nikolayneykov

Untitled

May 28th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getData () {
  2.   let input = JSON.parse(document.querySelector('textarea').value)
  3.   let paragraphs = {
  4.     peopleIn: document.querySelector('#peopleIn p'),
  5.     peopleOut: document.querySelector('#peopleOut p'),
  6.     blacklist: document.querySelector('#blacklist p')
  7.   }
  8.  
  9.   let lastElement = input.pop()
  10.  
  11.   let disco = (function () {
  12.     let lists = {
  13.       peopleIn: [],
  14.       peopleOut: [],
  15.       blacklist: []
  16.     }
  17.  
  18.     function peopleIn (person) {
  19.       let personBlacklist = lists.blacklist.find(
  20.         p => p.firstName === person.firstName && p.lastName === person.lastName
  21.       )
  22.  
  23.       if (!personBlacklist) {
  24.         lists.peopleIn.push(person)
  25.         displayLists()
  26.       }
  27.     }
  28.  
  29.     function peopleOut (person) {
  30.       let personIn = lists.peopleIn.find(
  31.         p => p.firstName === person.firstName && p.lastName === person.lastName
  32.       )
  33.  
  34.       if (personIn) {
  35.         let index = lists.peopleIn.indexOf(personIn)
  36.         lists.peopleIn.splice(index, 1)
  37.         lists.peopleOut.push(person)
  38.         displayLists()
  39.       }
  40.     }
  41.  
  42.     function blacklist (person) {
  43.       lists.blacklist.push(person)
  44.       displayLists()
  45.     }
  46.  
  47.     function displayLists () {
  48.       paragraphs.peopleIn.textContent = lists.peopleIn
  49.         .map(x => JSON.stringify(x))
  50.         .join(' ')
  51.       paragraphs.peopleOut.textContent = lists.peopleOut
  52.         .map(x => JSON.stringify(x))
  53.         .join(' ')
  54.       paragraphs.blacklist.textContent = lists.blacklist
  55.         .map(x => JSON.stringify(x))
  56.         .join(' ')
  57.  
  58.       if (lastElement.action !== '' && lastElement.criteria !== '') {
  59.         paragraphs[lastElement.action].textContent = lists[lastElement.action]
  60.           .sort((a, b) =>
  61.             a[lastElement.criteria].localeCompare(b[lastElement.criteria])
  62.           )
  63.           .map(x => JSON.stringify(x))
  64.           .join(' ')
  65.       }
  66.     }
  67.  
  68.     return { peopleIn, peopleOut, blacklist }
  69.   })()
  70.  
  71.   input.forEach(p =>
  72.     disco[p.action]({ firstName: p.firstName, lastName: p.lastName })
  73.   )
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement