Skip to content

Commit

Permalink
feat: weighted last digits
Browse files Browse the repository at this point in the history
  • Loading branch information
import-brain committed Oct 18, 2023
1 parent 6b74642 commit 0f0286b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 33 deletions.
21 changes: 20 additions & 1 deletion src/modules/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,32 @@ export class CommerceModule {
return `${symbol}${0.0}`;
}

if (min === max) {
return `${symbol}${min.toFixed(dec)}`;
}

const randValue = this.faker.number.float({
min,
max,
precision: (1 / 10) ** dec,
});

return symbol + randValue.toFixed(dec);
if (dec === 0) {
return `${symbol}${randValue.toFixed(dec)}`;
}

const randValueString = randValue.toFixed(dec).toString();
const lastDigit = this.faker.helpers.weightedArrayElement([
{ weight: 5, value: '9' },
{ weight: 3, value: '5' },
{ weight: 1, value: '0' },
{
weight: 1,
value: this.faker.number.int({ min: 0, max: 9 }).toString(),
},
]);

return `${symbol}${randValueString.replace(/\d$/, lastDigit)}`;
}

/**
Expand Down
60 changes: 30 additions & 30 deletions test/modules/__snapshots__/commerce.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ exports[`commerce > 42 > isbn > with variant 10 and space separators 1`] = `"0 7

exports[`commerce > 42 > isbn > with variant 13 1`] = `"978-0-7917-7551-6"`;

exports[`commerce > 42 > price > noArgs 1`] = `"375.16"`;
exports[`commerce > 42 > price > noArgs 1`] = `"375.17"`;

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

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

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

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

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

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

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

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

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

exports[`commerce > 42 > price > with min option 1`] = `"400.80"`;
exports[`commerce > 42 > price > with min option 1`] = `"400.87"`;

exports[`commerce > 42 > product 1`] = `"Pants"`;

Expand All @@ -56,27 +56,27 @@ exports[`commerce > 1211 > isbn > with variant 10 and space separators 1`] = `"1

exports[`commerce > 1211 > isbn > with variant 13 1`] = `"978-1-4872-1906-2"`;

exports[`commerce > 1211 > price > noArgs 1`] = `"928.59"`;
exports[`commerce > 1211 > price > noArgs 1`] = `"928.50"`;

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

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

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

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

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

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

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

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

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

exports[`commerce > 1211 > price > with min option 1`] = `"931.52"`;
exports[`commerce > 1211 > price > with min option 1`] = `"931.50"`;

exports[`commerce > 1211 > product 1`] = `"Sausages"`;

Expand All @@ -100,27 +100,27 @@ exports[`commerce > 1337 > isbn > with variant 10 and space separators 1`] = `"0

exports[`commerce > 1337 > isbn > with variant 13 1`] = `"978-0-512-25403-0"`;

exports[`commerce > 1337 > price > noArgs 1`] = `"262.76"`;
exports[`commerce > 1337 > price > noArgs 1`] = `"262.79"`;

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

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

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

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

exports[`commerce > 1337 > price > with min and max and decimals 1`] = `"63.1012"`;
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.1012"`;
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.1012"`;
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.1012"`;
exports[`commerce > 1337 > price > with min and max and decimals option 1`] = `"63.1019"`;

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

exports[`commerce > 1337 > price > with min option 1`] = `"293.01"`;
exports[`commerce > 1337 > price > with min option 1`] = `"293.09"`;

exports[`commerce > 1337 > product 1`] = `"Ball"`;

Expand Down
4 changes: 2 additions & 2 deletions test/modules/commerce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ describe('commerce', () => {
const price = faker.commerce.price(100, 100, 1);

expect(price).toBeTruthy();
expect(price, 'the price should be equal 100.0').toBe('100.0');
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);

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

Expand Down

0 comments on commit 0f0286b

Please sign in to comment.