Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. /**Walks the directory tree received from */
  2. const walkTree = tree => {
  3. if (!tree.children) {
  4. let unit = 0
  5. let subject = ''
  6. let { path } = tree
  7. if (path.includes('unit1')) {
  8. unit = 1
  9. } else {
  10. unit = 2
  11. }
  12. /**
  13. * Takes the path eg. public/June2011Paper1.pdf
  14. * Splits and take last element June2011Paper1.pdf
  15. * Then takes that and drops the .pdf to create the title
  16. * */
  17. let title = path
  18. .split('/')
  19. .slice(-1)[0]
  20. .split('.')[0]
  21. let [month, year, paper] = title.split('_')
  22.  
  23. if (path.includes('physics')) subject = 'physics'
  24. else if (path.includes('economics')) subject = 'economics'
  25. else if (path.includes('communication_studies'))
  26. subject = 'communication_studies'
  27. else if (path.includes('chemistry')) subject = 'chemistry'
  28. else if (path.includes('biology')) subject = 'biology'
  29. else if (path.includes('pure_math')) subject = 'pure_math'
  30.  
  31. //Removes public/ from path
  32. let newPath = path.replace('public/', '')
  33.  
  34. return { unit, title, month, year, paper, path: newPath, subject }
  35. } else {
  36. let { children } = tree
  37. return children.map(child => walkTree(child))
  38. }
  39. }
  40.  
  41. const flatten = (arr, result = []) => {
  42. arr.map(child => {
  43. if (Array.isArray(child)) {
  44. flatten(child, result)
  45. } else {
  46. result.push(child)
  47. }
  48. })
  49. return result
  50. }
  51.  
  52. module.exports = { flatten, walkTree }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement