11import 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 }
0 commit comments