Simplify outputHelper
This commit is contained in:
parent
f4d447f66f
commit
abac3442c4
2 changed files with 33 additions and 36 deletions
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue