From ee4f1873d1655f1ff90bb37811df66f70cfd8112 Mon Sep 17 00:00:00 2001 From: Vylpes Date: Fri, 12 Aug 2022 17:10:03 +0100 Subject: [PATCH] Fix type error (#34) Co-authored-by: Ethan Lane Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/34 --- .drone.yml | 4 +-- package.json | 3 ++- src/contracts/IFetchResult.ts | 15 ++++++++--- src/contracts/IReturnResult.ts | 6 +++++ src/index.ts | 49 +++++++++++++++++++--------------- tsconfig.json | 4 +-- yarn.lock | 5 ++++ 7 files changed, 55 insertions(+), 31 deletions(-) create mode 100644 src/contracts/IReturnResult.ts diff --git a/.drone.yml b/.drone.yml index 14582fa..00b7c07 100644 --- a/.drone.yml +++ b/.drone.yml @@ -19,6 +19,4 @@ trigger: - feature/* - hotfix/* event: - - push - - pull_request - - tag \ No newline at end of file + - push \ No newline at end of file diff --git a/package.json b/package.json index cf11209..cb5021b 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ ], "dependencies": { "glob-parent": "^6.0.0", - "got": "^11.8.3" + "got": "^11.8.3", + "linqts": "^1.14.4" }, "scripts": { "build": "tsc", diff --git a/src/contracts/IFetchResult.ts b/src/contracts/IFetchResult.ts index e913b05..578bbea 100644 --- a/src/contracts/IFetchResult.ts +++ b/src/contracts/IFetchResult.ts @@ -1,6 +1,13 @@ -import IRedditResult from "./IRedditResult"; - export default interface IFetchResult { - IsSuccess: boolean; - Result?: IRedditResult; + data: { + archived: boolean, + downs: number, + hidden: boolean, + permalink: string, + subreddit: string, + subreddit_subscribers: number, + title: string, + ups: number, + url: string + } } \ No newline at end of file diff --git a/src/contracts/IReturnResult.ts b/src/contracts/IReturnResult.ts new file mode 100644 index 0000000..d0ba105 --- /dev/null +++ b/src/contracts/IReturnResult.ts @@ -0,0 +1,6 @@ +import IRedditResult from "./IRedditResult"; + +export default interface IReturnResult { + IsSuccess: boolean; + Result?: IRedditResult; +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index f61a93a..f9c6c6a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,8 @@ -import IFetchResult from "./contracts/IFetchResult"; +import IReturnResult from "./contracts/IReturnResult"; import IRedditResult from "./contracts/IRedditResult"; import fetch from "got"; +import { List } from 'linqts'; +import IFetchResult from "./contracts/IFetchResult"; const sortable = [ 'new', @@ -8,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, maxTries = 100): Promise { if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot'; const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`); @@ -27,32 +29,37 @@ export default async function randomBunny(subreddit: string, sortBy?: string, ma } } - const data = json.data.children; - const size = data.length; + 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() * size - 1) + 0); // Between 0 and (size - 1) + const random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1) - const randomSelect = data[random].data; + const randomSelect = dataWithImages[random]; + + if (!randomSelect) continue; + + const randomData = randomSelect.data; const redditResult: IRedditResult = { - Archived: randomSelect['archived'], - Downs: randomSelect['downs'], - Hidden: randomSelect['hidden'], - Permalink: randomSelect['permalink'], - Subreddit: randomSelect['subreddit'], - SubredditSubscribers: randomSelect['subreddit_subscribers'], - Title: randomSelect['title'], - Ups: randomSelect['ups'], - Url: randomSelect['url'] + 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 (redditResult.Url.includes('.jpg')) { - return { - IsSuccess: true, - Result: redditResult - }; - } + return { + IsSuccess: true, + Result: redditResult + }; } return { diff --git a/tsconfig.json b/tsconfig.json index f0e488f..26e2acf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ - "target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */ + "target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ // "lib": [], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */ @@ -44,7 +44,7 @@ // "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */ /* Module Resolution Options */ - // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ diff --git a/yarn.lock b/yarn.lock index 8bda94f..990ec18 100644 --- a/yarn.lock +++ b/yarn.lock @@ -704,6 +704,11 @@ levn@^0.4.1: prelude-ls "^1.2.1" type-check "~0.4.0" +linqts@^1.14.4: + version "1.14.4" + resolved "https://registry.yarnpkg.com/linqts/-/linqts-1.14.4.tgz#0aa0f78fc6be073d7db874e0a0480fda5d06db7d" + integrity sha512-b5sJjG1ZQ8iLSTJV19jWgMLoQicrQVVRkkQN7B/aboU+cf30lgnhIoGM8vEjqPxZFpryDU78PFpuTnfYNIHMeg== + lodash.clonedeep@^4.5.0: version "4.5.0" resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"