Simplify outputHelper

This commit is contained in:
Ethan Lane 2024-10-18 18:56:15 +01:00
parent f4d447f66f
commit abac3442c4
2 changed files with 33 additions and 36 deletions

View file

@ -5,38 +5,33 @@ export default class OutputHelper {
public static GenerateOutput(response: IReturnResult, options: ICliOptions): string { public static GenerateOutput(response: IReturnResult, options: ICliOptions): string {
const result = response.Result!; 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.json) {
if (options.queryMetadata != null) { return JSON.stringify(outputObject);
return JSON.stringify({
...result,
query: {
...response.Query,
}
})
} }
return JSON.stringify(result); return this.GetFriendlyObjectText(outputObject, "");
} }
outputLines.push(`Archived = ${result.Archived}`); private static GetFriendlyObjectText(object: any, output: string, prefix: string = ""): string {
outputLines.push(`Author = ${result.Author}`); for (let key in object) {
outputLines.push(`Downvotes = ${result.Downs}`); if (typeof(object[key]) == "object") return this.GetFriendlyObjectText(object[key], output, `${key}.`);
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}`);
if (options.queryMetadata != null) { output += `${prefix}${key} = ${object[key]}\n`;
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
outputLines.push(`Query.Sort By = ${response.Query.sortBy}`);
outputLines.push(`Query.Limit = ${response.Query.limit}`);
} }
return outputLines.join("\n"); return output;
}
private static AppendObject(a: any, b: any, target: string): any {
a[target] = { ...b };
return a;
} }
} }

View file

@ -3,14 +3,15 @@
exports[`GenerateOutput EXPECT standout output to be returned 1`] = ` exports[`GenerateOutput EXPECT standout output to be returned 1`] = `
"Archived = false "Archived = false
Author = author Author = author
Downvotes = 0 Downs = 0
Hidden = false Hidden = false
Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/ Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/
Subreddit = Rabbits Subreddit = Rabbits
Subreddit Subscribers = 654751 SubredditSubscribers = 654751
Title = This is my Ms Bear! 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" 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"}"`; 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`] = ` exports[`GenerateOutput GIVEN options.queryMetadata is supplied, EXPECT query metadata to be added 1`] = `
"Archived = false "Archived = false
Author = author Author = author
Downvotes = 0 Downs = 0
Hidden = false Hidden = false
Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/ Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/
Subreddit = Rabbits Subreddit = Rabbits
Subreddit Subscribers = 654751 SubredditSubscribers = 654751
Title = This is my Ms Bear! 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 Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d
Query.Subreddit = rabbits query.subreddit = rabbits
Query.Sort By = hot query.sortBy = hot
Query.Limit = 100" query.limit = 100
"
`; `;