From 8766fd7f3d8658e77c4f0cbc2b63792372715940 Mon Sep 17 00:00:00 2001 From: DivisionByZero Date: Mon, 6 Feb 2023 10:56:51 +0100 Subject: [PATCH] refactor(commerce): price use options object (#1805) --- src/modules/commerce/index.ts | 135 ++++++++++++++++++++++- test/__snapshots__/commerce.spec.ts.snap | 36 +++++- test/commerce.spec.ts | 16 ++- 3 files changed, 179 insertions(+), 8 deletions(-) diff --git a/src/modules/commerce/index.ts b/src/modules/commerce/index.ts index 4afa881988f..66f6d02c47d 100644 --- a/src/modules/commerce/index.ts +++ b/src/modules/commerce/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../../faker'; +import { deprecated } from '../../internal/deprecated'; /** * Module to generate commerce and product related entries. @@ -41,6 +42,50 @@ export class CommerceModule { 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). * @@ -57,13 +102,95 @@ export class CommerceModule { * 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( - min: number = 1, - max: number = 1000, - dec: number = 2, - symbol: string = '' + 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}`; } diff --git a/test/__snapshots__/commerce.spec.ts.snap b/test/__snapshots__/commerce.spec.ts.snap index de1569a9752..48c8b4ba54d 100644 --- a/test/__snapshots__/commerce.spec.ts.snap +++ b/test/__snapshots__/commerce.spec.ts.snap @@ -4,7 +4,9 @@ exports[`commerce > 42 > department 1`] = `"Tools"`; exports[`commerce > 42 > price > noArgs 1`] = `"375.00"`; -exports[`commerce > 42 > price > with max 1`] = `"38.00"`; +exports[`commerce > 42 > price > with max 1`] = `"375.00"`; + +exports[`commerce > 42 > price > with max option 1`] = `"501.00"`; exports[`commerce > 42 > price > with min 1`] = `"406.00"`; @@ -14,6 +16,14 @@ exports[`commerce > 42 > price > with min and max and decimals 1`] = `"69.0000"` exports[`commerce > 42 > price > with min and max and decimals and symbol 1`] = `"$69.0000"`; +exports[`commerce > 42 > price > with min and max and decimals and symbol option 1`] = `"$69.0000"`; + +exports[`commerce > 42 > price > with min and max and decimals option 1`] = `"69.0000"`; + +exports[`commerce > 42 > price > with min and max option 1`] = `"69.00"`; + +exports[`commerce > 42 > price > with min option 1`] = `"401.00"`; + exports[`commerce > 42 > product 1`] = `"Pants"`; exports[`commerce > 42 > productAdjective 1`] = `"Fantastic"`; @@ -28,7 +38,9 @@ exports[`commerce > 1211 > department 1`] = `"Automotive"`; exports[`commerce > 1211 > price > noArgs 1`] = `"929.00"`; -exports[`commerce > 1211 > price > with max 1`] = `"93.00"`; +exports[`commerce > 1211 > price > with max 1`] = `"929.00"`; + +exports[`commerce > 1211 > price > with max option 1`] = `"1242.00"`; exports[`commerce > 1211 > price > with min 1`] = `"933.00"`; @@ -38,6 +50,14 @@ exports[`commerce > 1211 > price > with min and max and decimals 1`] = `"97.0000 exports[`commerce > 1211 > price > with min and max and decimals and symbol 1`] = `"$97.0000"`; +exports[`commerce > 1211 > price > with min and max and decimals and symbol option 1`] = `"$97.0000"`; + +exports[`commerce > 1211 > price > with min and max and decimals option 1`] = `"97.0000"`; + +exports[`commerce > 1211 > price > with min and max option 1`] = `"97.00"`; + +exports[`commerce > 1211 > price > with min option 1`] = `"932.00"`; + exports[`commerce > 1211 > product 1`] = `"Sausages"`; exports[`commerce > 1211 > productAdjective 1`] = `"Unbranded"`; @@ -52,7 +72,9 @@ exports[`commerce > 1337 > department 1`] = `"Computers"`; exports[`commerce > 1337 > price > noArgs 1`] = `"263.00"`; -exports[`commerce > 1337 > price > with max 1`] = `"27.00"`; +exports[`commerce > 1337 > price > with max 1`] = `"263.00"`; + +exports[`commerce > 1337 > price > with max option 1`] = `"351.00"`; exports[`commerce > 1337 > price > with min 1`] = `"299.00"`; @@ -62,6 +84,14 @@ exports[`commerce > 1337 > price > with min and max and decimals 1`] = `"63.0000 exports[`commerce > 1337 > price > with min and max and decimals and symbol 1`] = `"$63.0000"`; +exports[`commerce > 1337 > price > with min and max and decimals and symbol option 1`] = `"$63.0000"`; + +exports[`commerce > 1337 > price > with min and max and decimals option 1`] = `"63.0000"`; + +exports[`commerce > 1337 > price > with min and max option 1`] = `"63.00"`; + +exports[`commerce > 1337 > price > with min option 1`] = `"293.00"`; + exports[`commerce > 1337 > product 1`] = `"Ball"`; exports[`commerce > 1337 > productAdjective 1`] = `"Incredible"`; diff --git a/test/commerce.spec.ts b/test/commerce.spec.ts index 781b6431e5c..30b7dde1e47 100644 --- a/test/commerce.spec.ts +++ b/test/commerce.spec.ts @@ -25,7 +25,21 @@ describe('commerce', () => { .it('with max', undefined, 100) .it('with min and max', 50, 100) .it('with min and max and decimals', 50, 100, 4) - .it('with min and max and decimals and symbol', 50, 100, 4, '$'); + .it('with min and max and decimals and symbol', 50, 100, 4, '$') + .it('with min option', { min: 42 }) + .it('with max option', { max: 1337 }) + .it('with min and max option', { min: 50, max: 100 }) + .it('with min and max and decimals option', { + min: 50, + max: 100, + dec: 4, + }) + .it('with min and max and decimals and symbol option', { + min: 50, + max: 100, + dec: 4, + symbol: '$', + }); }); });