From af30ab5ea76e47160a5d7a8fd755edb06fc57aca Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 14 Sep 2024 17:57:42 +0100 Subject: [PATCH 1/9] Fix cli not allowing both -q and --json flags at the same time --- src/helpers/outputHelper.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/helpers/outputHelper.ts b/src/helpers/outputHelper.ts index 808634d..db6c7ee 100644 --- a/src/helpers/outputHelper.ts +++ b/src/helpers/outputHelper.ts @@ -8,6 +8,15 @@ export default class OutputHelper { const outputLines: string[] = []; if (options.json) { + if (options.queryMetadata != null) { + return JSON.stringify({ + ...result, + query: { + ...response.Query, + } + }) + } + return JSON.stringify(result); } -- 2.45.2 From ebf270f2bb0839a5833e0a5e65709329160a25d6 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 14 Sep 2024 17:59:12 +0100 Subject: [PATCH 2/9] Plan tests --- tests/helpers/outputHelper.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/helpers/outputHelper.test.ts b/tests/helpers/outputHelper.test.ts index 5e90dc8..2bf50db 100644 --- a/tests/helpers/outputHelper.test.ts +++ b/tests/helpers/outputHelper.test.ts @@ -102,4 +102,6 @@ describe("GenerateOutput", () => { // Assert expect(result).toMatchSnapshot(); }); + + test.todo("GIVEN options.queryMetadata AND options.json is supplied, EXPECT query metadata to be in JSON format") }); \ No newline at end of file -- 2.45.2 From bf026dae32ca1f9528eabcf6aae3930d39b1f694 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sun, 15 Sep 2024 11:55:14 +0100 Subject: [PATCH 3/9] Update tests --- .../__snapshots__/outputHelper.test.ts.snap | 2 ++ tests/helpers/outputHelper.test.ts | 35 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/tests/helpers/__snapshots__/outputHelper.test.ts.snap b/tests/helpers/__snapshots__/outputHelper.test.ts.snap index 39e952e..2c6b7e3 100644 --- a/tests/helpers/__snapshots__/outputHelper.test.ts.snap +++ b/tests/helpers/__snapshots__/outputHelper.test.ts.snap @@ -15,6 +15,8 @@ Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s exports[`GenerateOutput GIVEN options.json is true, EXPECT output to be returned as JSON 1`] = `"{"Archived":false,"Author":"author","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/","Subreddit":"Rabbits","SubredditSubscribers":654751,"Title":"This is my Ms Bear!","Ups":17,"Url":"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"}"`; +exports[`GenerateOutput GIVEN options.queryMetadata AND options.json is supplied, EXPECT query metadata to be in JSON format 1`] = `"{"Archived":false,"Author":"author","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/","Subreddit":"Rabbits","SubredditSubscribers":654751,"Title":"This is my Ms Bear!","Ups":17,"Url":"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d","query":{"subreddit":"rabbits","sortBy":"hot","limit":100}}"`; + exports[`GenerateOutput GIVEN options.queryMetadata is supplied, EXPECT query metadata to be added 1`] = ` "Archived = false Author = author diff --git a/tests/helpers/outputHelper.test.ts b/tests/helpers/outputHelper.test.ts index 2bf50db..24bd1cd 100644 --- a/tests/helpers/outputHelper.test.ts +++ b/tests/helpers/outputHelper.test.ts @@ -103,5 +103,38 @@ describe("GenerateOutput", () => { expect(result).toMatchSnapshot(); }); - test.todo("GIVEN options.queryMetadata AND options.json is supplied, EXPECT query metadata to be in JSON format") + test("GIVEN options.queryMetadata AND options.json is supplied, EXPECT query metadata to be in JSON format", () => { + // Arrange + const response = { + IsSuccess: true, + Query: { + subreddit: "rabbits", + sortBy: "hot", + limit: 100, + }, + Result: { + Archived: false, + Author: 'author', + Downs: 0, + Hidden: false, + Permalink: "/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/", + Subreddit: "Rabbits", + SubredditSubscribers: 654751, + Title: "This is my Ms Bear!", + Ups: 17, + Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d", + }, + } as IReturnResult; + + const options = { + json: true, + queryMetadata: true, + } as ICliOptions; + + // Act + const result = OutputHelper.GenerateOutput(response, options); + + // Assert + expect(result).toMatchSnapshot(); + }); }); \ No newline at end of file -- 2.45.2 From abac3442c49e01298342aa19ba195a0a83119b47 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 18 Oct 2024 18:56:15 +0100 Subject: [PATCH 4/9] Simplify outputHelper --- src/helpers/outputHelper.ts | 47 +++++++++---------- .../__snapshots__/outputHelper.test.ts.snap | 22 +++++---- 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/helpers/outputHelper.ts b/src/helpers/outputHelper.ts index db6c7ee..9f8f03f 100644 --- a/src/helpers/outputHelper.ts +++ b/src/helpers/outputHelper.ts @@ -5,38 +5,33 @@ export default class OutputHelper { public static GenerateOutput(response: IReturnResult, options: ICliOptions): string { const result = response.Result!; - const outputLines: string[] = []; + let outputObject = {}; + + outputObject = { ...result }; + + if (options.queryMetadata) { + this.AppendObject(outputObject, response.Query, "query"); + } if (options.json) { - if (options.queryMetadata != null) { - return JSON.stringify({ - ...result, - query: { - ...response.Query, - } - }) - } - - return JSON.stringify(result); + return JSON.stringify(outputObject); } - outputLines.push(`Archived = ${result.Archived}`); - outputLines.push(`Author = ${result.Author}`); - outputLines.push(`Downvotes = ${result.Downs}`); - outputLines.push(`Hidden = ${result.Hidden}`); - outputLines.push(`Permalink = ${result.Permalink}`); - outputLines.push(`Subreddit = ${result.Subreddit}`); - outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`); - outputLines.push(`Title = ${result.Title}`); - outputLines.push(`Upvotes = ${result.Ups}`); - outputLines.push(`Url = ${result.Url}`); + return this.GetFriendlyObjectText(outputObject, ""); + } - if (options.queryMetadata != null) { - outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`); - outputLines.push(`Query.Sort By = ${response.Query.sortBy}`); - outputLines.push(`Query.Limit = ${response.Query.limit}`); + private static GetFriendlyObjectText(object: any, output: string, prefix: string = ""): string { + for (let key in object) { + if (typeof(object[key]) == "object") return this.GetFriendlyObjectText(object[key], output, `${key}.`); + + output += `${prefix}${key} = ${object[key]}\n`; } - return outputLines.join("\n"); + return output; + } + + private static AppendObject(a: any, b: any, target: string): any { + a[target] = { ...b }; + return a; } } \ No newline at end of file diff --git a/tests/helpers/__snapshots__/outputHelper.test.ts.snap b/tests/helpers/__snapshots__/outputHelper.test.ts.snap index 2c6b7e3..51f5ec1 100644 --- a/tests/helpers/__snapshots__/outputHelper.test.ts.snap +++ b/tests/helpers/__snapshots__/outputHelper.test.ts.snap @@ -3,14 +3,15 @@ exports[`GenerateOutput EXPECT standout output to be returned 1`] = ` "Archived = false Author = author -Downvotes = 0 +Downs = 0 Hidden = false Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/ Subreddit = Rabbits -Subreddit Subscribers = 654751 +SubredditSubscribers = 654751 Title = This is my Ms Bear! -Upvotes = 17 -Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d" +Ups = 17 +Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d +" `; exports[`GenerateOutput GIVEN options.json is true, EXPECT output to be returned as JSON 1`] = `"{"Archived":false,"Author":"author","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/","Subreddit":"Rabbits","SubredditSubscribers":654751,"Title":"This is my Ms Bear!","Ups":17,"Url":"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"}"`; @@ -20,15 +21,16 @@ exports[`GenerateOutput GIVEN options.queryMetadata AND options.json is supplied exports[`GenerateOutput GIVEN options.queryMetadata is supplied, EXPECT query metadata to be added 1`] = ` "Archived = false Author = author -Downvotes = 0 +Downs = 0 Hidden = false Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/ Subreddit = Rabbits -Subreddit Subscribers = 654751 +SubredditSubscribers = 654751 Title = This is my Ms Bear! -Upvotes = 17 +Ups = 17 Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d -Query.Subreddit = rabbits -Query.Sort By = hot -Query.Limit = 100" +query.subreddit = rabbits +query.sortBy = hot +query.limit = 100 +" `; -- 2.45.2 From 49d986de05e43b6502690e6b08b5c69a3abef54a Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 18 Oct 2024 18:57:59 +0100 Subject: [PATCH 5/9] Fix linter --- src/helpers/outputHelper.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/helpers/outputHelper.ts b/src/helpers/outputHelper.ts index 9f8f03f..dc17cd6 100644 --- a/src/helpers/outputHelper.ts +++ b/src/helpers/outputHelper.ts @@ -20,8 +20,9 @@ export default class OutputHelper { return this.GetFriendlyObjectText(outputObject, ""); } + /* eslint-disable @typescript-eslint/no-explicit-any */ private static GetFriendlyObjectText(object: any, output: string, prefix: string = ""): string { - for (let key in object) { + for (const key in object) { if (typeof(object[key]) == "object") return this.GetFriendlyObjectText(object[key], output, `${key}.`); output += `${prefix}${key} = ${object[key]}\n`; @@ -34,4 +35,5 @@ export default class OutputHelper { a[target] = { ...b }; return a; } + /* eslint-enable @typescript-eslint/no-explicit-any */ } \ No newline at end of file -- 2.45.2 From 3b284484c27842757f5fa00b82808a3f14a05d30 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 18 Oct 2024 19:12:16 +0100 Subject: [PATCH 6/9] New line the return statement --- src/helpers/outputHelper.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/outputHelper.ts b/src/helpers/outputHelper.ts index dc17cd6..aed4eb9 100644 --- a/src/helpers/outputHelper.ts +++ b/src/helpers/outputHelper.ts @@ -23,7 +23,8 @@ export default class OutputHelper { /* eslint-disable @typescript-eslint/no-explicit-any */ private static GetFriendlyObjectText(object: any, output: string, prefix: string = ""): string { for (const key in object) { - if (typeof(object[key]) == "object") return this.GetFriendlyObjectText(object[key], output, `${key}.`); + if (typeof(object[key]) == "object") + return this.GetFriendlyObjectText(object[key], output, `${key}.`); output += `${prefix}${key} = ${object[key]}\n`; } @@ -33,7 +34,6 @@ export default class OutputHelper { private static AppendObject(a: any, b: any, target: string): any { a[target] = { ...b }; - return a; } /* eslint-enable @typescript-eslint/no-explicit-any */ } \ No newline at end of file -- 2.45.2 From 438884b270c589606a618c11320e9e70831bd8f1 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 18 Oct 2024 19:27:33 +0100 Subject: [PATCH 7/9] Make object flat --- src/helpers/outputHelper.ts | 19 +++++++------------ .../__snapshots__/outputHelper.test.ts.snap | 8 ++++---- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/helpers/outputHelper.ts b/src/helpers/outputHelper.ts index aed4eb9..621909a 100644 --- a/src/helpers/outputHelper.ts +++ b/src/helpers/outputHelper.ts @@ -5,35 +5,30 @@ export default class OutputHelper { public static GenerateOutput(response: IReturnResult, options: ICliOptions): string { const result = response.Result!; - let outputObject = {}; + let outputObject: any = {}; outputObject = { ...result }; if (options.queryMetadata) { - this.AppendObject(outputObject, response.Query, "query"); + outputObject = { ...outputObject, ...response.Query } } if (options.json) { return JSON.stringify(outputObject); } - return this.GetFriendlyObjectText(outputObject, ""); + return this.GetFriendlyObjectText(outputObject); } /* eslint-disable @typescript-eslint/no-explicit-any */ - private static GetFriendlyObjectText(object: any, output: string, prefix: string = ""): string { - for (const key in object) { - if (typeof(object[key]) == "object") - return this.GetFriendlyObjectText(object[key], output, `${key}.`); + private static GetFriendlyObjectText(object: any): string { + let output = ""; - output += `${prefix}${key} = ${object[key]}\n`; + for (const key in object) { + output += `${key} = ${object[key]}\n`; } return output; } - - private static AppendObject(a: any, b: any, target: string): any { - a[target] = { ...b }; - } /* eslint-enable @typescript-eslint/no-explicit-any */ } \ No newline at end of file diff --git a/tests/helpers/__snapshots__/outputHelper.test.ts.snap b/tests/helpers/__snapshots__/outputHelper.test.ts.snap index 51f5ec1..e9dc1f5 100644 --- a/tests/helpers/__snapshots__/outputHelper.test.ts.snap +++ b/tests/helpers/__snapshots__/outputHelper.test.ts.snap @@ -16,7 +16,7 @@ Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s exports[`GenerateOutput GIVEN options.json is true, EXPECT output to be returned as JSON 1`] = `"{"Archived":false,"Author":"author","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/","Subreddit":"Rabbits","SubredditSubscribers":654751,"Title":"This is my Ms Bear!","Ups":17,"Url":"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"}"`; -exports[`GenerateOutput GIVEN options.queryMetadata AND options.json is supplied, EXPECT query metadata to be in JSON format 1`] = `"{"Archived":false,"Author":"author","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/","Subreddit":"Rabbits","SubredditSubscribers":654751,"Title":"This is my Ms Bear!","Ups":17,"Url":"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d","query":{"subreddit":"rabbits","sortBy":"hot","limit":100}}"`; +exports[`GenerateOutput GIVEN options.queryMetadata AND options.json is supplied, EXPECT query metadata to be in JSON format 1`] = `"{"Archived":false,"Author":"author","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/","Subreddit":"Rabbits","SubredditSubscribers":654751,"Title":"This is my Ms Bear!","Ups":17,"Url":"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d","subreddit":"rabbits","sortBy":"hot","limit":100}"`; exports[`GenerateOutput GIVEN options.queryMetadata is supplied, EXPECT query metadata to be added 1`] = ` "Archived = false @@ -29,8 +29,8 @@ SubredditSubscribers = 654751 Title = This is my Ms Bear! Ups = 17 Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d -query.subreddit = rabbits -query.sortBy = hot -query.limit = 100 +subreddit = rabbits +sortBy = hot +limit = 100 " `; -- 2.45.2 From eb09754e2e31828264b085241c189f41221ae86a Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 18 Oct 2024 19:28:18 +0100 Subject: [PATCH 8/9] Fix linting --- src/helpers/outputHelper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/outputHelper.ts b/src/helpers/outputHelper.ts index 621909a..b8e2608 100644 --- a/src/helpers/outputHelper.ts +++ b/src/helpers/outputHelper.ts @@ -5,7 +5,7 @@ export default class OutputHelper { public static GenerateOutput(response: IReturnResult, options: ICliOptions): string { const result = response.Result!; - let outputObject: any = {}; + let outputObject = {}; outputObject = { ...result }; -- 2.45.2 From 67e6c160285d3680aaeb0c83247d8713d431f7df Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 26 Oct 2024 20:52:48 +0100 Subject: [PATCH 9/9] Update output helper class to not append a new line right at the end of the output --- src/helpers/outputHelper.ts | 6 +++--- tests/helpers/__snapshots__/outputHelper.test.ts.snap | 6 ++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/helpers/outputHelper.ts b/src/helpers/outputHelper.ts index b8e2608..f7b3790 100644 --- a/src/helpers/outputHelper.ts +++ b/src/helpers/outputHelper.ts @@ -22,13 +22,13 @@ export default class OutputHelper { /* eslint-disable @typescript-eslint/no-explicit-any */ private static GetFriendlyObjectText(object: any): string { - let output = ""; + const output: string[] = []; for (const key in object) { - output += `${key} = ${object[key]}\n`; + output.push(`${key} = ${object[key]}`); } - return output; + return output.join("\n"); } /* eslint-enable @typescript-eslint/no-explicit-any */ } \ No newline at end of file diff --git a/tests/helpers/__snapshots__/outputHelper.test.ts.snap b/tests/helpers/__snapshots__/outputHelper.test.ts.snap index e9dc1f5..ff9175a 100644 --- a/tests/helpers/__snapshots__/outputHelper.test.ts.snap +++ b/tests/helpers/__snapshots__/outputHelper.test.ts.snap @@ -10,8 +10,7 @@ Subreddit = Rabbits SubredditSubscribers = 654751 Title = This is my Ms Bear! Ups = 17 -Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d -" +Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d" `; exports[`GenerateOutput GIVEN options.json is true, EXPECT output to be returned as JSON 1`] = `"{"Archived":false,"Author":"author","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/","Subreddit":"Rabbits","SubredditSubscribers":654751,"Title":"This is my Ms Bear!","Ups":17,"Url":"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"}"`; @@ -31,6 +30,5 @@ Ups = 17 Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d subreddit = rabbits sortBy = hot -limit = 100 -" +limit = 100" `; -- 2.45.2