-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new
financial-calculating
(#29)
* feat(financial-calculating): initial Document Calculating * docs(financial-calculate): enhance README and add detailed API documentation * refactor(financial-calculate): update package.json files and improve function naming in calculate.ts * feat(financial-calculating): write test * docs(financial-calculate): improve parameter descriptions in calculate.ts * feat(financial-calculate): more and more test's * lint: Make Happy :) * docs(financial-calculate): fix name of package installation * refactor(financial-calculate): change name of functions * docs(financial-calculate): change `Api` to `API` * refactor: remove files except anything about financial calculate * chore(financial-calculate): rename all functions changed * fix: change typescript extends * refactor(financial-document): use parse float for better handling * fix: update TypeScript configuration to extend browser library * docs: readme --------- Co-authored-by: S. Amir Mohammad Najafi <[email protected]>
- Loading branch information
Showing
8 changed files
with
911 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# @nexim/financial-calculate | ||
|
||
![NPM Version](https://img.shields.io/npm/v/@nexim/financial-calculate) | ||
![npm bundle size](https://img.shields.io/bundlephobia/min/@nexim/financial-calculate) | ||
![Build & Lint & Test](https://github.com/the-nexim/nanolib/actions/workflows/build-lint-test.yaml/badge.svg) | ||
![NPM Downloads](https://img.shields.io/npm/dm/@nexim/financial-calculate) | ||
![NPM License](https://img.shields.io/npm/l/@nexim/financial-calculate) | ||
|
||
Provides a set of utils to handle common financial operations. | ||
|
||
## Installation | ||
|
||
To install the package, use npm or yarn: | ||
|
||
```sh | ||
npm install @nexim/financial-calculate | ||
|
||
# Or using yarn | ||
yarn add @nexim/financial-calculate | ||
``` | ||
|
||
## API | ||
|
||
### calculateDiscountedPrice | ||
|
||
Calculate the price after applying a discount. | ||
|
||
```ts | ||
calculateDiscountedPrice(100, 10, 1); // returns 90.0 | ||
``` | ||
|
||
### calculateDiscountAmount | ||
|
||
Calculate the discount amount from the original price. | ||
|
||
```ts | ||
calculateDiscountAmount(100, 10, 1); // returns 10.0 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
{ | ||
"name": "@nexim/financial-calculate", | ||
"version": "0.0.0", | ||
"description": "Provides a set of utils to handle common financial operations.", | ||
"keywords": [ | ||
"financial", | ||
"calculate", | ||
"payment", | ||
"typescript", | ||
"nexim" | ||
], | ||
"homepage": "https://github.com/the-nexim/nanolib/tree/next/packages/financial-calculate#readme", | ||
"bugs": { | ||
"url": "https://github.com/the-nexim/nanolib/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/the-nexim/nanolib", | ||
"directory": "packages/financial-calculate" | ||
}, | ||
"license": "AGPL-3.0-only", | ||
"author": "S. Amir Mohammad Najafi <[email protected]> (https://www.njfamirm.ir)", | ||
"contributors": [ | ||
"Arash Ghardashpoor <[email protected]> (https://www.agpagp.ir)" | ||
], | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"types": "./dist/main.d.ts", | ||
"import": "./dist/main.mjs", | ||
"require": "./dist/main.cjs" | ||
} | ||
}, | ||
"main": "./dist/main.cjs", | ||
"module": "./dist/main.mjs", | ||
"types": "./dist/main.d.ts", | ||
"files": [ | ||
"**/*.{js,mjs,cjs,map,d.ts,html,md,LEGAL.txt}", | ||
"LICENSE" | ||
], | ||
"scripts": { | ||
"build": "wireit", | ||
"test": "wireit", | ||
"watch": "wireit" | ||
}, | ||
"dependencies": { | ||
"@alwatr/logger": "^5.0.0", | ||
"@alwatr/package-tracer": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"@alwatr/nano-build": "^5.0.0", | ||
"@alwatr/type-helper": "^5.0.0", | ||
"@nexim/typescript-config": "workspace:^", | ||
"ava": "^6.2.0", | ||
"typescript": "^5.7.2", | ||
"wireit": "^0.14.9" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"nano-build": { | ||
"platform": "browser" | ||
}, | ||
"wireit": { | ||
"test": { | ||
"command": "NODE_OPTIONS=\"$NODE_OPTIONS --enable-source-maps --experimental-vm-modules\" ava" | ||
}, | ||
"build": { | ||
"dependencies": [ | ||
"build:type", | ||
"build:es" | ||
] | ||
}, | ||
"build:es": { | ||
"command": "nano-build --preset=module", | ||
"files": [ | ||
"src", | ||
"tsconfig.json" | ||
], | ||
"clean": "if-file-deleted", | ||
"output": [ | ||
"dist", | ||
"tsconfig.tsbuildinfo" | ||
] | ||
}, | ||
"build:type": { | ||
"command": "tsc --build" | ||
}, | ||
"watch": { | ||
"dependencies": [ | ||
"watch:ts" | ||
] | ||
}, | ||
"watch:ts": { | ||
"command": "tsc --build --watch --preserveWatchOutput", | ||
"dependencies": [ | ||
"watch:es" | ||
] | ||
}, | ||
"watch:es": { | ||
"command": "nano-build --preset=module -- --watch", | ||
"service": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import test from 'ava'; | ||
|
||
import {calculateDiscountAmount, calculateDiscountedPrice} from '@nexim/financial-calculate'; | ||
|
||
test('calculate discount from price with: 3, 4 input', (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(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('calculate price from discount with: 15034, 73 input', (test) => { | ||
test.is(calculateDiscountedPrice(15034, 73), 4059.18); | ||
}); | ||
|
||
test('calculate discount from price with: 54205, 1332, 5 input', (test) => { | ||
test.is(calculateDiscountAmount(54205, 1332, 5), 722010.6); | ||
}); | ||
|
||
test('calculate price from discount with: 54205, 1332, 5 input', (test) => { | ||
test.is(calculateDiscountedPrice(54205, 1332, 5), -667805.6); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {createLogger} from '@alwatr/logger'; | ||
import {packageTracer} from '@alwatr/package-tracer'; | ||
|
||
__dev_mode__: packageTracer.add(__package_name__, __package_version__); | ||
|
||
const logger = createLogger(__package_name__); | ||
|
||
/** | ||
* Calculate the price after applying a discount. | ||
* | ||
* @param price - The original price. | ||
* @param discount - The discount percentage to apply. | ||
* @param decimal - The number of decimal places to round to (default is 2). | ||
* | ||
* @example | ||
* calculateDiscountedPrice(100, 10, 1); // returns 90.0 | ||
*/ | ||
export function calculateDiscountedPrice(price: number, discount: number, decimal = 2): number { | ||
logger.logMethodArgs?.('calculateDiscountedPrice', {price, discount, decimal}); | ||
|
||
const discountedPrice = price * (1 - discount / 100); | ||
return parseFloat(discountedPrice.toFixed(decimal)); | ||
} | ||
|
||
/** | ||
* Calculate the discount amount from the original price. | ||
* | ||
* @param price - The original price. | ||
* @param discount - The discount percentage. | ||
* @param decimal - The number of decimal places to round to (default is 2). | ||
* | ||
* @example | ||
* calculateDiscountAmount(100, 10, 1); // returns 10.0 | ||
*/ | ||
export function calculateDiscountAmount(price: number, discount: number, decimal = 2): number { | ||
logger.logMethodArgs?.('calculateDiscountAmount', {price, discount, decimal}); | ||
|
||
const discountAmount = (price * discount) / 100; | ||
return parseFloat(discountAmount.toFixed(decimal)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "@nexim/typescript-config/tsconfig.browser.lib.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "dist", | ||
"types": ["@alwatr/nano-build", "@alwatr/type-helper"] | ||
}, | ||
"include": ["src/**/*.ts", "src/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters