Remove maxTries variable
This commit is contained in:
parent
bb9f092731
commit
089f693413
2 changed files with 25 additions and 27 deletions
2
app.ts
2
app.ts
|
@ -1,7 +1,7 @@
|
||||||
import randomBunny from "./dist";
|
import randomBunny from "./dist";
|
||||||
|
|
||||||
async function app() {
|
async function app() {
|
||||||
const result = await randomBunny('rabbits', 'hot', 100);
|
const result = await randomBunny('rabbits', 'hot');
|
||||||
|
|
||||||
console.log(result);
|
console.log(result);
|
||||||
}
|
}
|
||||||
|
|
50
src/index.ts
50
src/index.ts
|
@ -10,7 +10,7 @@ const sortable = [
|
||||||
'top'
|
'top'
|
||||||
];
|
];
|
||||||
|
|
||||||
export default async function randomBunny(subreddit: string, sortBy?: string, maxTries = 100): Promise<IReturnResult> {
|
export default async function randomBunny(subreddit: string, sortBy?: string): Promise<IReturnResult> {
|
||||||
if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot';
|
if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot';
|
||||||
|
|
||||||
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`);
|
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 data: IFetchResult[] = json.data.children;
|
||||||
|
|
||||||
const dataWithImages = new List<IFetchResult>(data)
|
const dataWithImages = new List<IFetchResult>(data)
|
||||||
.Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png'))
|
.Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png'))
|
||||||
.ToArray();
|
.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];
|
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']
|
|
||||||
};
|
|
||||||
|
|
||||||
|
if (!randomSelect) {
|
||||||
return {
|
return {
|
||||||
IsSuccess: true,
|
IsSuccess: false,
|
||||||
Result: redditResult
|
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
|
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 {
|
return {
|
||||||
IsSuccess: false
|
IsSuccess: true,
|
||||||
}
|
Result: redditResult
|
||||||
|
};
|
||||||
}
|
}
|
Loading…
Reference in a new issue