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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
import type { Faker } from '../../faker';
import { deprecated } from '../../internal/deprecated';
/**
* Module to generate commerce and product related entries.
*
* ### Overview
*
* For a long product name like `'Incredible Soft Gloves'`, use [`productName()`](https://next.fakerjs.dev/api/commerce.html#productname). The product names are generated from a list of adjectives, materials, and products, which can each be accessed separately using [`productAdjective()`](https://next.fakerjs.dev/api/commerce.html#productadjective), [`productMaterial()`](https://next.fakerjs.dev/api/commerce.html#productmaterial), and [`product()`](https://next.fakerjs.dev/api/commerce.html#product). You can also create a description using [`productDescription()`](https://next.fakerjs.dev/api/commerce.html#productdescription).
*
* For a department in a shop or product category, use [`department()`](https://next.fakerjs.dev/api/commerce.html#department).
*
* You can also create a price using [`price()`](https://next.fakerjs.dev/api/commerce.html#price).
*/
export class CommerceModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(
CommerceModule.prototype
) as Array<keyof CommerceModule | 'constructor'>) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}
this[name] = this[name].bind(this);
}
}
/**
* Returns a department inside a shop.
*
* @example
* faker.commerce.department() // 'Garden'
*
* @since 3.0.0
*/
department(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.commerce.department
);
}
/**
* Generates a random descriptive product name.
*
* @example
* faker.commerce.productName() // 'Incredible Soft Gloves'
*
* @since 3.0.0
*/
productName(): string {
return `${this.productAdjective()} ${this.productMaterial()} ${this.product()}`;
}
/**
* Generates a price between min and max (inclusive).
*
* @param options An options object. Defaults to `{}`.
* @param options.min The minimum price. Defaults to `1`.
* @param options.max The maximum price. Defaults to `1000`.
* @param options.dec The number of decimal places. Defaults to `2`.
* @param options.symbol The currency value to use. Defaults to `''`.
*
* @example
* faker.commerce.price() // 828.00
* faker.commerce.price({ min: 100 }) // 904.00
* faker.commerce.price({ min: 100, max: 200 }) // 154.00
* faker.commerce.price({ min: 100, max: 200, dec: 0 }) // 133
* faker.commerce.price({ min: 100, max: 200, dec: 0, symbol: '$' }) // $114
*
* @since 3.0.0
*/
price(options?: {
/**
* The minimum price.
*
* @default 1
*/
min?: number;
/**
* The maximum price.
*
* @default 1000
*/
max?: number;
/**
* The number of decimal places.
*
* @default 2
*/
dec?: number;
/**
* The currency value to use.
*
* @default ''
*/
symbol?: string;
}): string;
/**
* Generates a price between min and max (inclusive).
*
* @param min The minimum price. Defaults to `1`.
* @param max The maximum price. Defaults to `1000`.
* @param dec The number of decimal places. Defaults to `2`.
* @param symbol The currency value to use. Defaults to `''`.
*
* @example
* faker.commerce.price() // 828.00
* faker.commerce.price(100) // 904.00
* faker.commerce.price(100, 200) // 154.00
* faker.commerce.price(100, 200, 0) // 133
* faker.commerce.price(100, 200, 0, '$') // $114
*
* @since 3.0.0
*
* @deprecated Use `faker.commerce.price({ min, max, dec, symbol })` instead.
*/
price(min?: number, max?: number, dec?: number, symbol?: string): string;
/**
* Generates a price between min and max (inclusive).
*
* @param options The minimum price or on options object. Defaults to `{}`.
* @param options.min The minimum price. Defaults to `1`.
* @param options.max The maximum price. Defaults to `1000`.
* @param options.dec The number of decimal places. Defaults to `2`.
* @param options.symbol The currency value to use. Defaults to `''`.
* @param legacyMax The maximum price. This argument is deprecated. Defaults to `1000`.
* @param legacyDec The number of decimal places. This argument is deprecated. Defaults to `2`.
* @param legacySymbol The currency value to use. This argument is deprecated. Defaults to `''`.
*
* @example
* faker.commerce.price() // 828.00
* faker.commerce.price({ min: 100 }) // 904.00
* faker.commerce.price({ min: 100, max: 200 }) // 154.00
* faker.commerce.price({ min: 100, max: 200, dec: 0 }) // 133
* faker.commerce.price({ min: 100, max: 200, dec: 0, symbol: '$' }) // $114
*
* @since 3.0.0
*/
price(
options?:
| number
| {
min?: number;
max?: number;
dec?: number;
symbol?: string;
},
legacyMax?: number,
legacyDec?: number,
legacySymbol?: string
): string;
/**
* Generates a price between min and max (inclusive).
*
* @param options The minimum price or on options object. Defaults to `{}`.
* @param options.min The minimum price. Defaults to `1`.
* @param options.max The maximum price. Defaults to `1000`.
* @param options.dec The number of decimal places. Defaults to `2`.
* @param options.symbol The currency value to use. Defaults to `''`.
* @param legacyMax The maximum price. This argument is deprecated. Defaults to `1000`.
* @param legacyDec The number of decimal places. This argument is deprecated. Defaults to `2`.
* @param legacySymbol The currency value to use. This argument is deprecated. Defaults to `''`.
*
* @example
* faker.commerce.price() // 828.00
* faker.commerce.price({ min: 100 }) // 904.00
* faker.commerce.price({ min: 100, max: 200 }) // 154.00
* faker.commerce.price({ min: 100, max: 200, dec: 0 }) // 133
* faker.commerce.price({ min: 100, max: 200, dec: 0, symbol: '$' }) // $114
*
* @since 3.0.0
*/
price(
options:
| number
| {
min?: number;
max?: number;
dec?: number;
symbol?: string;
} = {},
legacyMax: number = 1000,
legacyDec: number = 2,
legacySymbol: string = ''
): string {
if (typeof options === 'number') {
deprecated({
deprecated: 'faker.commerce.price(min, max, dec, symbol)',
proposed: 'faker.commerce.price({ min, max, dec, symbol })',
since: '8.0',
until: '9.0',
});
options = {
min: options,
dec: legacyDec,
max: legacyMax,
symbol: legacySymbol,
};
}
const { dec = 2, max = 1000, min = 1, symbol = '' } = options;
if (min < 0 || max < 0) {
return `${symbol}${0.0}`;
}
// TODO @Shinigami92 2022-11-24: https://github.com/faker-js/faker/issues/350
const randValue = this.faker.number.int({ min, max });
return symbol + randValue.toFixed(dec);
}
/**
* Returns an adjective describing a product.
*
* @example
* faker.commerce.productAdjective() // 'Handcrafted'
*
* @since 3.0.0
*/
productAdjective(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.commerce.product_name.adjective
);
}
/**
* Returns a material of a product.
*
* @example
* faker.commerce.productMaterial() // 'Rubber'
*
* @since 3.0.0
*/
productMaterial(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.commerce.product_name.material
);
}
/**
* Returns a short product name.
*
* @example
* faker.commerce.product() // 'Computer'
*
* @since 3.0.0
*/
product(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.commerce.product_name.product
);
}
/**
* Returns a product description.
*
* @example
* faker.commerce.productDescription() // 'Andy shoes are designed to keeping...'
*
* @since 5.0.0
*/
productDescription(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.commerce.product_description
);
}
}
|