Fix bug where you weren't able to use both the -q and -j flags at the same time #227
3 changed files with 67 additions and 29 deletions
|
@ -5,29 +5,30 @@ export default class OutputHelper {
|
|||
public static GenerateOutput(response: IReturnResult, options: ICliOptions): string {
|
||||
const result = response.Result!;
|
||||
|
||||
const outputLines: string[] = [];
|
||||
let outputObject = {};
|
||||
|
||||
outputObject = { ...result };
|
||||
|
||||
Vylpes marked this conversation as resolved
Outdated
|
||||
if (options.queryMetadata) {
|
||||
outputObject = { ...outputObject, ...response.Query }
|
||||
}
|
||||
|
||||
if (options.json) {
|
||||
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}`);
|
||||
|
||||
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}`);
|
||||
return this.GetFriendlyObjectText(outputObject);
|
||||
}
|
||||
|
||||
return outputLines.join("\n");
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
private static GetFriendlyObjectText(object: any): string {
|
||||
const output: string[] = [];
|
||||
VylpesTester
commented
I don't think I like the way this is done, I think it should be how it was before, but when its actually printed to the terminal it should remove the appended new line I don't think I like the way this is done, I think it should be how it was before, but when its actually printed to the terminal it should remove the appended new line
|
||||
|
||||
for (const key in object) {
|
||||
output.push(`${key} = ${object[key]}`);
|
||||
Vylpes marked this conversation as resolved
Outdated
VylpesTester
commented
This will add an additional new line at the end of the output which might not look right This will add an additional new line at the end of the output which might not look right
|
||||
}
|
||||
|
||||
return output.join("\n");
|
||||
}
|
||||
/* eslint-enable @typescript-eslint/no-explicit-any */
|
||||
}
|
|
@ -3,30 +3,32 @@
|
|||
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
|
||||
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"}"`;
|
||||
|
||||
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
|
||||
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"
|
||||
subreddit = rabbits
|
||||
sortBy = hot
|
||||
limit = 100"
|
||||
`;
|
||||
|
|
|
@ -102,4 +102,39 @@ describe("GenerateOutput", () => {
|
|||
// Assert
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue
I feel like there could be a better way to do this? As now we have the output kinda splintered. I suggest maybe we have the json object created in a separate function, and then split to human-readable if
-j
isn't present