Skip to content

Commit

Permalink
refactor(commerce)!: remove v8 deprecated commerce method (#2752)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Mar 15, 2024
1 parent 776a625 commit 8c80877
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 208 deletions.
7 changes: 7 additions & 0 deletions docs/guide/upgrading_v9/2752.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Remove deprecated commerce method

Removed deprecated commerce method

| old | replacement |
| --------------------------------------------- | ------------------------------------------------- |
| `faker.commerce.price(min, max, dec, symbol)` | `faker.commerce.price({ min, max, dec, symbol })` |
195 changes: 26 additions & 169 deletions src/modules/commerce/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FakerError } from '../../errors/faker-error';
import { deprecated } from '../../internal/deprecated';
import { ModuleBase } from '../../internal/module-base';

// Source for official prefixes: https://www.isbn-international.org/range_file_generation
Expand Down Expand Up @@ -138,176 +137,34 @@ export class CommerceModule extends ModuleBase {
*
* @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).
*
* To better represent real-world prices, when `options.dec` is greater than `0`, the final decimal digit in the returned string will be generated as follows:
*
* - 50% of the time: `9`
* - 30% of the time: `5`
* - 10% of the time: `0`
* - 10% of the time: a random digit from `0` to `9`
*
* @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.07
* faker.commerce.price(100) // 904.19
* faker.commerce.price(100, 200) // 154.55
* 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).
*
* To better represent real-world prices, when `options.dec` is greater than `0`, the final decimal digit in the returned string will be generated as follows:
*
* - 50% of the time: `9`
* - 30% of the time: `5`
* - 10% of the time: `0`
* - 10% of the time: a random digit from `0` to `9`
*
* @param options The minimum price or an options object.
* @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.07
* faker.commerce.price({ min: 100 }) // 904.19
* faker.commerce.price({ min: 100, max: 200 }) // 154.55
* 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
| {
/**
* 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;
},
legacyMax?: number,
legacyDec?: number,
legacySymbol?: string
): string;
/**
* Generates a price between min and max (inclusive).
*
* To better represent real-world prices, when `options.dec` is greater than `0`, the final decimal digit in the returned string will be generated as follows:
*
* - 50% of the time: `9`
* - 30% of the time: `5`
* - 10% of the time: `0`
* - 10% of the time: a random digit from `0` to `9`
*
* @param options The minimum price or an options object.
* @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.07
* faker.commerce.price({ min: 100 }) // 904.19
* faker.commerce.price({ min: 100, max: 200 }) // 154.55
* 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 = ''
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 {
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) {
Expand Down
30 changes: 0 additions & 30 deletions test/modules/__snapshots__/commerce.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,8 @@ exports[`commerce > 42 > price > noArgs 1`] = `"375.15"`;

exports[`commerce > 42 > price > with float min and float max option 1`] = `"1.05"`;

exports[`commerce > 42 > price > with max 1`] = `"375.15"`;

exports[`commerce > 42 > price > with max option 1`] = `"501.35"`;

exports[`commerce > 42 > price > with min 1`] = `"405.85"`;

exports[`commerce > 42 > price > with min and max 1`] = `"68.75"`;

exports[`commerce > 42 > price > with min and max and decimals 1`] = `"68.7275"`;

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

exports[`commerce > 42 > price > with min and max and decimals and symbol option 1`] = `"$68.7275"`;

exports[`commerce > 42 > price > with min and max and decimals option 1`] = `"68.7275"`;
Expand Down Expand Up @@ -62,18 +52,8 @@ exports[`commerce > 1211 > price > noArgs 1`] = `"928.69"`;

exports[`commerce > 1211 > price > with float min and float max option 1`] = `"1.10"`;

exports[`commerce > 1211 > price > with max 1`] = `"928.69"`;

exports[`commerce > 1211 > price > with max option 1`] = `"1241.59"`;

exports[`commerce > 1211 > price > with min 1`] = `"932.19"`;

exports[`commerce > 1211 > price > with min and max 1`] = `"96.49"`;

exports[`commerce > 1211 > price > with min and max and decimals 1`] = `"96.4269"`;

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

exports[`commerce > 1211 > price > with min and max and decimals and symbol option 1`] = `"$96.4269"`;

exports[`commerce > 1211 > price > with min and max and decimals option 1`] = `"96.4269"`;
Expand Down Expand Up @@ -108,18 +88,8 @@ exports[`commerce > 1337 > price > noArgs 1`] = `"262.79"`;

exports[`commerce > 1337 > price > with float min and float max option 1`] = `"1.09"`;

exports[`commerce > 1337 > price > with max 1`] = `"262.79"`;

exports[`commerce > 1337 > price > with max option 1`] = `"351.09"`;

exports[`commerce > 1337 > price > with min 1`] = `"298.99"`;

exports[`commerce > 1337 > price > with min and max 1`] = `"63.19"`;

exports[`commerce > 1337 > price > with min and max and decimals 1`] = `"63.1019"`;

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

exports[`commerce > 1337 > price > with min and max and decimals and symbol option 1`] = `"$63.1019"`;

exports[`commerce > 1337 > price > with min and max and decimals option 1`] = `"63.1019"`;
Expand Down
13 changes: 4 additions & 9 deletions test/modules/commerce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ describe('commerce', () => {

t.describe('price', (t) => {
t.it('noArgs')
.it('with min', 50)
.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 option', { min: 42 })
.it('with max option', { max: 1337 })
.it('with min and max option', { min: 50, max: 100 })
Expand Down Expand Up @@ -115,29 +110,29 @@ describe('commerce', () => {
});

it('should handle negative amounts, but return 0', () => {
const amount = faker.commerce.price(-200, -1);
const amount = faker.commerce.price({ min: -200, max: -1 });

expect(amount).toBeTruthy();
expect(amount, 'the amount should equal 0').toBe('0');
});

it('should handle argument dec', () => {
const price = faker.commerce.price(100, 100, 1);
const price = faker.commerce.price({ min: 100, max: 100, dec: 1 });

expect(price).toBeTruthy();
expect(price, 'the price should equal 100.0').toBe('100.0');
});

it('should handle argument dec = 0', () => {
const price = faker.commerce.price(100, 100, 0);
const price = faker.commerce.price({ min: 100, max: 100, dec: 0 });

expect(price).toBeTruthy();
expect(price, 'the price should equal 100').toBe('100');
});

it('should return decimal values between min and max', () => {
const result = faker.helpers.multiple(
() => faker.commerce.price(1, 1.1, 2),
() => faker.commerce.price({ min: 1, max: 1.1, dec: 2 }),
{ count: 50 }
);

Expand Down

0 comments on commit 8c80877

Please sign in to comment.