mirror of
https://github.com/gurnec/removeddit.git
synced 2026-03-11 08:54:27 +00:00
Replace unflatten() with Array.flat()
This commit is contained in:
parent
f7dc5a76fe
commit
2de967fa56
2 changed files with 3 additions and 10 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { fetchJson, chunk, flatten } from '../../utils'
|
||||
import { fetchJson, chunk } from '../../utils'
|
||||
import { getAuth } from './auth'
|
||||
|
||||
const errorHandler = (error, from) => {
|
||||
|
|
@ -25,8 +25,7 @@ export const getPost = (subreddit, threadID) => (
|
|||
// Helper function that fetches a list of comments
|
||||
const fetchComments = (commentIDs, auth) => {
|
||||
return fetchJson(`https://oauth.reddit.com/api/info?id=${commentIDs.map(id => `t1_${id}`).join()}`, auth)
|
||||
.then(results => results.data.children)
|
||||
.then(commentsData => commentsData.map(commentData => commentData.data))
|
||||
.then(results => results.data.children.map(({data}) => data))
|
||||
}
|
||||
|
||||
export const getComments = commentIDs => {
|
||||
|
|
@ -34,7 +33,7 @@ export const getComments = commentIDs => {
|
|||
.then(auth => (
|
||||
Promise.all(chunk(commentIDs, 100)
|
||||
.map(ids => fetchComments(ids, auth)))
|
||||
.then(flatten)
|
||||
.then(results => results.flat())
|
||||
))
|
||||
.catch(error => errorHandler(error, 'reddit.getComments'))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,12 +18,6 @@ export const fetchJson = (url, init = {}) =>
|
|||
})
|
||||
)
|
||||
|
||||
// Flatten arrays one level
|
||||
export const flatten = arr => arr.reduce(
|
||||
(accumulator, value) => accumulator.concat(value),
|
||||
[]
|
||||
)
|
||||
|
||||
// Take on big array and split it into an array of chunks with correct size
|
||||
export const chunk = (arr, size) => {
|
||||
const chunks = []
|
||||
|
|
|
|||
Loading…
Reference in a new issue