aboutsummaryrefslogtreecommitdiff
path: root/src/modules/image/providers/lorempicsum.ts
blob: 6299cf1bef10a3811d661862a624987641b43c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* eslint-disable deprecation/deprecation */
import type { Faker } from '../../..';
import { deprecated } from '../../../internal/deprecated';

/**
 * Module to generate links to random images on https://picsum.photos.
 *
 * @deprecated Use `faker.image.urlPicsumPhotos` instead.
 */
export class LoremPicsum {
  constructor(private readonly faker: Faker) {}

  /**
   * Generates a new picsum photos image url.
   *
   * @param width The width of the image. Defaults to `640`.
   * @param height The height of the image. Defaults to `480`.
   * @param grayscale Whether to return a grayscale image. Default to `false`.
   * @param blur The optional level of blur to apply. Supports `1` - `10`.
   *
   * @deprecated Use `faker.image.urlPicsumPhotos` instead.
   */
  image(
    width?: number,
    height?: number,
    grayscale?: boolean,
    blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
  ): string {
    deprecated({
      deprecated: 'faker.lorempicsum.image',
      proposed: 'faker.image.urlPicsumPhotos',
      since: '8.0',
      until: '9.0',
    });
    return this.imageUrl(width, height, grayscale, blur);
  }

  /**
   * Generates a new picsum photos image url.
   *
   * @param width The width of the image. Defaults to `640`.
   * @param height The height of the image. Defaults to `480`.
   * @param grayscale Whether to return a grayscale image. Default to `false`.
   *
   * @deprecated Use `faker.image.urlPicsumPhotos` instead.
   */
  imageGrayscale(width?: number, height?: number, grayscale?: boolean): string {
    deprecated({
      deprecated: 'faker.lorempicsum.imageGrayscale',
      proposed: 'faker.image.urlPicsumPhotos',
      since: '8.0',
      until: '9.0',
    });
    return this.imageUrl(width, height, grayscale);
  }

  /**
   * Generates a new picsum photos image url.
   *
   * @param width The width of the image. Defaults to `640`.
   * @param height The height of the image. Defaults to `480`.
   * @param blur The optional level of blur to apply. Supports `1` - `10`.
   *
   * @deprecated Use `faker.image.urlPicsumPhotos` instead.
   */
  imageBlurred(
    width?: number,
    height?: number,
    blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
  ): string {
    deprecated({
      deprecated: 'faker.lorempicsum.imageBlurred',
      proposed: 'faker.image.urlPicsumPhotos',
      since: '8.0',
      until: '9.0',
    });
    return this.imageUrl(width, height, undefined, blur);
  }

  /**
   * Generates a new picsum photos image url.
   *
   * @param width The width of the image. Defaults to `640`.
   * @param height The height of the image. Defaults to `480`.
   * @param grayscale Whether to return a grayscale image. Default to `false`.
   * @param blur The optional level of blur to apply. Supports `1` - `10`.
   * @param seed The optional seed to use.
   *
   * @deprecated Use `faker.image.urlPicsumPhotos` instead.
   */
  imageRandomSeeded(
    width?: number,
    height?: number,
    grayscale?: boolean,
    blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10,
    seed?: string
  ): string {
    deprecated({
      deprecated: 'faker.lorempicsum.imageRandomSeeded',
      proposed: 'faker.image.urlPicsumPhotos',
      since: '8.0',
      until: '9.0',
    });
    // TODO @ST-DDT 2022-03-11: This method does the same as image url, maybe generate a seed, if it is missing?
    return this.imageUrl(width, height, grayscale, blur, seed);
  }

  /**
   * Generates a new picsum photos image url.
   *
   * @param width The width of the image. Defaults to `640`.
   * @param height The height of the image. Defaults to `480`.
   * @param grayscale Whether to return a grayscale image. Default to `false`.
   * @param blur The optional level of blur to apply. Supports `1` - `10`.
   * @param seed The optional seed to use.
   *
   * @deprecated Use `faker.image.urlPicsumPhotos` instead.
   */
  imageUrl(
    width?: number,
    height?: number,
    grayscale?: boolean,
    blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10,
    seed?: string
  ): string {
    deprecated({
      deprecated: 'faker.lorempicsum.imageUrl',
      proposed: 'faker.image.urlPicsumPhotos',
      since: '8.0',
      until: '9.0',
    });
    width = width || 640;
    height = height || 480;

    let url = 'https://picsum.photos';

    if (seed) {
      url += `/seed/${seed}`;
    }

    url += `/${width}/${height}`;

    if (grayscale && blur) {
      return `${url}?grayscale&blur=${blur}`;
    }

    if (grayscale) {
      return `${url}?grayscale`;
    }

    if (blur) {
      return `${url}?blur=${blur}`;
    }

    return url;
  }
}