Created
March 26, 2018 08:49
-
-
Save HoverBaum/70c19fe18ec6991e8a3070ba5a5257a2 to your computer and use it in GitHub Desktop.
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 getCategories = (posts, baseUrl, simpleLog = console.log) => new Promise(async resolve => { | |
const apiURL = `${baseUrl.replace(/\/$/, '')}/wp-json/wp/v2/categories` | |
// First reduce posts to an array of category numbers. | |
simpleLog('Reducing posts to category numbers') | |
const categories = await Promise.all(posts.reduce((all, post) => { | |
if(!post.category) return all | |
if(all.indexOf(post.category) > -1) return all | |
return all.concat([post.category]) | |
}, []) | |
.map(async categoryNumber => { | |
simpleLog(`Getting information about categories`) | |
const categoryData = await getJSON(`${apiURL}/${categoryNumber}`) | |
return { | |
categoryNumber, | |
name: categoryData.name, | |
slug: categoryData.slug, | |
description: categoryData.description | |
} | |
})) | |
resolve(categories) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment