diff --git a/.gitea/ISSUE_TEMPLATE.md b/.gitea/ISSUE_TEMPLATE.md deleted file mode 100644 index 090d6b3..0000000 --- a/.gitea/ISSUE_TEMPLATE.md +++ /dev/null @@ -1,18 +0,0 @@ -Epic: \ -Story Points: - ---- - -*No description* - -## Acceptance Criteria - -*No acceptance criteria* - -## Subtasks - -*No subtasks* - -## Notes - -*No notes* \ No newline at end of file diff --git a/.gitea/PULL_REQUEST_TEMPLATE.md b/.gitea/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 698d07e..0000000 --- a/.gitea/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,29 +0,0 @@ -# Description - -Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - -Fixes # (issue) - -## Type of change - -Please delete options that are not relevant. - -- [ ] Bug fix (non-breaking change which fixes an issue) -- [ ] New feature (non-breaking change which adds functionality) -- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) -- [ ] This change requires a documentation update - -# How Has This Been Tested? - -Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. - -# Checklist - -- [ ] My code follows the style guidelines of this project -- [ ] I have performed a self-review of my own code -- [ ] I have commented my code, particularly in hard-to-understand areas -- [ ] I have made corresponding changes to the documentation -- [ ] My changes generate no new warnings -- [ ] I have added tests that provde my fix is effective or that my feature works -- [ ] New and existing unit tests pass locally with my changes -- [ ] Any dependent changes have been merged and published in downstream modules \ No newline at end of file diff --git a/app.ts b/app.ts index 5099496..3d66d96 100644 --- a/app.ts +++ b/app.ts @@ -1,7 +1,7 @@ -import randomBunny from "./dist"; +import randomBunny from "./src"; async function app() { - const result = await randomBunny('rabbits', 'hot'); + const result = await randomBunny('rabbits', 'hot', 100); console.log(result); } diff --git a/package.json b/package.json index 111953f..8fd042c 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "random-bunny", - "version": "2.0.5", + "version": "2.0.3", "description": "Get a random subreddit image url", "license": "MIT", "author": "Vylpes", - "main": "./dist/index.js", - "typings": "./dist/index.d.ts", + "main": "./dist/index", + "typings": "./dist", "keywords": [ "rabbit", "bunny", @@ -33,12 +33,9 @@ "homepage": "https://gitea.vylpes.xyz/RabbitLabs/random-bunny", "funding": "https://ko-fi.com/vylpes", "devDependencies": { - "@types/node": "^18.0.0", + "@types/node": "^16.11.11", "eslint": "^8.0.0", "ts-node": "^10.4.0", "typescript": "^4.5.2" - }, - "files": [ - "dist" - ] + } } diff --git a/readme.md b/readme.md index 4403cd6..d129cf3 100644 --- a/readme.md +++ b/readme.md @@ -21,7 +21,7 @@ yarn add random-bunny import randomBunny from "random-bunny"; // ... In an async function -const result = await randomBunny('rabbits', 'hot'); +const result = await randomBunny('rabbits', 'hot', 100); console.log(result); ``` @@ -29,7 +29,7 @@ console.log(result); ### `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: - archived @@ -44,6 +44,8 @@ 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. diff --git a/src/index.ts b/src/index.ts index 6af1875..f9c6c6a 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): Promise { +export default async function randomBunny(subreddit: string, sortBy?: string, maxTries = 100): Promise { if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot'; const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`); @@ -30,37 +30,39 @@ export default async function randomBunny(subreddit: string, sortBy?: string): P } const data: IFetchResult[] = json.data.children; - + const dataWithImages = new List(data) .Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png')) .ToArray(); - const random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1) + for (let i = 0; i < maxTries; i++) { + 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) { - return { - IsSuccess: false, + 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 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: true, + Result: redditResult + }; + } return { - IsSuccess: true, - Result: redditResult - }; + IsSuccess: false + } } \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index dbcc2a7..7a23727 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14,10 +14,10 @@ dependencies: "@cspotcode/source-map-consumer" "0.8.0" -"@eslint/eslintrc@^2.0.0": - version "2.0.0" - resolved "https://registry.yarnpkg.com/@eslint/eslintrc/-/eslintrc-2.0.0.tgz#943309d8697c52fc82c076e90c1c74fbbe69dbff" - integrity sha512-fluIaaV+GyV24CCu/ggiHdV+j4RNh85yQnAYS/G2mZODZgGmmlrgCydjUcV3YvxCm9x8nMAfThsqTni4KiXT4A== +"@eslint/eslintrc@^1.4.0": + version "1.4.0" + resolved "https://registry.yarnpkg.com/@eslint/eslintrc/-/eslintrc-1.4.0.tgz#8ec64e0df3e7a1971ee1ff5158da87389f167a63" + integrity sha512-7yfvXy6MWLgWSFsLhz5yH3iQ52St8cdUY6FoGieKkRDVxuxmrNuUetIuu6cmjNWwniUHiWXjxCr5tTXDrbYS5A== dependencies: ajv "^6.12.4" debug "^4.3.2" @@ -29,11 +29,6 @@ minimatch "^3.1.2" strip-json-comments "^3.1.1" -"@eslint/js@8.35.0": - version "8.35.0" - resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.35.0.tgz#b7569632b0b788a0ca0e438235154e45d42813a7" - integrity sha512-JXdzbRiWclLVoD8sNUjR443VVlYqiYmDVT6rGUEIEHU5YJW0gaVZwV2xgM7D4arkvASqD0IlLUVjHiFuxaftRw== - "@humanwhocodes/config-array@^0.11.8": version "0.11.8" resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.11.8.tgz#03595ac2075a4dc0f191cc2131de14fbd7d410b9" @@ -128,16 +123,11 @@ dependencies: "@types/node" "*" -"@types/node@*": +"@types/node@*", "@types/node@^16.11.11": version "16.11.11" resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.11.tgz#6ea7342dfb379ea1210835bada87b3c512120234" integrity sha512-KB0sixD67CeecHC33MYn+eYARkqTheIRNuu97y2XMjR7Wu3XibO1vaY6VBV6O/a89SPI81cEUIYT87UqUWlZNw== -"@types/node@^18.0.0": - version "18.11.18" - resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.18.tgz#8dfb97f0da23c2293e554c5a50d61ef134d7697f" - integrity sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA== - "@types/responselike@*", "@types/responselike@^1.0.0": version "1.0.0" resolved "https://registry.yarnpkg.com/@types/responselike/-/responselike-1.0.0.tgz#251f4fe7d154d2bad125abe1b429b23afd262e29" @@ -161,9 +151,9 @@ acorn@^8.4.1: integrity sha512-U1riIR+lBSNi3IbxtaHOIKdH8sLFv3NYfNv8sg7ZsNhcfl4HF2++BfqqrNAxoCLQW1iiylOj76ecnaUxz+z9yw== acorn@^8.8.0: - version "8.8.2" - resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.8.2.tgz#1b2f25db02af965399b9776b0c2c391276d37c4a" - integrity sha512-xjIYgE8HBrkpd/sJqOGNspf8uHG+NOHGOw6a/Urj8taM2EXfdNAH2oFcPeIFfsv3+kz/mJrS5VuMqbNLjCa2vw== + version "8.8.1" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.8.1.tgz#0a3f9cbecc4ec3bea6f0a80b66ae8dd2da250b73" + integrity sha512-7zFpHzhnqYKrkYdUjF1HI1bzd0VygEGX8lFk4k5zVMqHEoES+P+7TKI+EvLO9WVMJ8eekdO0aDEK044xTXwPPA== ajv@^6.10.0, ajv@^6.12.4: version "6.12.6" @@ -360,12 +350,11 @@ eslint-visitor-keys@^3.3.0: integrity sha512-mQ+suqKJVyeuwGYHAdjMFqjCyfl8+Ldnxuyp3ldiMBFKkvytrXUZWaiPCEav8qDHKty44bD+qV1IP4T+w+xXRA== eslint@^8.0.0: - version "8.35.0" - resolved "https://registry.yarnpkg.com/eslint/-/eslint-8.35.0.tgz#fffad7c7e326bae606f0e8f436a6158566d42323" - integrity sha512-BxAf1fVL7w+JLRQhWl2pzGeSiGqbWumV4WNvc9Rhp6tiCtm4oHnyPBSEtMGZwrQgudFQ+otqzWoPB7x+hxoWsw== + version "8.30.0" + resolved "https://registry.yarnpkg.com/eslint/-/eslint-8.30.0.tgz#83a506125d089eef7c5b5910eeea824273a33f50" + integrity sha512-MGADB39QqYuzEGov+F/qb18r4i7DohCDOfatHaxI2iGlPuC65bwG2gxgO+7DkyL38dRFaRH7RaRAgU6JKL9rMQ== dependencies: - "@eslint/eslintrc" "^2.0.0" - "@eslint/js" "8.35.0" + "@eslint/eslintrc" "^1.4.0" "@humanwhocodes/config-array" "^0.11.8" "@humanwhocodes/module-importer" "^1.0.1" "@nodelib/fs.walk" "^1.2.8" @@ -379,7 +368,7 @@ eslint@^8.0.0: eslint-utils "^3.0.0" eslint-visitor-keys "^3.3.0" espree "^9.4.0" - esquery "^1.4.2" + esquery "^1.4.0" esutils "^2.0.2" fast-deep-equal "^3.1.3" file-entry-cache "^6.0.1" @@ -414,10 +403,10 @@ espree@^9.4.0: acorn-jsx "^5.3.2" eslint-visitor-keys "^3.3.0" -esquery@^1.4.2: - version "1.4.2" - resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.4.2.tgz#c6d3fee05dd665808e2ad870631f221f5617b1d1" - integrity sha512-JVSoLdTlTDkmjFmab7H/9SL9qGSyjElT3myyKp7krqjVFQCDLmj1QFaCLRFBszBKI0XVZaiiXvuPIX3ZwHe1Ng== +esquery@^1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.4.0.tgz#2148ffc38b82e8c7057dfed48425b3e61f0f24a5" + integrity sha512-cCDispWt5vHHtwMY2YrAQ4ibFkAL8RbH5YGBnZBc90MolvvfkkQcJro/aZiAQUlQ3qgrYS6D6v8Gc5G5CQsc9w== dependencies: estraverse "^5.1.0" @@ -454,9 +443,9 @@ fast-levenshtein@^2.0.6: integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc= fastq@^1.6.0: - version "1.15.0" - resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.15.0.tgz#d04d07c6a2a68fe4599fea8d2e103a937fae6b3a" - integrity sha512-wBrocU2LCXXa+lWBt8RoIRD89Fi8OdABODa/kEnyeyjS5aZO5/GNvI5sEINADqP/h8M29UHTHUb53sUu5Ihqdw== + version "1.14.0" + resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.14.0.tgz#107f69d7295b11e0fccc264e1fc6389f623731ce" + integrity sha512-eR2D+V9/ExcbF9ls441yIuN6TI2ED1Y2ZcA5BmMtJsOkWOFRJQ0Jt0g1UwqXJJVAb+V+umH5Dfr8oh4EVP7VVg== dependencies: reusify "^1.0.4" @@ -527,9 +516,9 @@ glob@^7.1.3: path-is-absolute "^1.0.0" globals@^13.19.0: - version "13.20.0" - resolved "https://registry.yarnpkg.com/globals/-/globals-13.20.0.tgz#ea276a1e508ffd4f1612888f9d1bad1e2717bf82" - integrity sha512-Qg5QtVkCy/kv3FUSlu4ukeZDVf9ee0iXLAUYX13gbR17bnejFTzr4iS9bY7kwCf1NztRNm1t91fjOiyx4CSwPQ== + version "13.19.0" + resolved "https://registry.yarnpkg.com/globals/-/globals-13.19.0.tgz#7a42de8e6ad4f7242fbcca27ea5b23aca367b5c8" + integrity sha512-dkQ957uSRWHw7CFXLUtUHQI3g3aWApYhfNR2O6jn/907riyTYKVBmxYVROkBcY614FSSeSJh7Xm7SrUWCxvJMQ== dependencies: type-fest "^0.20.2" @@ -634,9 +623,9 @@ isexe@^2.0.0: integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA= js-sdsl@^4.1.4: - version "4.3.0" - resolved "https://registry.yarnpkg.com/js-sdsl/-/js-sdsl-4.3.0.tgz#aeefe32a451f7af88425b11fdb5f58c90ae1d711" - integrity sha512-mifzlm2+5nZ+lEcLJMoBK0/IH/bDg8XnJfd/Wq6IP+xoCjLZsTOnV2QpxlVbX9bMnkl5PdEjNtBJ9Cj1NjifhQ== + version "4.2.0" + resolved "https://registry.yarnpkg.com/js-sdsl/-/js-sdsl-4.2.0.tgz#278e98b7bea589b8baaf048c20aeb19eb7ad09d0" + integrity sha512-dyBIzQBDkCqCu+0upx25Y2jGdbTGxE9fshMsCdK0ViOongpV+n5tXRcZY9v7CaVQ79AGS9KA1KHtojxiM7aXSQ== js-yaml@^4.1.0: version "4.1.0"