Created
June 28, 2019 12:52
-
-
Save rproenca/ab02e97344d34898ab6c431e96ec0e70 to your computer and use it in GitHub Desktop.
p-pipe
This file contains hidden or 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
const fetch = require('node-fetch') | |
const pipe = require('p-pipe') | |
const { table } = require('table') | |
async function fetchDogs(dogs) { | |
const promises = dogs.map(async function (dog) { | |
const response = await fetch(`https://dog.ceo/api/breed/${dog}/images/random/1`) | |
const data = await response.json() | |
return { | |
[dog]: data.message | |
} | |
}) | |
return await Promise.all(promises) | |
} | |
function upperCaseKeys(objectArray) { | |
return objectArray.map(obj => { | |
const [key, value] = Object.entries(obj)[0] | |
return { | |
[key.toUpperCase()]: value | |
} | |
}) | |
} | |
function generateTable(objectArray) { | |
const formattedArray = objectArray.map(Object.entries) | |
const flattenedArray = formattedArray.reduce((acc, curr) => acc.concat(curr), []) | |
return table(flattenedArray) | |
} | |
(async () => { | |
const dogs = ["affenpinscher", "african", "airedale", "akita", "appenzeller", "basenji", "beagle", "bluetick", "borzoi", "bouvier", "boxer", "brabancon", "briard", "bulldog", "bullterrier", "cairn", "cattledog", "chihuahua", "chow", "clumber", "cockapoo", "collie", "coonhound", "corgi", "cotondetulear", "dachshund", "dalmatian", "dane", "deerhound", "dhole", "dingo", "doberman", "elkhound", "entlebucher", "eskimo", "frise", "germanshepherd", "greyhound", "groenendael", "hound", "husky", "keeshond"] | |
console.time('Execution time:') | |
const dogPipeline = pipe( | |
fetchDogs, | |
upperCaseKeys, | |
generateTable, | |
console.log | |
) | |
await dogPipeline(dogs) | |
console.timeEnd('Execution time:') | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment