daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const all = [
  2.   {
  3.     name: 'Folder1',
  4.     children: [
  5.       {
  6.         name: 'subfolder 1.1',
  7.         children: [
  8.           {
  9.             name: 'item 1.1.1',
  10.           }, {
  11.             name: 'item 1.1.2',
  12.           },
  13.         ],
  14.       },
  15.       {
  16.         name: 'subfolder 1.2',
  17.         children: [
  18.           {
  19.             name: 'item 1.2.1',
  20.           },
  21.           {
  22.             name: 'item 1.2.2',
  23.           },
  24.         ],
  25.       },
  26.     ],
  27.   },
  28.   {
  29.     name: 'Folder2',
  30.     children: [
  31.       {
  32.         name: 'subfolder 2.1',
  33.         children: [
  34.           {
  35.             name: 'Item 2.1.1',
  36.           },
  37.           {
  38.             name: 'Item 2.1.2',
  39.           },
  40.         ],
  41.       },
  42.       {
  43.         name: 'subfolder 2.2',
  44.         children: [
  45.           {
  46.             name: 'Item 2.2.1',
  47.           },
  48.           {
  49.             name: 'Item 2.2.2',
  50.           },
  51.         ],
  52.       },
  53.     ],
  54.   },
  55.   {
  56.     name: 'Folder3',
  57.     children: [
  58.       {
  59.         name: 'subfolder 3.1',
  60.         children: [
  61.           {
  62.             name: 'Item 3.1.1',
  63.           },
  64.           {
  65.             name: 'Item 3.1.2',
  66.           },
  67.         ],
  68.       },
  69.       {
  70.         name: 'subfolder 3.2',
  71.         children: [
  72.           {
  73.             name: 'Item 3.2.1',
  74.           },
  75.           {
  76.             name: 'Item 3.2.2',
  77.           },
  78.         ],
  79.       },
  80.     ],
  81.   },
  82. ]
  83.  
  84. function applyFilters(data, filters) {
  85.   function applyFilter(items = [], filter) {
  86.     const filtered = []
  87.  
  88.     for (const item of items) {
  89.       if (item.name.indexOf(filter) !== -1) {
  90.         filtered.push(item)
  91.       } else {
  92.         const children = applyFilter(item.children, filter)
  93.  
  94.         if (children.length) {
  95.           filtered.push({
  96.             ...item,
  97.             children,
  98.           })
  99.         }
  100.       }
  101.     }
  102.  
  103.     return filtered
  104.   }
  105.  
  106.   let result = data
  107.  
  108.   for (const filter of filters) {
  109.     result = applyFilter(result, filter)
  110.   }
  111.  
  112.   return result
  113. }
  114.  
  115.  
  116. // test suites
  117.  
  118. function it(message, expression, result) {
  119.   if (expression === result) {
  120.     return console.log(`${message}: passed. Result: ${expression}\n\n`)
  121.   }
  122.  
  123.   throw Error(`${message}: failed. Expected: "${result}". Actual: "${expression}"`)
  124. }
  125.  
  126. it('JSON.stringify(applyFilters(all, [\'3.2.2\']))', JSON.stringify(applyFilters(all, ['3.2.2'])), '[{"name":"Folder3","children":[{"name":"subfolder 3.2","children":[{"name":"Item 3.2.2"}]}]}]')
  127.  
  128. it('JSON.stringify(applyFilters(all, [\'3\', \'2\']).filter(f => f.children.length))', JSON.stringify(applyFilters(all, ['3', '2']).filter(f => f.children.length)), '[{"name":"Folder3","children":[{"name":"subfolder 3.1","children":[{"name":"Item 3.1.2"}]},{"name":"subfolder 3.2","children":[{"name":"Item 3.2.1"},{"name":"Item 3.2.2"}]}]}]')
  129.  
  130. it('JSON.stringify(applyFilters(all, [\'.1\']).filter(f => f.children.length))', JSON.stringify(applyFilters(all, ['.1']).filter(f => f.children.length)), '[{"name":"Folder1","children":[{"name":"subfolder 1.1","children":[{"name":"item 1.1.1"},{"name":"item 1.1.2"}]},{"name":"subfolder 1.2","children":[{"name":"item 1.2.1"}]}]},{"name":"Folder2","children":[{"name":"subfolder 2.1","children":[{"name":"Item 2.1.1"},{"name":"Item 2.1.2"}]},{"name":"subfolder 2.2","children":[{"name":"Item 2.2.1"}]}]},{"name":"Folder3","children":[{"name":"subfolder 3.1","children":[{"name":"Item 3.1.1"},{"name":"Item 3.1.2"}]},{"name":"subfolder 3.2","children":[{"name":"Item 3.2.1"}]}]}]')
  131.  
  132. it('JSON.stringify(applyFilters(all, [\'Folder2\', \'1.1\']).filter(f => f.children.length))', JSON.stringify(applyFilters(all, ['Folder2', '1.1']).filter(f => f.children.length)), '[{"name":"Folder2","children":[{"name":"subfolder 2.1","children":[{"name":"Item 2.1.1"}]}]}]')
  133.  
  134. it('JSON.stringify(applyFilters(all, [\'Folder2\', \'2.1\']).filter(f => f.children.length))', JSON.stringify(applyFilters(all, ['Folder2', '2.1']).filter(f => f.children.length)), '[{"name":"Folder2","children":[{"name":"subfolder 2.1","children":[{"name":"Item 2.1.1"},{"name":"Item 2.1.2"}]},{"name":"subfolder 2.2","children":[{"name":"Item 2.2.1"}]}]}]')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top