Skip to content

Commit 915b439

Browse files
author
Matt Mayer
authored
Merge branch 'next' into 1777-dutch-names
2 parents cb94028 + 99308d4 commit 915b439

4 files changed

Lines changed: 180 additions & 9 deletions

File tree

docs/about/team/members.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"desc": "Passionate TypeScript enthusiast. Also core member of Vite.",
1010
"links": [
1111
{ "icon": "github", "link": "https://github.com/Shinigami92" },
12-
{ "icon": "twitter", "link": "https://twitter.com/Shini_92" }
12+
{ "icon": "mastodon", "link": "https://elk.zone/mas.to/@Shini92" }
1313
],
1414
"sponsor": "https://github.com/sponsors/Shinigami92"
1515
},

src/modules/commerce/index.ts

Lines changed: 131 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '../../faker';
2+
import { deprecated } from '../../internal/deprecated';
23

34
/**
45
* Module to generate commerce and product related entries.
@@ -41,6 +42,50 @@ export class CommerceModule {
4142
return `${this.productAdjective()} ${this.productMaterial()} ${this.product()}`;
4243
}
4344

45+
/**
46+
* Generates a price between min and max (inclusive).
47+
*
48+
* @param options An options object. Defaults to `{}`.
49+
* @param options.min The minimum price. Defaults to `1`.
50+
* @param options.max The maximum price. Defaults to `1000`.
51+
* @param options.dec The number of decimal places. Defaults to `2`.
52+
* @param options.symbol The currency value to use. Defaults to `''`.
53+
*
54+
* @example
55+
* faker.commerce.price() // 828.00
56+
* faker.commerce.price({ min: 100 }) // 904.00
57+
* faker.commerce.price({ min: 100, max: 200 }) // 154.00
58+
* faker.commerce.price({ min: 100, max: 200, dec: 0 }) // 133
59+
* faker.commerce.price({ min: 100, max: 200, dec: 0, symbol: '$' }) // $114
60+
*
61+
* @since 3.0.0
62+
*/
63+
price(options?: {
64+
/**
65+
* The minimum price.
66+
*
67+
* @default 1
68+
*/
69+
min?: number;
70+
/**
71+
* The maximum price.
72+
*
73+
* @default 1000
74+
*/
75+
max?: number;
76+
/**
77+
* The number of decimal places.
78+
*
79+
* @default 2
80+
*/
81+
dec?: number;
82+
/**
83+
* The currency value to use.
84+
*
85+
* @default ''
86+
*/
87+
symbol?: string;
88+
}): string;
4489
/**
4590
* Generates a price between min and max (inclusive).
4691
*
@@ -57,13 +102,95 @@ export class CommerceModule {
57102
* faker.commerce.price(100, 200, 0, '$') // $114
58103
*
59104
* @since 3.0.0
105+
*
106+
* @deprecated Use `faker.commerce.price({ min, max, dec, symbol })` instead.
107+
*/
108+
price(min?: number, max?: number, dec?: number, symbol?: string): string;
109+
/**
110+
* Generates a price between min and max (inclusive).
111+
*
112+
* @param options The minimum price or on options object. Defaults to `{}`.
113+
* @param options.min The minimum price. Defaults to `1`.
114+
* @param options.max The maximum price. Defaults to `1000`.
115+
* @param options.dec The number of decimal places. Defaults to `2`.
116+
* @param options.symbol The currency value to use. Defaults to `''`.
117+
* @param legacyMax The maximum price. This argument is deprecated. Defaults to `1000`.
118+
* @param legacyDec The number of decimal places. This argument is deprecated. Defaults to `2`.
119+
* @param legacySymbol The currency value to use. This argument is deprecated. Defaults to `''`.
120+
*
121+
* @example
122+
* faker.commerce.price() // 828.00
123+
* faker.commerce.price({ min: 100 }) // 904.00
124+
* faker.commerce.price({ min: 100, max: 200 }) // 154.00
125+
* faker.commerce.price({ min: 100, max: 200, dec: 0 }) // 133
126+
* faker.commerce.price({ min: 100, max: 200, dec: 0, symbol: '$' }) // $114
127+
*
128+
* @since 3.0.0
129+
*/
130+
price(
131+
options?:
132+
| number
133+
| {
134+
min?: number;
135+
max?: number;
136+
dec?: number;
137+
symbol?: string;
138+
},
139+
legacyMax?: number,
140+
legacyDec?: number,
141+
legacySymbol?: string
142+
): string;
143+
/**
144+
* Generates a price between min and max (inclusive).
145+
*
146+
* @param options The minimum price or on options object. Defaults to `{}`.
147+
* @param options.min The minimum price. Defaults to `1`.
148+
* @param options.max The maximum price. Defaults to `1000`.
149+
* @param options.dec The number of decimal places. Defaults to `2`.
150+
* @param options.symbol The currency value to use. Defaults to `''`.
151+
* @param legacyMax The maximum price. This argument is deprecated. Defaults to `1000`.
152+
* @param legacyDec The number of decimal places. This argument is deprecated. Defaults to `2`.
153+
* @param legacySymbol The currency value to use. This argument is deprecated. Defaults to `''`.
154+
*
155+
* @example
156+
* faker.commerce.price() // 828.00
157+
* faker.commerce.price({ min: 100 }) // 904.00
158+
* faker.commerce.price({ min: 100, max: 200 }) // 154.00
159+
* faker.commerce.price({ min: 100, max: 200, dec: 0 }) // 133
160+
* faker.commerce.price({ min: 100, max: 200, dec: 0, symbol: '$' }) // $114
161+
*
162+
* @since 3.0.0
60163
*/
61164
price(
62-
min: number = 1,
63-
max: number = 1000,
64-
dec: number = 2,
65-
symbol: string = ''
165+
options:
166+
| number
167+
| {
168+
min?: number;
169+
max?: number;
170+
dec?: number;
171+
symbol?: string;
172+
} = {},
173+
legacyMax: number = 1000,
174+
legacyDec: number = 2,
175+
legacySymbol: string = ''
66176
): string {
177+
if (typeof options === 'number') {
178+
deprecated({
179+
deprecated: 'faker.commerce.price(min, max, dec, symbol)',
180+
proposed: 'faker.commerce.price({ min, max, dec, symbol })',
181+
since: '8.0',
182+
until: '9.0',
183+
});
184+
options = {
185+
min: options,
186+
dec: legacyDec,
187+
max: legacyMax,
188+
symbol: legacySymbol,
189+
};
190+
}
191+
192+
const { dec = 2, max = 1000, min = 1, symbol = '' } = options;
193+
67194
if (min < 0 || max < 0) {
68195
return `${symbol}${0.0}`;
69196
}

test/__snapshots__/commerce.spec.ts.snap

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ exports[`commerce > 42 > department 1`] = `"Tools"`;
44

55
exports[`commerce > 42 > price > noArgs 1`] = `"375.00"`;
66

7-
exports[`commerce > 42 > price > with max 1`] = `"38.00"`;
7+
exports[`commerce > 42 > price > with max 1`] = `"375.00"`;
8+
9+
exports[`commerce > 42 > price > with max option 1`] = `"501.00"`;
810

911
exports[`commerce > 42 > price > with min 1`] = `"406.00"`;
1012

@@ -14,6 +16,14 @@ exports[`commerce > 42 > price > with min and max and decimals 1`] = `"69.0000"`
1416

1517
exports[`commerce > 42 > price > with min and max and decimals and symbol 1`] = `"$69.0000"`;
1618

19+
exports[`commerce > 42 > price > with min and max and decimals and symbol option 1`] = `"$69.0000"`;
20+
21+
exports[`commerce > 42 > price > with min and max and decimals option 1`] = `"69.0000"`;
22+
23+
exports[`commerce > 42 > price > with min and max option 1`] = `"69.00"`;
24+
25+
exports[`commerce > 42 > price > with min option 1`] = `"401.00"`;
26+
1727
exports[`commerce > 42 > product 1`] = `"Pants"`;
1828

1929
exports[`commerce > 42 > productAdjective 1`] = `"Fantastic"`;
@@ -28,7 +38,9 @@ exports[`commerce > 1211 > department 1`] = `"Automotive"`;
2838

2939
exports[`commerce > 1211 > price > noArgs 1`] = `"929.00"`;
3040

31-
exports[`commerce > 1211 > price > with max 1`] = `"93.00"`;
41+
exports[`commerce > 1211 > price > with max 1`] = `"929.00"`;
42+
43+
exports[`commerce > 1211 > price > with max option 1`] = `"1242.00"`;
3244

3345
exports[`commerce > 1211 > price > with min 1`] = `"933.00"`;
3446

@@ -38,6 +50,14 @@ exports[`commerce > 1211 > price > with min and max and decimals 1`] = `"97.0000
3850

3951
exports[`commerce > 1211 > price > with min and max and decimals and symbol 1`] = `"$97.0000"`;
4052

53+
exports[`commerce > 1211 > price > with min and max and decimals and symbol option 1`] = `"$97.0000"`;
54+
55+
exports[`commerce > 1211 > price > with min and max and decimals option 1`] = `"97.0000"`;
56+
57+
exports[`commerce > 1211 > price > with min and max option 1`] = `"97.00"`;
58+
59+
exports[`commerce > 1211 > price > with min option 1`] = `"932.00"`;
60+
4161
exports[`commerce > 1211 > product 1`] = `"Sausages"`;
4262

4363
exports[`commerce > 1211 > productAdjective 1`] = `"Unbranded"`;
@@ -52,7 +72,9 @@ exports[`commerce > 1337 > department 1`] = `"Computers"`;
5272

5373
exports[`commerce > 1337 > price > noArgs 1`] = `"263.00"`;
5474

55-
exports[`commerce > 1337 > price > with max 1`] = `"27.00"`;
75+
exports[`commerce > 1337 > price > with max 1`] = `"263.00"`;
76+
77+
exports[`commerce > 1337 > price > with max option 1`] = `"351.00"`;
5678

5779
exports[`commerce > 1337 > price > with min 1`] = `"299.00"`;
5880

@@ -62,6 +84,14 @@ exports[`commerce > 1337 > price > with min and max and decimals 1`] = `"63.0000
6284

6385
exports[`commerce > 1337 > price > with min and max and decimals and symbol 1`] = `"$63.0000"`;
6486

87+
exports[`commerce > 1337 > price > with min and max and decimals and symbol option 1`] = `"$63.0000"`;
88+
89+
exports[`commerce > 1337 > price > with min and max and decimals option 1`] = `"63.0000"`;
90+
91+
exports[`commerce > 1337 > price > with min and max option 1`] = `"63.00"`;
92+
93+
exports[`commerce > 1337 > price > with min option 1`] = `"293.00"`;
94+
6595
exports[`commerce > 1337 > product 1`] = `"Ball"`;
6696

6797
exports[`commerce > 1337 > productAdjective 1`] = `"Incredible"`;

test/commerce.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,21 @@ describe('commerce', () => {
2525
.it('with max', undefined, 100)
2626
.it('with min and max', 50, 100)
2727
.it('with min and max and decimals', 50, 100, 4)
28-
.it('with min and max and decimals and symbol', 50, 100, 4, '$');
28+
.it('with min and max and decimals and symbol', 50, 100, 4, '$')
29+
.it('with min option', { min: 42 })
30+
.it('with max option', { max: 1337 })
31+
.it('with min and max option', { min: 50, max: 100 })
32+
.it('with min and max and decimals option', {
33+
min: 50,
34+
max: 100,
35+
dec: 4,
36+
})
37+
.it('with min and max and decimals and symbol option', {
38+
min: 50,
39+
max: 100,
40+
dec: 4,
41+
symbol: '$',
42+
});
2943
});
3044
});
3145

0 commit comments

Comments
 (0)