From 2de967fa5674c05478ad2ce0c8359d6fc57bfb38 Mon Sep 17 00:00:00 2001 From: Christopher Gurnee Date: Sun, 14 Nov 2021 09:23:22 -0500 Subject: [PATCH] Replace unflatten() with Array.flat() --- src/api/reddit/index.js | 7 +++---- src/utils.js | 6 ------ 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/api/reddit/index.js b/src/api/reddit/index.js index 4281776..ddba27a 100644 --- a/src/api/reddit/index.js +++ b/src/api/reddit/index.js @@ -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')) } diff --git a/src/utils.js b/src/utils.js index 544d796..36f50fc 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 = []