Skip to content

Commit

Permalink
feat(img): allow and returning random image by default (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
raflymln authored Nov 3, 2024
1 parent e594a5a commit e89b27b
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions packages/falso/src/lib/img.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface ImgOptions extends FakeOptions {
width?: number;
height?: number;
grayscale?: boolean;
random?: boolean;
}

/**
Expand All @@ -30,20 +31,32 @@ interface ImgOptions extends FakeOptions {
* @example
*
* randImg({ grayscale: true }) // return a grayscale image (default is false)
*
* @example
*
* randImg({ random: true }) // default is true, prevent the image from being cached
*
*/
export function randImg<Options extends ImgOptions = never>(options?: Options) {
const [width, height, grayscale] = [
const [width, height, grayscale, random] = [
options?.width ?? options?.height ?? 500,
options?.height ?? options?.width ?? 500,
options?.grayscale ?? false,
options?.random ?? true,
];

const query = new URLSearchParams();

if (grayscale) {
query.append('grayscale', '');
}

if (random) {
query.append('random', '1');
}

return fake(
() =>
`https://picsum.photos/${width}/${height}${
grayscale ? '?grayscale' : ''
}`,
() => `https://picsum.photos/${width}/${height}${query.toString()}`,
options
);
}

0 comments on commit e89b27b

Please sign in to comment.