Remove MaxTries variable #51
4 changed files with 35 additions and 36 deletions
4
app.ts
4
app.ts
|
@ -1,7 +1,7 @@
|
|||
import randomBunny from "./src";
|
||||
import randomBunny from "./dist";
|
||||
|
||||
async function app() {
|
||||
const result = await randomBunny('rabbits', 'hot', 100);
|
||||
const result = await randomBunny('rabbits', 'hot');
|
||||
|
||||
console.log(result);
|
||||
}
|
||||
|
|
11
package.json
11
package.json
|
@ -1,11 +1,11 @@
|
|||
{
|
||||
"name": "random-bunny",
|
||||
"version": "2.0.3",
|
||||
"version": "2.0.5",
|
||||
"description": "Get a random subreddit image url",
|
||||
"license": "MIT",
|
||||
"author": "Vylpes",
|
||||
"main": "./dist/index",
|
||||
"typings": "./dist",
|
||||
"main": "./dist/index.js",
|
||||
"typings": "./dist/index.d.ts",
|
||||
"keywords": [
|
||||
"rabbit",
|
||||
"bunny",
|
||||
|
@ -37,5 +37,8 @@
|
|||
"eslint": "^7.17.0",
|
||||
"ts-node": "^10.4.0",
|
||||
"typescript": "^4.5.2"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
]
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ yarn add random-bunny
|
|||
import randomBunny from "random-bunny";
|
||||
|
||||
// ... In an async function
|
||||
const result = await randomBunny('rabbits', 'hot', 100);
|
||||
const result = await randomBunny('rabbits', 'hot');
|
||||
console.log(result);
|
||||
```
|
||||
|
||||
|
@ -29,7 +29,7 @@ console.log(result);
|
|||
|
||||
### `randomBunny()`
|
||||
|
||||
Returns a `json string` for a random post. Accepts 3 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'), `maxTries?` (default 100)
|
||||
Returns a `json string` for a random post. Accepts 2 arguments: `subreddit`, and `sortby` ('new', 'hot', 'top')
|
||||
|
||||
The json string which gets returned consists of:
|
||||
- archived
|
||||
|
@ -44,8 +44,6 @@ The json string which gets returned consists of:
|
|||
|
||||
`sortBy` will default to 'hot' if not given or invalid
|
||||
|
||||
`maxTries` prevents the script from rerolling too many times. The script rerolls the randomiser if the post its given doesn't contain an image. Default 100.
|
||||
|
||||
## Notes
|
||||
|
||||
* Node 4 or newer.
|
||||
|
|
48
src/index.ts
48
src/index.ts
|
@ -10,7 +10,7 @@ const sortable = [
|
|||
'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';
|
||||
|
||||
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`);
|
||||
|
@ -35,34 +35,32 @@ export default async function randomBunny(subreddit: string, sortBy?: string, ma
|
|||
.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
|
||||
};
|
||||
}
|
Loading…
Reference in a new issue