From 089f6934134855e65c6fd0f0487c3000fe7a996c Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 22 Feb 2023 18:18:50 +0000 Subject: [PATCH] Remove maxTries variable --- app.ts | 2 +- src/index.ts | 50 ++++++++++++++++++++++++-------------------------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/app.ts b/app.ts index 1ad56df..5099496 100644 --- a/app.ts +++ b/app.ts @@ -1,7 +1,7 @@ import randomBunny from "./dist"; async function app() { - const result = await randomBunny('rabbits', 'hot', 100); + const result = await randomBunny('rabbits', 'hot'); console.log(result); } diff --git a/src/index.ts b/src/index.ts index f9c6c6a..6af1875 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,7 +10,7 @@ const sortable = [ 'top' ]; -export default async function randomBunny(subreddit: string, sortBy?: string, maxTries = 100): Promise { +export default async function randomBunny(subreddit: string, sortBy?: string): Promise { if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot'; const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`); @@ -30,39 +30,37 @@ export default async function randomBunny(subreddit: string, sortBy?: string, ma } const data: IFetchResult[] = json.data.children; - + const dataWithImages = new List(data) .Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png')) .ToArray(); - for (let i = 0; i < maxTries; i++) { - const random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1) + const random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1) - const randomSelect = dataWithImages[random]; - - if (!randomSelect) continue; - - const randomData = randomSelect.data; - - const redditResult: IRedditResult = { - Archived: randomData['archived'], - Downs: randomData['downs'], - Hidden: randomData['hidden'], - Permalink: randomData['permalink'], - Subreddit: randomData['subreddit'], - SubredditSubscribers: randomData['subreddit_subscribers'], - Title: randomData['title'], - Ups: randomData['ups'], - Url: randomData['url'] - }; + const randomSelect = dataWithImages[random]; + if (!randomSelect) { return { - IsSuccess: true, - Result: redditResult + IsSuccess: false, }; - } + }; + + const randomData = randomSelect.data; + + const redditResult: IRedditResult = { + Archived: randomData['archived'], + Downs: randomData['downs'], + Hidden: randomData['hidden'], + Permalink: randomData['permalink'], + Subreddit: randomData['subreddit'], + SubredditSubscribers: randomData['subreddit_subscribers'], + Title: randomData['title'], + Ups: randomData['ups'], + Url: randomData['url'] + }; return { - IsSuccess: false - } + IsSuccess: true, + Result: redditResult + }; } \ No newline at end of file