Skip to content

Commit

Permalink
feat(financial-calculating): add calculateDiscountParentage method (#…
Browse files Browse the repository at this point in the history
…33)

* feat(financial-calculating): add calculateDiscountPercentage function for discount percentage calculation

* docs(financial-calculating): add tsDoc for new method

* docs(financial-calculate): add Documentation for the new method

* test(financial-calculating): write three test for calculate discount percentage

* test(financial-calculating): update tests for calculateDiscountAmount and calculateDiscountedPrice functions

* refactor(financial-calculating): separate calculateDiscountPercentage for calculate profit and discount

* refactor(financial-calculating): rename discount percentage functions for clarity

* lint: Make Happy :)
  • Loading branch information
arashagp authored Jan 11, 2025
1 parent 50504fd commit 7e61430
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 26 deletions.
9 changes: 9 additions & 0 deletions packages/financial-calculate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ Calculate the discount amount from the original price.
```ts
calculateDiscountAmount(100, 10, 1); // returns 10.0
```

### calculateDiscountPercentage

Calculates the discount percentage between the market price and the sale price.

```ts
calculateDiscountPercentage(100, 80); // Returns 20.00
calculateDiscountPercentage(100, 80, 1, false); // Returns 25.0
```
47 changes: 21 additions & 26 deletions packages/financial-calculate/src/main.test.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
import test from 'ava';

import {calculateDiscountAmount, calculateDiscountedPrice} from '@nexim/financial-calculate';

test('calculate discount from price with: 3, 4 input', (test) => {
import {
calculateDiscountAmount,
calculateDiscountedPrice,
calculatePercentageDiscount,
calculatePercentageProfit,
} from '@nexim/financial-calculate';

test('calculate discount from price', (test) => {
test.is(calculateDiscountAmount(3, 4), 0.12);
});

test('calculateDiscountAmount(100, 10) returns 10.00', (t) => {
t.is(calculateDiscountAmount(100, 10), 10);
});

test('calculate price from discount with: 3, 4 input', (test) => {
test.is(calculateDiscountedPrice(3, 4), 2.88);
});

test('calculate discount from price with: 587, 629 input', (test) => {
test.is(calculateDiscountAmount(100, 10), 10);
test.is(calculateDiscountAmount(587, 629), 3692.23);
});

test('calculate price from discount with: 587, 629 input', (test) => {
test.is(calculateDiscountedPrice(587, 629), -3105.23);
});

test('calculate discount from price with: 15034, 73 input', (test) => {
test.is(calculateDiscountAmount(15034, 73), 10974.82);
test.is(calculateDiscountAmount(54205, 1332, 5), 722010.6);
});

test('calculate price from discount with: 15034, 73 input', (test) => {
test('calculate price from discount', (test) => {
test.is(calculateDiscountedPrice(3, 4), 2.88);
test.is(calculateDiscountedPrice(100, 10), 90);
test.is(calculateDiscountedPrice(15034, 73), 4059.18);
test.is(calculateDiscountedPrice(587, 629), -3105.23);
test.is(calculateDiscountedPrice(54205, 1332, 5), -667805.6);
});

test('calculate discount from price with: 54205, 1332, 5 input', (test) => {
test.is(calculateDiscountAmount(54205, 1332, 5), 722010.6);
test('calculate discount percentage for profit and discount', (test) => {
test.is(calculatePercentageProfit(100, 80), 25);
test.is(calculatePercentageProfit(100, 53), 88.68);
});

test('calculate price from discount with: 54205, 1332, 5 input', (test) => {
test.is(calculateDiscountedPrice(54205, 1332, 5), -667805.6);
test('calculate discount percentage for profit', (test) => {
test.is(calculatePercentageDiscount(100, 80, 1), 20);
test.is(calculatePercentageDiscount(100, 53), 47);
});
42 changes: 42 additions & 0 deletions packages/financial-calculate/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,45 @@ export function calculateDiscountAmount(price: number, discount: number, decimal
const discountAmount = (price * discount) / 100;
return parseFloat(discountAmount.toFixed(decimal));
}

/**
* Calculates the discount percentage between the market price and the sale price for profit.
*
* @param marketPrice - The original market price of the item.
* @param salePrice - The sale price of the item.
* @param decimal - The number of decimal places to round the result to(optional with default value = 2).
* @param upSide - Determines the denominator for the percentage calculation (optional with default value = true).
*
* @example
* ```
* calculateDiscountPercentage(100, 80); // Returns 20.00
* calculateDiscountPercentage(100, 80, 1, false); // Returns 25.0
* ```
*/
export function calculatePercentageProfit(marketPrice: number, salePrice: number, decimal = 2): number {
logger.logMethodArgs?.('calculatePercentageProfit', {marketPrice, salePrice, decimal});

const percentage = ((marketPrice - salePrice) / salePrice) * 100;
return parseFloat(percentage.toFixed(decimal));
}

/**
* Calculates the discount percentage between the market price and the sale price for discount.
*
* @param marketPrice - The original market price of the item.
* @param salePrice - The sale price of the item.
* @param decimal - The number of decimal places to round the result to(optional with default value = 2).
* @param upSide - Determines the denominator for the percentage calculation (optional with default value = true).
*
* @example
* ```
* calculateDiscountPercentage(100, 80); // Returns 20.00
* calculateDiscountPercentage(100, 80, 1, false); // Returns 25.0
* ```
*/
export function calculatePercentageDiscount(marketPrice: number, salePrice: number, decimal = 2): number {
logger.logMethodArgs?.('calculatePercentageDiscount', {marketPrice, salePrice, decimal});

const percentage = ((marketPrice - salePrice) / marketPrice) * 100;
return parseFloat(percentage.toFixed(decimal));
}

0 comments on commit 7e61430

Please sign in to comment.