Compare commits

..

No commits in common. "948f513b53f8bc7afc55a2ae135d5b9e405158f4" and "f6d934c80fc31a751d92ff407f04d0455568e203" have entirely different histories.

4 changed files with 36 additions and 35 deletions

4
app.ts
View file

@ -1,7 +1,7 @@
import randomBunny from "./dist"; import randomBunny from "./src";
async function app() { async function app() {
const result = await randomBunny('rabbits', 'hot'); const result = await randomBunny('rabbits', 'hot', 100);
console.log(result); console.log(result);
} }

View file

@ -1,11 +1,11 @@
{ {
"name": "random-bunny", "name": "random-bunny",
"version": "2.0.5", "version": "2.0.3",
"description": "Get a random subreddit image url", "description": "Get a random subreddit image url",
"license": "MIT", "license": "MIT",
"author": "Vylpes", "author": "Vylpes",
"main": "./dist/index.js", "main": "./dist/index",
"typings": "./dist/index.d.ts", "typings": "./dist",
"keywords": [ "keywords": [
"rabbit", "rabbit",
"bunny", "bunny",
@ -37,8 +37,5 @@
"eslint": "^7.17.0", "eslint": "^7.17.0",
"ts-node": "^10.4.0", "ts-node": "^10.4.0",
"typescript": "^4.5.2" "typescript": "^4.5.2"
}, }
"files": [
"dist"
]
} }

View file

@ -21,7 +21,7 @@ yarn add random-bunny
import randomBunny from "random-bunny"; import randomBunny from "random-bunny";
// ... In an async function // ... In an async function
const result = await randomBunny('rabbits', 'hot'); const result = await randomBunny('rabbits', 'hot', 100);
console.log(result); console.log(result);
``` ```
@ -29,7 +29,7 @@ console.log(result);
### `randomBunny()` ### `randomBunny()`
Returns a `json string` for a random post. Accepts 2 arguments: `subreddit`, and `sortby` ('new', 'hot', 'top') Returns a `json string` for a random post. Accepts 3 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'), `maxTries?` (default 100)
The json string which gets returned consists of: The json string which gets returned consists of:
- archived - archived
@ -44,6 +44,8 @@ The json string which gets returned consists of:
`sortBy` will default to 'hot' if not given or invalid `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 ## Notes
* Node 4 or newer. * Node 4 or newer.

View file

@ -10,7 +10,7 @@ const sortable = [
'top' 'top'
]; ];
export default async function randomBunny(subreddit: string, sortBy?: string): Promise<IReturnResult> { export default async function randomBunny(subreddit: string, sortBy?: string, maxTries = 100): 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`);
@ -35,15 +35,12 @@ export default async function randomBunny(subreddit: string, sortBy?: string): P
.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) { if (!randomSelect) continue;
return {
IsSuccess: false,
};
};
const randomData = randomSelect.data; const randomData = randomSelect.data;
@ -63,4 +60,9 @@ export default async function randomBunny(subreddit: string, sortBy?: string): P
IsSuccess: true, IsSuccess: true,
Result: redditResult Result: redditResult
}; };
}
return {
IsSuccess: false
}
} }