Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**Walks the directory tree received from */
- const walkTree = tree => {
- if (!tree.children) {
- let unit = 0
- let subject = ''
- let { path } = tree
- if (path.includes('unit1')) {
- unit = 1
- } else {
- unit = 2
- }
- /**
- * Takes the path eg. public/June2011Paper1.pdf
- * Splits and take last element June2011Paper1.pdf
- * Then takes that and drops the .pdf to create the title
- * */
- let title = path
- .split('/')
- .slice(-1)[0]
- .split('.')[0]
- let [month, year, paper] = title.split('_')
- if (path.includes('physics')) subject = 'physics'
- else if (path.includes('economics')) subject = 'economics'
- else if (path.includes('communication_studies'))
- subject = 'communication_studies'
- else if (path.includes('chemistry')) subject = 'chemistry'
- else if (path.includes('biology')) subject = 'biology'
- else if (path.includes('pure_math')) subject = 'pure_math'
- //Removes public/ from path
- let newPath = path.replace('public/', '')
- return { unit, title, month, year, paper, path: newPath, subject }
- } else {
- let { children } = tree
- return children.map(child => walkTree(child))
- }
- }
- const flatten = (arr, result = []) => {
- arr.map(child => {
- if (Array.isArray(child)) {
- flatten(child, result)
- } else {
- result.push(child)
- }
- })
- return result
- }
- module.exports = { flatten, walkTree }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement