Created
July 16, 2019 14:43
-
-
Save iandesj/51540e8bc86ff198241216388fcecf06 to your computer and use it in GitHub Desktop.
Simplified Methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// before method simplification | |
function parseAndSanitizeAndGenerateDocument(data) { | |
const parsedData = []; | |
for (let index = 0; index < data.length; index++) { | |
const element = data[index]; | |
if (element.name === 'bacon') { | |
parsedData.push({ | |
name: element.name, | |
age: element.age, | |
}); | |
} | |
} | |
const sanitizedData = []; | |
for (let index = 0; index < parsedData.length; index++) { | |
const element = parsedData[index]; | |
if (element.status === 'active') { | |
sanitizedData.push(element); | |
} | |
} | |
fs.writeFile("/tmp/example", sanitizedData.join(','), (err) => { | |
if(err) { | |
return console.log(err); | |
} | |
console.log("The file was saved!"); | |
}); | |
} | |
// after method simplification | |
// still accomplishes the same task | |
function generateDocumentFromFoodData(foodData) { | |
const sanitizedData = parseAndSanitize(foodData); | |
fs.writeFile("/tmp/example", sanitizedData.join(','), (err) => { | |
if(err) { | |
return console.log(err); | |
} | |
console.log("The file was saved!"); | |
}); | |
} | |
function parseAndSanitize(foodData, nameToFind) { | |
const activeFoodList = foodData.filter( | |
food => food.name === nameToFind && | |
food.status === 'active'); | |
return activeFoodList.map(food => { | |
return { name: food.name, status: food.status }; | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment