From e89b27b7c28044eb7affaf35c5e24e9002036368 Mon Sep 17 00:00:00 2001 From: Rafly Maulana Date: Sun, 3 Nov 2024 17:13:10 +0700 Subject: [PATCH] feat(img): allow and returning random image by default (#385) --- packages/falso/src/lib/img.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/falso/src/lib/img.ts b/packages/falso/src/lib/img.ts index 2824da7b9..d1184d7ea 100644 --- a/packages/falso/src/lib/img.ts +++ b/packages/falso/src/lib/img.ts @@ -4,6 +4,7 @@ interface ImgOptions extends FakeOptions { width?: number; height?: number; grayscale?: boolean; + random?: boolean; } /** @@ -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?: 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 ); }