Compare commits

...

3 commits

Author SHA1 Message Date
ee8bffa7ae Update the output to be in alphabetical order
All checks were successful
Test / build (push) Successful in 11s
2024-10-26 16:30:42 +01:00
91c88b1180 Update documentation 2024-10-26 16:28:14 +01:00
b1adc39a35 Add new tests 2024-10-26 16:22:43 +01:00
7 changed files with 64 additions and 9 deletions

View file

@ -26,6 +26,7 @@ $ random-bunny
Archived = false
Author = Rabbit_Owner
Downvotes = 0
Gallery = https://i.redd.it/sfz0srdrimjc1.png, https://i.redd.it/sfz0srdrimjc1.png
Hidden = false
Permalink = /r/Rabbits/comments/1av1rg9/cute_baby_bun/
Subreddit = Rabbits
@ -35,6 +36,8 @@ Upvotes = 211
Url = https://i.redd.it/sfz0srdrimjc1.png
```
- The `Gallery` field is only shown when there is more than 1 image returned, which then the `Url` field is the first image of that list.
## Help
The command also includes a help option in case you are stuck.
@ -72,9 +75,11 @@ $ random-bunny --json
$ randon-bunny -j
{"Archived":false,"Author":"Rabbit_Owner","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1av1rg9/cute_baby_bun/","Subreddit":"Rabbits","SubredditSubscribers":486085,"Title":"Cute baby bun","Ups":210,"Url":"https://i.redd.it/sfz0srdrimjc1.png"}
{"Archived":false,"Author":"Rabbit_Owner","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1av1rg9/cute_baby_bun/","Subreddit":"Rabbits","SubredditSubscribers":486085,"Title":"Cute baby bun","Ups":210,"Url":"https://i.redd.it/sfz0srdrimjc1.png","Gallery":["https://i.redd.it/sfz0srdrimjc1.png"]}
```
- The `Url` field is the first image of the `Gallery` array
## Sort
You can also choose the sorting option which reddit will use to return the available posts to randomise from.

View file

@ -39,6 +39,7 @@ The json string which gets returned consists of:
- archived
- author
- downs
- gallery
- hidden
- permalink
- subreddit

View file

@ -2,6 +2,7 @@ export default interface IRedditResult {
Archived: boolean,
Author: string,
Downs: number,
Gallery: string[],
Hidden: boolean,
Permalink: string,
Subreddit: string,
@ -9,5 +10,4 @@ export default interface IRedditResult {
Title: string,
Ups: number,
Url: string,
Gallery: string[],
}

View file

@ -12,10 +12,6 @@ export default class ImageHelper {
const dom = htmlparser.parseDocument(fetched.body);
const img = htmlparser.DomUtils.findAll((x => x.tagName == "img" && x.attributes.find(y => y.value.includes("https://preview.redd.it")) != null), dom.children);
if (!img) {
return [];
}
const imgSrc = img
.flatMap(x => x.attributes.find(x => x.name == "src")?.value)
.filter(x => x != undefined);

View file

@ -14,7 +14,13 @@ export default class OutputHelper {
outputLines.push(`Archived = ${result.Archived}`);
outputLines.push(`Author = ${result.Author}`);
outputLines.push(`Downvotes = ${result.Downs}`);
if (result.Gallery.length > 1) {
outputLines.push(`Gallery = ${result.Gallery.join(", ")}`);
}
outputLines.push(`Hidden = ${result.Hidden}`);
outputLines.push(`Permalink = ${result.Permalink}`);
outputLines.push(`Subreddit = ${result.Subreddit}`);
outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`);
@ -22,9 +28,6 @@ export default class OutputHelper {
outputLines.push(`Upvotes = ${result.Ups}`);
outputLines.push(`Url = ${result.Url}`);
if (result.Gallery.length > 1) {
outputLines.push(`Gallery = ${result.Gallery.join(", ")}`);
}
if (options.queryMetadata != null) {
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);

View file

@ -30,3 +30,17 @@ Query.Subreddit = rabbits
Query.Sort By = hot
Query.Limit = 100"
`;
exports[`GenerateOutput GIVEN the Gallery input has more than 1 item, EXPECT Gallery line to be added 1`] = `
"Archived = false
Author = author
Downvotes = 0
Gallery = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d, https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d
Hidden = false
Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/
Subreddit = Rabbits
Subreddit Subscribers = 654751
Title = This is my Ms Bear!
Upvotes = 17
Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"
`;

View file

@ -105,4 +105,40 @@ describe("GenerateOutput", () => {
// Assert
expect(result).toMatchSnapshot();
});
test("GIVEN the Gallery input has more than 1 item, EXPECT Gallery line to be added", () => {
// 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",
Gallery: [
"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d",
"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"
],
},
} as IReturnResult;
const options = {} as ICliOptions;
// Act
const result = OutputHelper.GenerateOutput(response, options);
// Assert
expect(result).toMatchSnapshot();
});
});