Skip to content

Commit

Permalink
feat: new financial-calculating (#29)
Browse files Browse the repository at this point in the history
* 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
arashagp and njfamirm authored Jan 9, 2025
1 parent d4f8264 commit 63eaceb
Show file tree
Hide file tree
Showing 8 changed files with 911 additions and 3 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ Here is a brief overview of the included libraries:
2. [`alpine`](./packages/alpine): Utility functions to enhance Alpine.js usage with backup support.
3. [`service-worker`](./packages/service-worker): Utilities to simplify the usage of service workers in your web applications.
4. [`google-analytics`](./packages/google-analytics): Utilities provides functions to set up Google Analytics for your web application.
5. [`typescript-config`](./packages/typescript-config): Base TypeScript configuration for Nexim projects.
6. [`prettier-config`](./packages/prettier-config): Base Prettier configuration for Nexim projects.
7. [`eslint-config`](./packages/eslint-config): Base Eslint configuration for Nexim projects.
5. [`financial-calculate`](./packages/financial-calculate): Provides a set of utils to handle common financial operations.
6. [`typescript-config`](./packages/typescript-config): Base TypeScript configuration for Nexim projects.
7. [`prettier-config`](./packages/prettier-config): Base Prettier configuration for Nexim projects.
8. [`eslint-config`](./packages/eslint-config): Base Eslint configuration for Nexim projects.

For more detailed information and guidelines on how to use each package, please refer to each package's README.

Expand Down
661 changes: 661 additions & 0 deletions packages/financial-calculate/LICENSE

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions packages/financial-calculate/README.md
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
```
105 changes: 105 additions & 0 deletions packages/financial-calculate/package.json
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
}
}
}
39 changes: 39 additions & 0 deletions packages/financial-calculate/src/main.test.js
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);
});
40 changes: 40 additions & 0 deletions packages/financial-calculate/src/main.ts
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));
}
9 changes: 9 additions & 0 deletions packages/financial-calculate/tsconfig.json
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"]
}
15 changes: 15 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,21 @@ __metadata:
languageName: unknown
linkType: soft

"@nexim/financial-calculate@workspace:packages/financial-calculate":
version: 0.0.0-use.local
resolution: "@nexim/financial-calculate@workspace:packages/financial-calculate"
dependencies:
"@alwatr/logger": "npm:^5.0.0"
"@alwatr/nano-build": "npm:^5.0.0"
"@alwatr/package-tracer": "npm:^5.0.0"
"@alwatr/type-helper": "npm:^5.0.0"
"@nexim/typescript-config": "workspace:^"
ava: "npm:^6.2.0"
typescript: "npm:^5.7.2"
wireit: "npm:^0.14.9"
languageName: unknown
linkType: soft

"@nexim/google-analytics@workspace:packages/google-analytics":
version: 0.0.0-use.local
resolution: "@nexim/google-analytics@workspace:packages/google-analytics"
Expand Down

0 comments on commit 63eaceb

Please sign in to comment.