Skip to content

Instantly share code, notes, and snippets.

@iandesj
Created July 16, 2019 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iandesj/51540e8bc86ff198241216388fcecf06 to your computer and use it in GitHub Desktop.
Save iandesj/51540e8bc86ff198241216388fcecf06 to your computer and use it in GitHub Desktop.
Simplified Methods
// 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